Skip to content

Commit

Permalink
Add subdirectory for Git context
Browse files Browse the repository at this point in the history
Since v0.9.0 of BuildKit (BuildX v0.7.0) you can provide a subdirectory
to the default Git context.

Closes docker#460
Closes docker#528
  • Loading branch information
BeyondEvil committed Dec 28, 2021
1 parent b1aeb11 commit 8f6d7e3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<owner>/<repo>.git#<ref>`.

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
Expand Down
18 changes: 18 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,24 @@ nproc=3`],
'.'
]
],
[
15,
'0.7.0',
new Map<string, string>([
['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<string, any>, expected: Array<string>) => {
Expand Down
4 changes: 4 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ export async function getArgs(inputs: Inputs, defaultContext: string, buildxVers
let args: Array<string> = ['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;
}

Expand Down

0 comments on commit 8f6d7e3

Please sign in to comment.