Skip to content

Commit

Permalink
git context support
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 Dec 19, 2023
1 parent 1e6dee0 commit f231da7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,22 @@ jobs:
with:
files: |
./test/config.hcl
git-context:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
version: ${{ inputs.buildx-version || env.BUILDX_VERSION }}
driver-opts: |
image=${{ inputs.buildkit-image || env.BUILDKIT_IMAGE }}
-
name: Build
uses: ./
with:
source: "{{defaultContext}}"
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ as a high-level build command.
___

* [Usage](#usage)
* [Default](#default)
* [Git context](#git-context)
* [Customizing](#customizing)
* [inputs](#inputs)
* [outputs](#outputs)
Expand All @@ -23,6 +25,8 @@ ___

## Usage

### Default

```yaml
name: ci

Expand Down Expand Up @@ -54,6 +58,46 @@ jobs:
push: true
```
### Git context
By default, this action will use the local bake definition, so you need to
use the [`actions/checkout`](https://github.com/actions/checkout/) action to
check out the repository.

If you want to use a remote bake definition, you can use the `source` input
to specify a remote bake definition to use. Default Git context can be provided
using the [Handlebars template](https://handlebarsjs.com/guide/) expression
`{{defaultContext}}`:

```yaml
name: ci
on:
push:
branches:
- 'master'
jobs:
bake:
runs-on: ubuntu-latest
steps:
-
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@v4
with:
source: "{{defaultContext}}"
push: true
```

## Customizing

### inputs
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"license": "Apache-2.0",
"dependencies": {
"@actions/core": "^1.10.1",
"@docker/actions-toolkit": "^0.14.0"
"@docker/actions-toolkit": "^0.14.0",
"handlebars": "^4.7.8"
},
"devDependencies": {
"@types/node": "^20.5.9",
Expand Down
11 changes: 8 additions & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as core from '@actions/core';
import * as handlebars from 'handlebars';
import {Bake} from '@docker/actions-toolkit/lib/buildx/bake';
import {Context} from '@docker/actions-toolkit/lib/context';
import {Inputs as BuildxInputs} from '@docker/actions-toolkit/lib/buildx/inputs';
import {GitHub} from '@docker/actions-toolkit/lib/github';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
Expand Down Expand Up @@ -48,8 +50,11 @@ export async function getArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<s

async function getBakeArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> {
const args: Array<string> = ['bake'];
if (inputs.source) {
args.push(inputs.source);
const source = handlebars.compile(inputs.source)({
defaultContext: Context.gitContext()
});
if (source) {
args.push(source);
}
await Util.asyncForEach(inputs.files, async file => {
args.push('--file', file);
Expand All @@ -61,7 +66,7 @@ async function getBakeArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<stri
args.push('--metadata-file', BuildxInputs.getBuildMetadataFilePath());
}
if (await toolkit.buildx.versionSatisfies('>=0.10.0')) {
const bakedef = await toolkit.bake.parseDefinitions([...inputs.files, inputs.source], inputs.targets, inputs.set, inputs.load, inputs.push, inputs.workdir);
const bakedef = await toolkit.bake.parseDefinitions([...inputs.files, source], inputs.targets, inputs.set, inputs.load, inputs.push, inputs.workdir);
if (inputs.provenance) {
args.push('--provenance', inputs.provenance);
} else if ((await toolkit.buildkit.versionSatisfies(inputs.builder, '>=0.11.0')) && !Bake.hasDockerExporter(bakedef, inputs.load)) {
Expand Down

0 comments on commit f231da7

Please sign in to comment.