Merge pull request #347 from crazy-max/fix-ref-spec

Fix ref spec for default Git context
This commit is contained in:
CrazyMax 2021-05-26 13:00:32 +02:00 committed by GitHub
commit 74242a33cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 15 deletions

@ -112,7 +112,7 @@ PejgXO0uIRolYQ3sz2tMGhx1MfBqH64=
-----END PGP PRIVATE KEY BLOCK-----`; -----END PGP PRIVATE KEY BLOCK-----`;
jest.spyOn(context, 'defaultContext').mockImplementation((): string => { 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 => { jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
@ -162,7 +162,7 @@ describe('getArgs', () => {
'--build-arg', 'MY_ARG=val1,val2,val3', '--build-arg', 'MY_ARG=val1,val2,val3',
'--build-arg', 'ARG=val', '--build-arg', 'ARG=val',
'--iidfile', '/tmp/.docker-build-push-jest/iidfile', '--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'
] ]
], ],
[ [
@ -176,7 +176,7 @@ describe('getArgs', () => {
'--tag', 'name/app:7.4', '--tag', 'name/app:7.4',
'--tag', 'name/app:latest', '--tag', 'name/app:latest',
'--iidfile', '/tmp/.docker-build-push-jest/iidfile', '--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'
] ]
], ],
[ [
@ -245,13 +245,13 @@ describe('getArgs', () => {
'build', 'build',
'--output', '.', '--output', '.',
'--secret', 'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest', '--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', '0.4.2',
new Map<string, string>([ 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'], ['tag', 'localhost:5000/name/app:latest'],
['platforms', 'linux/amd64,linux/arm64'], ['platforms', 'linux/amd64,linux/arm64'],
['secrets', 'GIT_AUTH_TOKEN=abcdefghijklmno=0123456789'], ['secrets', 'GIT_AUTH_TOKEN=abcdefghijklmno=0123456789'],
@ -268,13 +268,13 @@ describe('getArgs', () => {
'--file', './test/Dockerfile', '--file', './test/Dockerfile',
'--builder', 'builder-git-context-2', '--builder', 'builder-git-context-2',
'--push', '--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', '0.4.2',
new Map<string, string>([ 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'], ['tag', 'localhost:5000/name/app:latest'],
['platforms', 'linux/amd64,linux/arm64'], ['platforms', 'linux/amd64,linux/arm64'],
['secrets', `GIT_AUTH_TOKEN=abcdefghi,jklmno=0123456789 ['secrets', `GIT_AUTH_TOKEN=abcdefghi,jklmno=0123456789
@ -302,13 +302,13 @@ ccc"`],
'--file', './test/Dockerfile', '--file', './test/Dockerfile',
'--builder', 'builder-git-context-2', '--builder', 'builder-git-context-2',
'--push', '--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', '0.4.2',
new Map<string, string>([ 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'], ['tag', 'localhost:5000/name/app:latest'],
['platforms', 'linux/amd64,linux/arm64'], ['platforms', 'linux/amd64,linux/arm64'],
['secrets', `GIT_AUTH_TOKEN=abcdefghi,jklmno=0123456789 ['secrets', `GIT_AUTH_TOKEN=abcdefghi,jklmno=0123456789
@ -336,13 +336,13 @@ ccc`],
'--file', './test/Dockerfile', '--file', './test/Dockerfile',
'--builder', 'builder-git-context-2', '--builder', 'builder-git-context-2',
'--push', '--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', '0.5.1',
new Map<string, string>([ 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'], ['tag', 'localhost:5000/name/app:latest'],
['secret-files', `MY_SECRET=${path.join(__dirname, 'fixtures', 'secret.txt').split(path.sep).join(path.posix.sep)}`], ['secret-files', `MY_SECRET=${path.join(__dirname, 'fixtures', 'secret.txt').split(path.sep).join(path.posix.sep)}`],
['file', './test/Dockerfile'], ['file', './test/Dockerfile'],
@ -359,7 +359,7 @@ ccc`],
'--builder', 'builder-git-context-2', '--builder', 'builder-git-context-2',
'--network', 'host', '--network', 'host',
'--push', '--push',
'https://github.com/docker/build-push-action.git#heads/master' 'https://github.com/docker/build-push-action.git#refs/heads/master'
] ]
], ],
[ [

10
dist/index.js generated vendored

@ -13401,9 +13401,15 @@ const github = __importStar(__webpack_require__(438));
const buildx = __importStar(__webpack_require__(295)); const buildx = __importStar(__webpack_require__(295));
let _defaultContext, _tmpDir; let _defaultContext, _tmpDir;
function defaultContext() { function defaultContext() {
var _a, _b;
if (!_defaultContext) { if (!_defaultContext) {
_defaultContext = `${process.env.GITHUB_SERVER_URL || 'https://github.com'}/${github.context.repo.owner}/${github.context.repo.repo}.git#${(_b = (_a = github.context) === null || _a === void 0 ? void 0 : _a.ref) === null || _b === void 0 ? void 0 : _b.replace(/^refs\//, '')}`; let ref = github.context.ref;
if (github.context.sha && ref && !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#${ref}`;
} }
return _defaultContext; return _defaultContext;
} }

@ -39,9 +39,16 @@ export interface Inputs {
export function defaultContext(): string { export function defaultContext(): string {
if (!_defaultContext) { if (!_defaultContext) {
let ref = github.context.ref;
if (github.context.sha && ref && !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}/${ _defaultContext = `${process.env.GITHUB_SERVER_URL || 'https://github.com'}/${github.context.repo.owner}/${
github.context.repo.repo github.context.repo.repo
}.git#${github.context?.ref?.replace(/^refs\//, '')}`; }.git#${ref}`;
} }
return _defaultContext; return _defaultContext;
} }