Skip to content

Commit 74242a3

Browse files
authored
Merge pull request #347 from crazy-max/fix-ref-spec
Fix ref spec for default Git context
2 parents 2b2d125 + 19c0738 commit 74242a3

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

__tests__/context.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ PejgXO0uIRolYQ3sz2tMGhx1MfBqH64=
112112
-----END PGP PRIVATE KEY BLOCK-----`;
113113

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

118118
jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
@@ -162,7 +162,7 @@ describe('getArgs', () => {
162162
'--build-arg', 'MY_ARG=val1,val2,val3',
163163
'--build-arg', 'ARG=val',
164164
'--iidfile', '/tmp/.docker-build-push-jest/iidfile',
165-
'https://github.com/docker/build-push-action.git#test-jest'
165+
'https://github.com/docker/build-push-action.git#refs/heads/test-jest'
166166
]
167167
],
168168
[
@@ -176,7 +176,7 @@ describe('getArgs', () => {
176176
'--tag', 'name/app:7.4',
177177
'--tag', 'name/app:latest',
178178
'--iidfile', '/tmp/.docker-build-push-jest/iidfile',
179-
'https://github.com/docker/build-push-action.git#test-jest'
179+
'https://github.com/docker/build-push-action.git#refs/heads/test-jest'
180180
]
181181
],
182182
[
@@ -245,13 +245,13 @@ describe('getArgs', () => {
245245
'build',
246246
'--output', '.',
247247
'--secret', 'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest',
248-
'https://github.com/docker/build-push-action.git#test-jest'
248+
'https://github.com/docker/build-push-action.git#refs/heads/test-jest'
249249
]
250250
],
251251
[
252252
'0.4.2',
253253
new Map<string, string>([
254-
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
254+
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
255255
['tag', 'localhost:5000/name/app:latest'],
256256
['platforms', 'linux/amd64,linux/arm64'],
257257
['secrets', 'GIT_AUTH_TOKEN=abcdefghijklmno=0123456789'],
@@ -268,13 +268,13 @@ describe('getArgs', () => {
268268
'--file', './test/Dockerfile',
269269
'--builder', 'builder-git-context-2',
270270
'--push',
271-
'https://github.com/docker/build-push-action.git#heads/master'
271+
'https://github.com/docker/build-push-action.git#refs/heads/master'
272272
]
273273
],
274274
[
275275
'0.4.2',
276276
new Map<string, string>([
277-
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
277+
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
278278
['tag', 'localhost:5000/name/app:latest'],
279279
['platforms', 'linux/amd64,linux/arm64'],
280280
['secrets', `GIT_AUTH_TOKEN=abcdefghi,jklmno=0123456789
@@ -302,13 +302,13 @@ ccc"`],
302302
'--file', './test/Dockerfile',
303303
'--builder', 'builder-git-context-2',
304304
'--push',
305-
'https://github.com/docker/build-push-action.git#heads/master'
305+
'https://github.com/docker/build-push-action.git#refs/heads/master'
306306
]
307307
],
308308
[
309309
'0.4.2',
310310
new Map<string, string>([
311-
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
311+
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
312312
['tag', 'localhost:5000/name/app:latest'],
313313
['platforms', 'linux/amd64,linux/arm64'],
314314
['secrets', `GIT_AUTH_TOKEN=abcdefghi,jklmno=0123456789
@@ -336,13 +336,13 @@ ccc`],
336336
'--file', './test/Dockerfile',
337337
'--builder', 'builder-git-context-2',
338338
'--push',
339-
'https://github.com/docker/build-push-action.git#heads/master'
339+
'https://github.com/docker/build-push-action.git#refs/heads/master'
340340
]
341341
],
342342
[
343343
'0.5.1',
344344
new Map<string, string>([
345-
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
345+
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
346346
['tag', 'localhost:5000/name/app:latest'],
347347
['secret-files', `MY_SECRET=${path.join(__dirname, 'fixtures', 'secret.txt').split(path.sep).join(path.posix.sep)}`],
348348
['file', './test/Dockerfile'],
@@ -359,7 +359,7 @@ ccc`],
359359
'--builder', 'builder-git-context-2',
360360
'--network', 'host',
361361
'--push',
362-
'https://github.com/docker/build-push-action.git#heads/master'
362+
'https://github.com/docker/build-push-action.git#refs/heads/master'
363363
]
364364
],
365365
[

dist/index.js

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/context.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,16 @@ export interface Inputs {
3939

4040
export function defaultContext(): string {
4141
if (!_defaultContext) {
42+
let ref = github.context.ref;
43+
if (github.context.sha && ref && !ref.startsWith('refs/')) {
44+
ref = `refs/heads/${github.context.ref}`;
45+
}
46+
if (github.context.sha && !ref.startsWith(`refs/pull/`)) {
47+
ref = github.context.sha;
48+
}
4249
_defaultContext = `${process.env.GITHUB_SERVER_URL || 'https://github.com'}/${github.context.repo.owner}/${
4350
github.context.repo.repo
44-
}.git#${github.context?.ref?.replace(/^refs\//, '')}`;
51+
}.git#${ref}`;
4552
}
4653
return _defaultContext;
4754
}

0 commit comments

Comments
 (0)