diff --git a/README.md b/README.md index f12393333..ea474cb56 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,19 @@ By default, this action uses the [Git context](#git-context) so you don't need t done directly by buildkit. The git reference will be based on the [event that triggered your workflow](https://docs.github.com/en/actions/reference/events-that-trigger-workflows) and will result in the following context: `https://github.com//.git#`. +Beginning with BuildKit version `v0.9.0` ([Buildx](https://github.com/docker/buildx) `v0.7.0`) you can provide a subdirectory to the [Git context](#git-context) by using the magic variable `{{defaultContext}}`: + +```yaml + - + name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + context: {{defaultContext}}:docker + push: true + tags: user/app:latest +``` + Be careful because **any file mutation in the steps that precede the build step will be ignored, including processing of the `.dockerignore` file** since the context is based on the git reference. However, you can use the [Path context](#path-context) using the [`context` input](#inputs) alongside the [`actions/checkout`](https://github.com/actions/checkout/) action to remove diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 7b6c16409..532159fe1 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -491,6 +491,24 @@ nproc=3`], '.' ] ], + [ + 15, + '0.7.0', + new Map([ + ['context', '{{defaultContext}}:docker'], + ['load', 'false'], + ['no-cache', 'false'], + ['push', 'false'], + ['pull', 'false'], + ]), + [ + 'buildx', + 'build', + '--iidfile', '/tmp/.docker-build-push-jest/iidfile', + '--metadata-file', '/tmp/.docker-build-push-jest/metadata-file', + 'https://github.com/docker/build-push-action.git#refs/heads/test-jest:docker' + ] + ], ])( '[%d] given %p with %p as inputs, returns %p', async (num: number, buildxVersion: string, inputs: Map, expected: Array) => { diff --git a/src/context.ts b/src/context.ts index e538ac719..cd834cb2e 100644 --- a/src/context.ts +++ b/src/context.ts @@ -97,7 +97,11 @@ export async function getArgs(inputs: Inputs, defaultContext: string, buildxVers let args: Array = ['buildx']; args.push.apply(args, await getBuildArgs(inputs, defaultContext, buildxVersion)); args.push.apply(args, await getCommonArgs(inputs, buildxVersion)); + if (inputs.context.startsWith('{{defaultContext}}') && buildx.satisfies(buildxVersion, '>=0.7.0')) { + inputs.context = inputs.context.replace('{{defaultContext}}', defaultContext); + } args.push(inputs.context); + return args; }