Skip to content

Commit

Permalink
default to git context
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jun 18, 2024
1 parent f36da5f commit c25eb4c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 53 deletions.
19 changes: 15 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
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,10 +147,11 @@ jobs:
name: Build
uses: ./
with:
source: .
files: |
./test/config.hcl
source:
remote:
runs-on: ubuntu-latest
steps:
-
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 All @@ -466,6 +474,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 @@ -509,6 +518,7 @@ jobs:
uses: ./
with:
workdir: ./test/go
source: .
targets: image
load: true
push: true
Expand Down Expand Up @@ -541,6 +551,7 @@ jobs:
name: Build
uses: ./
with:
source: .
files: |
./test/config.hcl
targets: app
Expand Down
101 changes: 53 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
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 @@ -26,57 +26,23 @@ ___

## Usage

### Path context

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.

```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
-
name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/bake-action@v5
with:
push: true
```
### Git context

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.
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.

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:
Expand All @@ -95,15 +61,17 @@ jobs:
name: Build and push
uses: docker/bake-action@v5
with:
source: "${{ github.server_url }}/${{ github.repository }}.git#${{ github.ref }}"
push: true
set: |
*.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) alongside the [`actions/checkout`](https://github.com/actions/checkout/)
action to remove this restriction.
[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
Expand All @@ -116,6 +84,8 @@ to the default Git context:
with:
source: "{{defaultContext}}:mysubdir"
push: true
set: |
*.tags=user/app:latest
```

Building from the current repository automatically uses the `GITHUB_TOKEN`
Expand All @@ -132,12 +102,47 @@ another private repository for remote definitions, you can set the
name: Build and push
uses: docker/bake-action@v5
with:
source: "${{ github.server_url }}/${{ github.repository }}.git#${{ github.ref }}"
push: true
set: |
*.tags=user/app:latest
env:
BUILDX_BAKE_GIT_AUTH_TOKEN: ${{ secrets.MYTOKEN }}
```

### Path context

```yaml
name: ci
on:
push:
jobs:
bake:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/bake-action@v5
with:
source: .
push: true
set: |
*.tags=user/app:latest
```

## Customizing

### inputs
Expand Down
Loading

0 comments on commit c25eb4c

Please sign in to comment.