Skip to content

Commit

Permalink
Fix ref spec for default Git context
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Apr 21, 2021
1 parent c0c3e27 commit b3b8ef1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
24 changes: 12 additions & 12 deletions __tests__/context.test.ts
Expand Up @@ -111,7 +111,7 @@ PejgXO0uIRolYQ3sz2tMGhx1MfBqH64=
-----END PGP PRIVATE KEY BLOCK-----`;

jest.spyOn(context, 'defaultContext').mockImplementation((): string => {
return 'https://github.com/docker/build-push-action.git#test-jest';
return 'https://github.com/docker/build-push-action.git#refs/heads/test-jest';
});

jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('getArgs', () => {
'--build-arg', 'MY_ARG=val1,val2,val3',
'--build-arg', 'ARG=val',
'--iidfile', '/tmp/.docker-build-push-jest/iidfile',
'https://github.com/docker/build-push-action.git#test-jest'
'https://github.com/docker/build-push-action.git#refs/heads/test-jest'
]
],
[
Expand All @@ -175,7 +175,7 @@ describe('getArgs', () => {
'--tag', 'name/app:7.4',
'--tag', 'name/app:latest',
'--iidfile', '/tmp/.docker-build-push-jest/iidfile',
'https://github.com/docker/build-push-action.git#test-jest'
'https://github.com/docker/build-push-action.git#refs/heads/test-jest'
]
],
[
Expand Down Expand Up @@ -244,13 +244,13 @@ describe('getArgs', () => {
'build',
'--output', '.',
'--secret', 'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest',
'https://github.com/docker/build-push-action.git#test-jest'
'https://github.com/docker/build-push-action.git#refs/heads/test-jest'
]
],
[
'0.4.2',
new Map<string, string>([
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
['tag', 'localhost:5000/name/app:latest'],
['platforms', 'linux/amd64,linux/arm64'],
['secrets', 'GIT_AUTH_TOKEN=abcdefghijklmno=0123456789'],
Expand All @@ -267,13 +267,13 @@ describe('getArgs', () => {
'--file', './test/Dockerfile',
'--builder', 'builder-git-context-2',
'--push',
'https://github.com/docker/build-push-action.git#heads/master'
'https://github.com/docker/build-push-action.git#refs/heads/master'
]
],
[
'0.4.2',
new Map<string, string>([
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
['tag', 'localhost:5000/name/app:latest'],
['platforms', 'linux/amd64,linux/arm64'],
['secrets', `GIT_AUTH_TOKEN=abcdefghi,jklmno=0123456789
Expand Down Expand Up @@ -301,13 +301,13 @@ ccc"`],
'--file', './test/Dockerfile',
'--builder', 'builder-git-context-2',
'--push',
'https://github.com/docker/build-push-action.git#heads/master'
'https://github.com/docker/build-push-action.git#refs/heads/master'
]
],
[
'0.4.2',
new Map<string, string>([
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
['tag', 'localhost:5000/name/app:latest'],
['platforms', 'linux/amd64,linux/arm64'],
['secrets', `GIT_AUTH_TOKEN=abcdefghi,jklmno=0123456789
Expand Down Expand Up @@ -335,13 +335,13 @@ ccc`],
'--file', './test/Dockerfile',
'--builder', 'builder-git-context-2',
'--push',
'https://github.com/docker/build-push-action.git#heads/master'
'https://github.com/docker/build-push-action.git#refs/heads/master'
]
],
[
'0.5.1',
new Map<string, string>([
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
['tag', 'localhost:5000/name/app:latest'],
['secret-files', `MY_SECRET=${path.join(__dirname, 'fixtures', 'secret.txt').split(path.sep).join(path.posix.sep)}`],
['file', './test/Dockerfile'],
Expand All @@ -358,7 +358,7 @@ ccc`],
'--builder', 'builder-git-context-2',
'--network', 'host',
'--push',
'https://github.com/docker/build-push-action.git#heads/master'
'https://github.com/docker/build-push-action.git#refs/heads/master'
]
]
])(
Expand Down
10 changes: 8 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/context.ts
Expand Up @@ -38,9 +38,16 @@ export interface Inputs {

export function defaultContext(): string {
if (!_defaultContext) {
let ref = github.context.ref;
if (github.context.sha && github.context.ref && !github.context.ref.startsWith('refs/')) {
ref = `refs/heads/${github.context.ref}`;
}
if (github.context.sha && !ref.startsWith(`refs/pull/`)) {
ref = github.context.sha;
}
_defaultContext = `${process.env.GITHUB_SERVER_URL || 'https://github.com'}/${github.context.repo.owner}/${
github.context.repo.repo
}.git#${github.context?.ref?.replace(/^refs\//, '')}`;
}.git#${ref}`;
}
return _defaultContext;
}
Expand Down

0 comments on commit b3b8ef1

Please sign in to comment.