diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 33ee960c2..cf8b7864c 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -118,6 +118,7 @@ jest.spyOn(context, 'tmpDir').mockImplementation((): string => { const tmpDir = path.join('/tmp/.docker-build-push-jest').split(path.sep).join(path.posix.sep); if (!fs.existsSync(tmpDir)) { fs.mkdirSync(tmpDir, {recursive: true}); + 2; } return tmpDir; }); @@ -280,23 +281,77 @@ bbbb ccc"`], ['file', './test/Dockerfile'], ['builder', 'builder-git-context-2'], - ['push', 'true'] + ['push', 'true'], + ['trace-data', 'true'] ]), [ 'buildx', 'build', + '--label', 'dockerfile-path=https://github.com/docker/build-push-action.git#test-jest/./test/Dockerfile', '--platform', 'linux/amd64,linux/arm64', '--iidfile', '/tmp/.docker-build-push-jest/iidfile', '--secret', 'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest', '--secret', 'id=MYSECRET,src=/tmp/.docker-build-push-jest/.tmpname-jest', '--secret', 'id=FOO,src=/tmp/.docker-build-push-jest/.tmpname-jest', - '--secret', 'id=EMPTYLINE,src=/tmp/.docker-build-push-jest/.tmpname-jest', + '--secret', 'id=EMPTYLINE,src=/tmp/.docker-build-push-jest/.tmpname- '--file', './test/Dockerfile', '--builder', 'builder-git-context-2', '--push', 'https://github.com/docker/build-push-action.git#heads/master' ] ], + [ + '0.4.2', + new Map([ + ['context', '.'], + ['outputs', 'type=image,dest=./release-out'], + ['trace-data', 'true'] + ]), + [ + 'buildx', + 'build', + '--label', 'dockerfile-path=https://github.com/docker/build-push-action.git#test-jest/Dockerfile', + '--output', 'type=image,dest=./release-out', + '--iidfile', '/tmp/.docker-build-push-jest/iidfile', + '--file', 'Dockerfile', + '.' + ] + ], + [ + '0.4.2', + new Map([ + ['context', '.'], + ['outputs', 'type=registry,dest=./release-out'], + ['trace-data', 'true'] + ]), + [ + 'buildx', + 'build', + '--label', 'dockerfile-path=https://github.com/docker/build-push-action.git#test-jest/Dockerfile', + '--output', 'type=registry,dest=./release-out', + '--iidfile', '/tmp/.docker-build-push-jest/iidfile', + '--file', 'Dockerfile', + '.' + ] + ], + [ + '0.4.2', + new Map([ + ['context', '.'], + ['load', 'true'], + ['trace-data', 'true'] + ]), + [ + 'buildx', + 'build', + '--label', 'dockerfile-path=https://github.com/docker/build-push-action.git#test-jest/Dockerfile', + '--iidfile', '/tmp/.docker-build-push-jest/iidfile', + '--file', 'Dockerfile', + '--load', + '.' + ] + ] + , [ '0.4.2', new Map([ diff --git a/action.yml b/action.yml index cf1c5809b..106ae8fe8 100644 --- a/action.yml +++ b/action.yml @@ -70,7 +70,10 @@ inputs: ssh: description: "List of SSH agent socket or keys to expose to the build" required: false - + trace-data: + description: "Flag to indicate whether link to dockerfile is added to image labels or not" + default: 'false' + required: false outputs: digest: description: 'Image content-addressable identifier also called a digest' diff --git a/dist/index.js b/dist/index.js index a6a2974c7..9a5c9f236 100644 --- a/dist/index.js +++ b/dist/index.js @@ -12135,7 +12135,7 @@ function tmpNameSync(options) { exports.tmpNameSync = tmpNameSync; function getInputs(defaultContext) { return __awaiter(this, void 0, void 0, function* () { - return { + let userInputs = { context: core.getInput('context') || defaultContext, file: core.getInput('file') || 'Dockerfile', buildArgs: yield getInputList('build-args', true), @@ -12154,8 +12154,26 @@ function getInputs(defaultContext) { cacheTo: yield getInputList('cache-to', true), secrets: yield getInputList('secrets', true), githubToken: core.getInput('github-token'), - ssh: yield getInputList('ssh') + ssh: yield getInputList('ssh'), + traceData: core.getInput('trace-data') || 'false' }; + if (userInputs.traceData == 'true' && //if user explictly asks to add traceData + (userInputs.load == true || + userInputs.push == true || + userInputs.outputs.find(val => val.indexOf('type=image') > -1 || val.indexOf('type=registry') > -1))) { + //Add link to dockerfile as label + let dockerfilePath = userInputs.file; + let stringToReplace = ''; + if (defaultContext.indexOf('#heads')) { + stringToReplace = '.git#heads'; + } + else if (defaultContext.indexOf('#tags')) { + stringToReplace = '.git#tags'; + } + let repoPath = defaultContext.replace(stringToReplace, '/blob'); + userInputs.labels.push(`dockerfile-path=${repoPath}/${dockerfilePath}`); + } + return userInputs; }); } exports.getInputs = getInputs; diff --git a/src/context.ts b/src/context.ts index f62f2b8e5..a8060c1b7 100644 --- a/src/context.ts +++ b/src/context.ts @@ -55,7 +55,7 @@ export function tmpNameSync(options?: tmp.TmpNameOptions): string { } export async function getInputs(defaultContext: string): Promise { - return { + let userInputs = { context: core.getInput('context') || defaultContext, file: core.getInput('file') || 'Dockerfile', buildArgs: await getInputList('build-args', true), @@ -74,8 +74,30 @@ export async function getInputs(defaultContext: string): Promise { cacheTo: await getInputList('cache-to', true), secrets: await getInputList('secrets', true), githubToken: core.getInput('github-token'), - ssh: await getInputList('ssh') + ssh: await getInputList('ssh'), + traceData: core.getInput('trace-data') || 'false' }; + + if ( + userInputs.traceData == 'true' && //if user explictly asks to add traceData + (userInputs.load == true || + userInputs.push == true || + userInputs.outputs.find(val => val.indexOf('type=image') > -1 || val.indexOf('type=registry') > -1)) + ) { + //Add link to dockerfile as label + let dockerfilePath = userInputs.file; + let stringToReplace = ''; + if (defaultContext.indexOf('#heads')) { + stringToReplace = '.git#heads'; + } else if (defaultContext.indexOf('#tags')) { + stringToReplace = '.git#tags'; + } + + let repoPath = defaultContext.replace(stringToReplace, '/blob'); + userInputs.labels.push(`dockerfile-path=${repoPath}/${dockerfilePath}`); + } + + return userInputs; } export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise> {