From f231da7d1492c5a429585dcc050a1b5b014d1945 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 19 Dec 2023 13:56:22 +0100 Subject: [PATCH] git context support Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- .github/workflows/ci.yml | 19 +++++++++++++++++ README.md | 44 ++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- src/context.ts | 11 +++++++--- 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 82ee44c..a4ebd3c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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}}" diff --git a/README.md b/README.md index 979ac84..9b71979 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ as a high-level build command. ___ * [Usage](#usage) + * [Default](#default) + * [Git context](#git-context) * [Customizing](#customizing) * [inputs](#inputs) * [outputs](#outputs) @@ -23,6 +25,8 @@ ___ ## Usage +### Default + ```yaml name: ci @@ -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 diff --git a/package.json b/package.json index 5543ac2..d376220 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/context.ts b/src/context.ts index a55e3e5..fbd1843 100644 --- a/src/context.ts +++ b/src/context.ts @@ -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'; @@ -48,8 +50,11 @@ export async function getArgs(inputs: Inputs, toolkit: Toolkit): Promise> { const args: Array = ['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); @@ -61,7 +66,7 @@ async function getBakeArgs(inputs: Inputs, toolkit: Toolkit): Promise=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)) {