Skip to content

Commit 19c0738

Browse files
committed
Fix ref spec for default Git context
Signed-off-by: CrazyMax <[email protected]>
1 parent c0c3e27 commit 19c0738

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
@@ -111,7 +111,7 @@ PejgXO0uIRolYQ3sz2tMGhx1MfBqH64=
111111
-----END PGP PRIVATE KEY BLOCK-----`;
112112

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

117117
jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
@@ -161,7 +161,7 @@ describe('getArgs', () => {
161161
'--build-arg', 'MY_ARG=val1,val2,val3',
162162
'--build-arg', 'ARG=val',
163163
'--iidfile', '/tmp/.docker-build-push-jest/iidfile',
164-
'https://github.com/docker/build-push-action.git#test-jest'
164+
'https://github.com/docker/build-push-action.git#refs/heads/test-jest'
165165
]
166166
],
167167
[
@@ -175,7 +175,7 @@ describe('getArgs', () => {
175175
'--tag', 'name/app:7.4',
176176
'--tag', 'name/app:latest',
177177
'--iidfile', '/tmp/.docker-build-push-jest/iidfile',
178-
'https://github.com/docker/build-push-action.git#test-jest'
178+
'https://github.com/docker/build-push-action.git#refs/heads/test-jest'
179179
]
180180
],
181181
[
@@ -244,13 +244,13 @@ describe('getArgs', () => {
244244
'build',
245245
'--output', '.',
246246
'--secret', 'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest',
247-
'https://github.com/docker/build-push-action.git#test-jest'
247+
'https://github.com/docker/build-push-action.git#refs/heads/test-jest'
248248
]
249249
],
250250
[
251251
'0.4.2',
252252
new Map<string, string>([
253-
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
253+
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
254254
['tag', 'localhost:5000/name/app:latest'],
255255
['platforms', 'linux/amd64,linux/arm64'],
256256
['secrets', 'GIT_AUTH_TOKEN=abcdefghijklmno=0123456789'],
@@ -267,13 +267,13 @@ describe('getArgs', () => {
267267
'--file', './test/Dockerfile',
268268
'--builder', 'builder-git-context-2',
269269
'--push',
270-
'https://github.com/docker/build-push-action.git#heads/master'
270+
'https://github.com/docker/build-push-action.git#refs/heads/master'
271271
]
272272
],
273273
[
274274
'0.4.2',
275275
new Map<string, string>([
276-
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
276+
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
277277
['tag', 'localhost:5000/name/app:latest'],
278278
['platforms', 'linux/amd64,linux/arm64'],
279279
['secrets', `GIT_AUTH_TOKEN=abcdefghi,jklmno=0123456789
@@ -301,13 +301,13 @@ ccc"`],
301301
'--file', './test/Dockerfile',
302302
'--builder', 'builder-git-context-2',
303303
'--push',
304-
'https://github.com/docker/build-push-action.git#heads/master'
304+
'https://github.com/docker/build-push-action.git#refs/heads/master'
305305
]
306306
],
307307
[
308308
'0.4.2',
309309
new Map<string, string>([
310-
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
310+
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
311311
['tag', 'localhost:5000/name/app:latest'],
312312
['platforms', 'linux/amd64,linux/arm64'],
313313
['secrets', `GIT_AUTH_TOKEN=abcdefghi,jklmno=0123456789
@@ -335,13 +335,13 @@ ccc`],
335335
'--file', './test/Dockerfile',
336336
'--builder', 'builder-git-context-2',
337337
'--push',
338-
'https://github.com/docker/build-push-action.git#heads/master'
338+
'https://github.com/docker/build-push-action.git#refs/heads/master'
339339
]
340340
],
341341
[
342342
'0.5.1',
343343
new Map<string, string>([
344-
['context', 'https://github.com/docker/build-push-action.git#heads/master'],
344+
['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'],
345345
['tag', 'localhost:5000/name/app:latest'],
346346
['secret-files', `MY_SECRET=${path.join(__dirname, 'fixtures', 'secret.txt').split(path.sep).join(path.posix.sep)}`],
347347
['file', './test/Dockerfile'],
@@ -358,7 +358,7 @@ ccc`],
358358
'--builder', 'builder-git-context-2',
359359
'--network', 'host',
360360
'--push',
361-
'https://github.com/docker/build-push-action.git#heads/master'
361+
'https://github.com/docker/build-push-action.git#refs/heads/master'
362362
]
363363
]
364364
])(

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
@@ -38,9 +38,16 @@ export interface Inputs {
3838

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

0 commit comments

Comments
 (0)