Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default to git context #193

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions .github/workflows/ci.yml
Expand Up @@ -69,6 +69,7 @@ jobs:
name: Build and push
uses: ./
with:
source: .
builder: ${{ steps.buildx.outputs.name }}
files: |
./test/config.hcl
Expand All @@ -87,6 +88,7 @@ jobs:
continue-on-error: true
uses: ./
with:
source: .
files: |
./test/config.hcl
set: |
Expand All @@ -108,6 +110,7 @@ jobs:
continue-on-error: true
uses: ./
with:
source: .
files: |
./test/config.hcl
-
Expand Down Expand Up @@ -144,6 +147,7 @@ jobs:
name: Build
uses: ./
with:
source: .
files: |
./test/config.hcl

Expand Down Expand Up @@ -188,6 +192,7 @@ jobs:
uses: ./
with:
workdir: ./test/go
source: .
targets: binary
provenance: ${{ matrix.attrs }}
set: |
Expand Down Expand Up @@ -229,6 +234,7 @@ jobs:
uses: ./
with:
workdir: ./test/go
source: .
targets: ${{ matrix.target }}
sbom: true
set: |
Expand Down Expand Up @@ -275,6 +281,7 @@ jobs:
uses: ./
with:
workdir: ./test/go
source: .
set: |
*.platform=linux/amd64
*.output=type=image,"name=localhost:5000/name/app:v1.0.0,localhost:5000/name/app:latest",push=true
Expand Down Expand Up @@ -304,6 +311,7 @@ jobs:
uses: ./
with:
workdir: ./test/group
source: .
push: true
set: |
t1.tags=localhost:5000/name/app:t1
Expand All @@ -324,6 +332,7 @@ jobs:
name: Build
uses: ./
with:
source: .
files: |
./test/config.hcl

Expand Down Expand Up @@ -361,6 +370,7 @@ jobs:
name: Build
uses: ./
with:
source: .
files: |
./test/config.hcl
targets: app-proxy
Expand Down Expand Up @@ -396,6 +406,7 @@ jobs:
name: Build
uses: ./
with:
source: .
files: |
./test/config.hcl

Expand All @@ -415,8 +426,6 @@ jobs:
-
name: Build
uses: ./
with:
source: "{{defaultContext}}"

git-context-and-local:
runs-on: ubuntu-latest
Expand All @@ -439,7 +448,6 @@ jobs:
name: Build
uses: ./
with:
source: "{{defaultContext}}"
files: |
cwd://${{ steps.meta.outputs.bake-file }}

Expand Down Expand Up @@ -468,6 +476,7 @@ jobs:
uses: ./
with:
workdir: ./test/go
source: .
set: |
*.output=type=image,name=localhost:5000/name/app:latest,push=true
*.output=type=docker,name=app:local
Expand Down Expand Up @@ -513,6 +522,7 @@ jobs:
uses: ./
with:
workdir: ./test/go
source: .
load: true
push: true
set: |
Expand Down
78 changes: 41 additions & 37 deletions README.md
Expand Up @@ -14,8 +14,8 @@ as a high-level build command.
___

* [Usage](#usage)
* [Path context](#path-context)
* [Git context](#git-context)
* [Path context](#path-context)
* [Customizing](#customizing)
* [inputs](#inputs)
* [outputs](#outputs)
Expand All @@ -25,27 +25,28 @@ ___

## Usage

### Path context
### Git context

Like the [build-push-action](https://github.com/docker/build-push-action), this
action uses the [Git context](https://docs.docker.com/build/bake/remote-definition/)
to build from a remote bake definition. This means that you don't need to use
the [`actions/checkout`](https://github.com/actions/checkout/) action to check
out the repository as [BuildKit](https://docs.docker.com/build/buildkit/) will
do this directly.

By default, this action will use the local bake definition (`source: .`), so
you need to use the [`actions/checkout`](https://github.com/actions/checkout/)
action to check out the repository.
The git reference will be based on the [event that triggered your workflow](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows)
and will result in the following context: `https://github.com/<owner>/<repo>.git#<ref>`.

```yaml
name: ci

on:
push:
branches:
- 'master'

jobs:
bake:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
Expand All @@ -60,27 +61,47 @@ jobs:
uses: docker/bake-action@v4
with:
push: true
set: |
*.tags=user/app:latest
```

### Git context
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 [`source` input](#inputs) alongside
the [`actions/checkout`](https://github.com/actions/checkout/) action to remove
this restriction.

Default Git context can also be provided using the [Handlebars template](https://handlebarsjs.com/guide/)
expression `{{defaultContext}}`. Here we can use it to provide a subdirectory
to the default Git context:

```yaml
-
name: Build and push
uses: docker/bake-action@v4
with:
source: "{{defaultContext}}:mysubdir"
push: true
set: |
*.tags=user/app:latest
```

Git context can be provided using the [`source` input](#inputs). This means
that you don't need to use the [`actions/checkout`](https://github.com/actions/checkout/)
action to check out the repository as [BuildKit](https://docs.docker.com/build/buildkit/)
will do this directly.
### Path context

```yaml
name: ci

on:
push:
branches:
- 'master'

jobs:
bake:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
Expand All @@ -94,27 +115,10 @@ jobs:
name: Build and push
uses: docker/bake-action@v4
with:
source: "${{ github.server_url }}/${{ github.repository }}.git#${{ github.ref }}"
push: true
```

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) alongside the [`actions/checkout`](https://github.com/actions/checkout/)
action to remove this restriction.

Default Git context can also be provided using the [Handlebars template](https://handlebarsjs.com/guide/)
expression `{{defaultContext}}`. Here we can use it to provide a subdirectory
to the default Git context:

```yaml
-
name: Build and push
uses: docker/bake-action@v4
with:
source: "{{defaultContext}}:mysubdir"
source: .
push: true
set: |
*.tags=user/app:latest
```

## Customizing
Expand Down
12 changes: 11 additions & 1 deletion __tests__/context.test.ts
Expand Up @@ -130,6 +130,7 @@ describe('getArgs', () => {
0,
'0.4.1',
new Map<string, string>([
['source', '.'],
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
Expand All @@ -143,6 +144,7 @@ describe('getArgs', () => {
1,
'0.8.2',
new Map<string, string>([
['source', '.'],
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
Expand All @@ -157,6 +159,7 @@ describe('getArgs', () => {
2,
'0.8.2',
new Map<string, string>([
['source', '.'],
['targets', 'webapp\nvalidate'],
['load', 'false'],
['no-cache', 'false'],
Expand All @@ -173,6 +176,7 @@ describe('getArgs', () => {
3,
'0.8.2',
new Map<string, string>([
['source', '.'],
['set', '*.cache-from=type=gha\n*.cache-to=type=gha'],
['load', 'false'],
['no-cache', 'false'],
Expand All @@ -190,6 +194,7 @@ describe('getArgs', () => {
4,
'0.10.0',
new Map<string, string>([
['source', '.'],
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
Expand All @@ -205,6 +210,7 @@ describe('getArgs', () => {
5,
'0.10.0',
new Map<string, string>([
['source', '.'],
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
Expand All @@ -221,6 +227,7 @@ describe('getArgs', () => {
6,
'0.10.0',
new Map<string, string>([
['source', '.'],
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
Expand All @@ -237,6 +244,7 @@ describe('getArgs', () => {
7,
'0.10.0',
new Map<string, string>([
['source', '.'],
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
Expand All @@ -253,6 +261,7 @@ describe('getArgs', () => {
8,
'0.10.0',
new Map<string, string>([
['source', '.'],
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
Expand All @@ -269,6 +278,7 @@ describe('getArgs', () => {
9,
'0.10.0',
new Map<string, string>([
['source', '.'],
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
Expand All @@ -289,6 +299,7 @@ describe('getArgs', () => {
10,
'0.10.0',
new Map<string, string>([
['source', '.'],
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
Expand All @@ -308,7 +319,6 @@ describe('getArgs', () => {
11,
'0.10.0',
new Map<string, string>([
['source', '{{defaultContext}}'],
['load', 'false'],
['no-cache', 'false'],
['push', 'false'],
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/context.ts
Expand Up @@ -35,7 +35,7 @@ export async function getInputs(): Promise<Inputs> {
push: core.getBooleanInput('push'),
sbom: core.getInput('sbom'),
set: Util.getInputList('set', {ignoreComma: true, quote: false}),
source: core.getInput('source')
source: core.getInput('source') || Context.gitContext()
};
}

Expand Down