From 0d7f78d53b85e8f2e65e8e5a3594425049239b86 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Wed, 3 Jul 2024 11:42:53 +0200 Subject: [PATCH] cache-image input to enable/disable caching of binfmt image Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- .github/workflows/ci.yml | 23 +++++++++++++++++++++++ README.md | 9 +++++---- __tests__/context.test.ts | 9 ++++++++- action.yml | 5 +++++ src/context.ts | 4 +++- src/main.ts | 8 +------- 6 files changed, 45 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ab52433..f0a5f5e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -79,3 +79,26 @@ jobs: echo "::error::Should have failed" exit 1 fi + + cache-image: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + cache: + - true + - false + steps: + - + name: Checkout + uses: actions/checkout@v4 + - + name: Set up QEMU + id: qemu + uses: ./ + with: + image: tonistiigi/binfmt:master + cache-image: ${{ matrix.cache }} + - + name: Available platforms + run: echo ${{ steps.qemu.outputs.platforms }} diff --git a/README.md b/README.md index ba1224f..5adf3ff 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,11 @@ jobs: The following inputs can be used as `step.with` keys: -| Name | Type | Default | Description | -|-------------|--------|-------------------------------------------------------------------------------|--------------------------------------------------| -| `image` | String | [`tonistiigi/binfmt:latest`](https://hub.docker.com/r/tonistiigi/binfmt/tags) | QEMU static binaries Docker image | -| `platforms` | String | `all` | Platforms to install (e.g., `arm64,riscv64,arm`) | +| Name | Type | Default | Description | +|---------------|--------|-------------------------------------------------------------------------------|----------------------------------------------------| +| `image` | String | [`tonistiigi/binfmt:latest`](https://hub.docker.com/r/tonistiigi/binfmt/tags) | QEMU static binaries Docker image | +| `platforms` | String | `all` | Platforms to install (e.g., `arm64,riscv64,arm`) | +| `cache-image` | Bool | `true` | Cache binfmt image to GitHub Actions cache backend | ### outputs diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 9e2f630..68da145 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -16,10 +16,13 @@ describe('getInputs', () => { test.each([ [ 0, - new Map([]), + new Map([ + ['cache-image', 'true'], + ]), { image: 'docker.io/tonistiigi/binfmt:latest', platforms: 'all', + cacheImage: true, } as context.Inputs ], [ @@ -27,20 +30,24 @@ describe('getInputs', () => { new Map([ ['image', 'docker/binfmt:latest'], ['platforms', 'arm64,riscv64,arm'], + ['cache-image', 'false'], ]), { image: 'docker/binfmt:latest', platforms: 'arm64,riscv64,arm', + cacheImage: false, } as context.Inputs ], [ 2, new Map([ ['platforms', 'arm64, riscv64, arm '], + ['cache-image', 'true'], ]), { image: 'docker.io/tonistiigi/binfmt:latest', platforms: 'arm64,riscv64,arm', + cacheImage: true, } as context.Inputs ] ])( diff --git a/action.yml b/action.yml index f347310..6135f30 100644 --- a/action.yml +++ b/action.yml @@ -15,6 +15,10 @@ inputs: description: 'Platforms to install (e.g. arm64,riscv64,arm)' default: 'all' required: false + cache-image: + description: 'Cache binfmt image to GitHub Actions cache backend' + default: 'true' + required: false outputs: platforms: @@ -23,3 +27,4 @@ outputs: runs: using: 'node20' main: 'dist/index.js' + post: 'dist/index.js' diff --git a/src/context.ts b/src/context.ts index dc63c3d..46b4d14 100644 --- a/src/context.ts +++ b/src/context.ts @@ -4,11 +4,13 @@ import {Util} from '@docker/actions-toolkit/lib/util'; export interface Inputs { image: string; platforms: string; + cacheImage: boolean; } export function getInputs(): Inputs { return { image: core.getInput('image') || 'docker.io/tonistiigi/binfmt:latest', - platforms: Util.getInputList('platforms').join(',') || 'all' + platforms: Util.getInputList('platforms').join(',') || 'all', + cacheImage: core.getBooleanInput('cache-image') }; } diff --git a/src/main.ts b/src/main.ts index 2bc30fe..9d1386c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,13 +20,7 @@ actionsToolkit.run( }); await core.group(`Pulling binfmt Docker image`, async () => { - await Docker.getExecOutput(['pull', input.image], { - ignoreReturnCode: true - }).then(res => { - if (res.stderr.length > 0 && res.exitCode != 0) { - throw new Error(res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'); - } - }); + await Docker.pull(input.image, input.cacheImage); }); await core.group(`Image info`, async () => {