From a93ce764213a416335c86f005cd6e66d41a354a0 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sun, 8 Dec 2024 17:15:10 +0900 Subject: [PATCH 1/8] Support docker/bake-action --- .github/workflows/ts.yaml | 19 +++++++++++++------ action.yaml | 6 ++++++ src/bake.ts | 20 ++++++++++++++++++++ src/docker.ts | 12 ++++++------ src/github.ts | 3 +++ src/main.ts | 6 +++++- src/run.ts | 23 +++++++++++++++++++++-- tests/docker.test.ts | 29 ++++++++++++++--------------- tests/fixture/docker-bake.hcl | 8 ++++++++ 9 files changed, 96 insertions(+), 30 deletions(-) create mode 100644 src/bake.ts create mode 100644 tests/fixture/docker-bake.hcl diff --git a/.github/workflows/ts.yaml b/.github/workflows/ts.yaml index 80ce719e..5edbe05c 100644 --- a/.github/workflows/ts.yaml +++ b/.github/workflows/ts.yaml @@ -32,9 +32,10 @@ jobs: - run: pnpm test - run: pnpm build - # e2e test - - uses: ./ - id: cache-params + # E2E test + - name: Run int128/docker-build-cache-config-action + uses: ./ + id: cache with: image: ghcr.io/${{ github.repository }}/cache - uses: docker/metadata-action@369eb591f429131d6889c46b94e711f089e6ca96 # v5.6.1 @@ -48,14 +49,20 @@ jobs: password: ${{ secrets.GITHUB_TOKEN }} - uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1 - uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # v6.10.0 - id: build with: context: tests/fixture push: ${{ github.event_name == 'push' }} tags: ${{ steps.metadata.outputs.tags }} labels: ${{ steps.metadata.outputs.labels }} - cache-from: ${{ steps.cache-params.outputs.cache-from }} - cache-to: ${{ steps.cache-params.outputs.cache-to }} + cache-from: ${{ steps.cache.outputs.cache-from }} + cache-to: ${{ steps.cache.outputs.cache-to }} + - uses: docker/bake-action@3fc70e1131fee40a422dd8dd0ff22014ae20a1f3 # v5.11.0 + with: + push: ${{ github.event_name == 'push' }} + files: | + tests/fixture/docker-bake.hcl + ${{ steps.metadata.outputs.bake-file }} + ${{ steps.cache.outputs.bake-file }} generate: runs-on: ubuntu-latest diff --git a/action.yaml b/action.yaml index eef3d2df..008e5efd 100644 --- a/action.yaml +++ b/action.yaml @@ -28,6 +28,10 @@ inputs: cache-key-fallback: description: Custom cache key to fallback (experimental) required: false + bake-target: + description: Bake target name + required: false + default: docker-build-cache-config-action token: description: GitHub token required: false @@ -38,6 +42,8 @@ outputs: description: cache-from parameter for docker/build-push-action cache-to: description: cache-to parameter for docker/build-push-action + bake-file: + description: Bake definition file runs: using: 'node20' diff --git a/src/bake.ts b/src/bake.ts new file mode 100644 index 00000000..e57896cb --- /dev/null +++ b/src/bake.ts @@ -0,0 +1,20 @@ +import { DockerFlags } from './docker.js' + +type Bake = { + target: { + [target: string]: { + // https://docs.docker.com/build/bake/reference/#target + 'cache-from': string[] + 'cache-to': string[] + } + } +} + +export const generateBake = (target: string, flags: DockerFlags): Bake => ({ + target: { + [target]: { + 'cache-from': flags.cacheFrom, + 'cache-to': flags.cacheTo, + }, + }, +}) diff --git a/src/docker.ts b/src/docker.ts index 84a4c43e..19964bf1 100644 --- a/src/docker.ts +++ b/src/docker.ts @@ -8,12 +8,12 @@ type Inputs = { extraCacheTo: string } -type Outputs = { - cacheFrom: string - cacheTo: string +export type DockerFlags = { + cacheFrom: string[] + cacheTo: string[] } -export const generateDockerFlags = (inputs: Inputs): Outputs => { +export const generateDockerFlags = (inputs: Inputs): DockerFlags => { const cacheType = `type=${inputs.cacheType}` const cacheFrom = inputs.cacheFromImageTag.map((tag) => { @@ -34,7 +34,7 @@ export const generateDockerFlags = (inputs: Inputs): Outputs => { }) return { - cacheFrom: cacheFrom.join('\n'), - cacheTo: cacheTo.join('\n'), + cacheFrom, + cacheTo, } } diff --git a/src/github.ts b/src/github.ts index 6d2892e1..fce6ba92 100644 --- a/src/github.ts +++ b/src/github.ts @@ -1,6 +1,9 @@ import * as github from '@actions/github' +import * as os from 'os' export type Octokit = ReturnType // For testability, use a subset of github.context in this module. export type Context = Pick + +export const getRunnerTemp = (): string => process.env.RUNNER_TEMP || os.tmpdir() diff --git a/src/main.ts b/src/main.ts index c3694341..dc3dc36e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,12 +20,16 @@ const main = async (): Promise => { cacheKeyFallback: core.getMultilineInput('cache-key-fallback'), extraCacheFrom: core.getInput('extra-cache-from'), extraCacheTo: core.getInput('extra-cache-to'), + bakeTarget: core.getInput('bake-target', { required: true }), context: github.context, octokit: github.getOctokit(core.getInput('token', { required: true })), }) - core.info(`Setting outputs: ${JSON.stringify(outputs, undefined, 2)}`) + core.startGroup('Outputs') + core.info(JSON.stringify(outputs, undefined, 2)) + core.endGroup() core.setOutput('cache-from', outputs.cacheFrom) core.setOutput('cache-to', outputs.cacheTo) + core.setOutput('bake-file', outputs.bakeFile) } main().catch((e: Error) => { diff --git a/src/run.ts b/src/run.ts index 067136cb..923f65d9 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,7 +1,10 @@ import * as core from '@actions/core' -import { Context, Octokit } from './github.js' +import * as fs from 'fs/promises' +import * as path from 'path' +import { Context, getRunnerTemp, Octokit } from './github.js' import { inferImageTags } from './infer.js' import { CacheType, generateDockerFlags } from './docker.js' +import { generateBake } from './bake.js' type Inputs = { image: string @@ -12,6 +15,7 @@ type Inputs = { cacheKeyFallback: string[] extraCacheFrom: string extraCacheTo: string + bakeTarget: string context: Context octokit: Octokit } @@ -19,6 +23,7 @@ type Inputs = { type Outputs = { cacheFrom: string cacheTo: string + bakeFile: string } export const run = async (inputs: Inputs): Promise => { @@ -31,11 +36,25 @@ export const run = async (inputs: Inputs): Promise => { }) core.info(`Inferred cache-from: ${tags.from.join(', ')}`) core.info(`Inferred cache-to: ${tags.to.join(', ')}`) - return generateDockerFlags({ + const dockerFlags = generateDockerFlags({ cacheType: inputs.cacheType, cacheFromImageTag: tags.from, cacheToImageTag: tags.to, extraCacheFrom: inputs.extraCacheFrom, extraCacheTo: inputs.extraCacheTo, }) + + const bake = generateBake(inputs.bakeTarget, dockerFlags) + core.startGroup('Bake file definition') + core.info(JSON.stringify(bake, undefined, 2)) + core.endGroup() + + const tempDir = await fs.mkdtemp(path.join(getRunnerTemp(), 'docker-build-cache-config-action-')) + const bakeFile = `${tempDir}/docker-build-cache-config-action-bake.json` + await fs.writeFile(bakeFile, JSON.stringify(bake)) + return { + cacheFrom: dockerFlags.cacheFrom.join('\n'), + cacheTo: dockerFlags.cacheTo.join('\n'), + bakeFile, + } } diff --git a/tests/docker.test.ts b/tests/docker.test.ts index 11b6cf57..7b2d41a0 100644 --- a/tests/docker.test.ts +++ b/tests/docker.test.ts @@ -9,8 +9,8 @@ test('both from and to', () => { extraCacheTo: '', }) expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main', - cacheTo: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main,mode=max', + cacheFrom: ['type=registry,ref=ghcr.io/int128/sandbox/cache:main'], + cacheTo: ['type=registry,ref=ghcr.io/int128/sandbox/cache:main,mode=max'], }) }) @@ -23,8 +23,8 @@ test('only from', () => { extraCacheTo: '', }) expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main', - cacheTo: '', + cacheFrom: ['type=registry,ref=ghcr.io/int128/sandbox/cache:main'], + cacheTo: [], }) }) @@ -37,8 +37,8 @@ test('both from and to with extra args', () => { extraCacheTo: 'image-manifest=true', }) expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main,foo=bar', - cacheTo: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main,mode=max,image-manifest=true', + cacheFrom: ['type=registry,ref=ghcr.io/int128/sandbox/cache:main,foo=bar'], + cacheTo: ['type=registry,ref=ghcr.io/int128/sandbox/cache:main,mode=max,image-manifest=true'], }) }) @@ -51,8 +51,8 @@ test('only from with extra args', () => { extraCacheTo: 'image-manifest=true', }) expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main,foo=bar', - cacheTo: '', + cacheFrom: ['type=registry,ref=ghcr.io/int128/sandbox/cache:main,foo=bar'], + cacheTo: [], }) }) @@ -65,11 +65,11 @@ test('both multiple from and to', () => { extraCacheTo: '', }) expect(outputs).toStrictEqual({ - cacheFrom: - 'type=registry,ref=ghcr.io/int128/sandbox/cache:pr-1' + - '\n' + + cacheFrom: [ + 'type=registry,ref=ghcr.io/int128/sandbox/cache:pr-1', 'type=registry,ref=ghcr.io/int128/sandbox/cache:main', - cacheTo: 'type=registry,ref=ghcr.io/int128/sandbox/cache:pr-1,mode=max', + ], + cacheTo: ['type=registry,ref=ghcr.io/int128/sandbox/cache:pr-1,mode=max'], }) }) @@ -82,8 +82,7 @@ test('cache type', () => { extraCacheTo: '', }) expect(outputs).toStrictEqual({ - cacheFrom: - 'type=gha,ref=ghcr.io/int128/sandbox/cache:pr-1' + '\n' + 'type=gha,ref=ghcr.io/int128/sandbox/cache:main', - cacheTo: 'type=gha,ref=ghcr.io/int128/sandbox/cache:pr-1,mode=max', + cacheFrom: ['type=gha,ref=ghcr.io/int128/sandbox/cache:pr-1', 'type=gha,ref=ghcr.io/int128/sandbox/cache:main'], + cacheTo: ['type=gha,ref=ghcr.io/int128/sandbox/cache:pr-1,mode=max'], }) }) diff --git a/tests/fixture/docker-bake.hcl b/tests/fixture/docker-bake.hcl new file mode 100644 index 00000000..57bdf8b6 --- /dev/null +++ b/tests/fixture/docker-bake.hcl @@ -0,0 +1,8 @@ +target "docker-metadata-action" {} + +target "docker-build-cache-config-action" {} + +target "default" { + inherits = ["docker-metadata-action", "docker-build-cache-config-action"] + context = "." +} From 25c0ec0dd02e9ff3fc4a102cffe5df89bd056410 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sun, 8 Dec 2024 17:23:12 +0900 Subject: [PATCH 2/8] Fix test --- tests/run.test.ts | 77 +++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/tests/run.test.ts b/tests/run.test.ts index 9c11cc22..444c3ac3 100644 --- a/tests/run.test.ts +++ b/tests/run.test.ts @@ -23,12 +23,11 @@ describe('Basic usage', () => { repo: { owner: 'int128', repo: 'sandbox' }, issue: { owner: 'int128', repo: 'sandbox', number: 0 }, }, + bakeTarget: 'docker-build-cache-config-action', octokit: getOctokit(), }) - expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main', - cacheTo: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main,mode=max', - }) + expect(outputs.cacheFrom).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:main') + expect(outputs.cacheTo).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:main,mode=max') }) test('pull_request event', async () => { @@ -56,12 +55,11 @@ describe('Basic usage', () => { repo: { owner: 'int128', repo: 'sandbox' }, issue: { owner: 'int128', repo: 'sandbox', number: 1 }, }, + bakeTarget: 'docker-build-cache-config-action', octokit: getOctokit(), }) - expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main', - cacheTo: '', - }) + expect(outputs.cacheFrom).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:main') + expect(outputs.cacheTo).toBe('') }) test('issue_comment event', async () => { @@ -88,12 +86,11 @@ describe('Basic usage', () => { repo: { owner: 'int128', repo: 'sandbox' }, issue: { owner: 'int128', repo: 'sandbox', number: 1 }, }, + bakeTarget: 'docker-build-cache-config-action', octokit: getOctokit(), }) - expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main', - cacheTo: '', - }) + expect(outputs.cacheFrom).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:main') + expect(outputs.cacheTo).toBe('') }) test('schedule event', async () => { @@ -113,12 +110,11 @@ describe('Basic usage', () => { repo: { owner: 'int128', repo: 'sandbox' }, issue: { owner: 'int128', repo: 'sandbox', number: 0 }, }, + bakeTarget: 'docker-build-cache-config-action', octokit: getOctokit(), }) - expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main', - cacheTo: '', - }) + expect(outputs.cacheFrom).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:main') + expect(outputs.cacheTo).toBe('') }) }) @@ -148,14 +144,13 @@ describe('Import and export a pull request cache', () => { repo: { owner: 'int128', repo: 'sandbox' }, issue: { owner: 'int128', repo: 'sandbox', number: 1 }, }, + bakeTarget: 'docker-build-cache-config-action', octokit: getOctokit(), }) - expect(outputs).toStrictEqual({ - cacheFrom: `\ + expect(outputs.cacheFrom).toBe(`\ type=registry,ref=ghcr.io/int128/sandbox/cache:pr-1 -type=registry,ref=ghcr.io/int128/sandbox/cache:main`, - cacheTo: 'type=registry,ref=ghcr.io/int128/sandbox/cache:pr-1,mode=max', - }) +type=registry,ref=ghcr.io/int128/sandbox/cache:main`) + expect(outputs.cacheTo).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:pr-1,mode=max') }) }) @@ -177,12 +172,11 @@ describe('Build multi-architecture images', () => { repo: { owner: 'int128', repo: 'sandbox' }, issue: { owner: 'int128', repo: 'sandbox', number: 0 }, }, + bakeTarget: 'docker-build-cache-config-action', octokit: getOctokit(), }) - expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main-arm64', - cacheTo: 'type=registry,ref=ghcr.io/int128/sandbox/cache:main-arm64,mode=max', - }) + expect(outputs.cacheFrom).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:main-arm64') + expect(outputs.cacheTo).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:main-arm64,mode=max') }) }) @@ -204,13 +198,15 @@ describe('For Amazon ECR', () => { repo: { owner: 'int128', repo: 'sandbox' }, issue: { owner: 'int128', repo: 'sandbox', number: 0 }, }, + bakeTarget: 'docker-build-cache-config-action', octokit: getOctokit(), }) - expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=123456789012.dkr.ecr.us-west-2.amazonaws.com/int128/sandbox:main-cache', - cacheTo: - 'type=registry,ref=123456789012.dkr.ecr.us-west-2.amazonaws.com/int128/sandbox:main-cache,mode=max,image-manifest=true', - }) + expect(outputs.cacheFrom).toBe( + 'type=registry,ref=123456789012.dkr.ecr.us-west-2.amazonaws.com/int128/sandbox:main-cache', + ) + expect(outputs.cacheTo).toBe( + 'type=registry,ref=123456789012.dkr.ecr.us-west-2.amazonaws.com/int128/sandbox:main-cache,mode=max,image-manifest=true', + ) }) }) @@ -232,12 +228,11 @@ describe('Build multiple image tags from a branch', () => { repo: { owner: 'int128', repo: 'sandbox' }, issue: { owner: 'int128', repo: 'sandbox', number: 0 }, }, + bakeTarget: 'docker-build-cache-config-action', octokit: getOctokit(), }) - expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:staging', - cacheTo: 'type=registry,ref=ghcr.io/int128/sandbox/cache:staging,mode=max', - }) + expect(outputs.cacheFrom).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:staging') + expect(outputs.cacheTo).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:staging,mode=max') }) test('pull_request event', async () => { @@ -265,12 +260,11 @@ describe('Build multiple image tags from a branch', () => { repo: { owner: 'int128', repo: 'sandbox' }, issue: { owner: 'int128', repo: 'sandbox', number: 1 }, }, + bakeTarget: 'docker-build-cache-config-action', octokit: getOctokit(), }) - expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:development', - cacheTo: '', - }) + expect(outputs.cacheFrom).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:development') + expect(outputs.cacheTo).toBe('') }) test('schedule event', async () => { @@ -290,11 +284,10 @@ describe('Build multiple image tags from a branch', () => { repo: { owner: 'int128', repo: 'sandbox' }, issue: { owner: 'int128', repo: 'sandbox', number: 0 }, }, + bakeTarget: 'docker-build-cache-config-action', octokit: getOctokit(), }) - expect(outputs).toStrictEqual({ - cacheFrom: 'type=registry,ref=ghcr.io/int128/sandbox/cache:development', - cacheTo: '', - }) + expect(outputs.cacheFrom).toBe('type=registry,ref=ghcr.io/int128/sandbox/cache:development') + expect(outputs.cacheTo).toBe('') }) }) From 0094b765ce2cce48a17d8720c3ff9e6866b34d69 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sun, 8 Dec 2024 17:24:59 +0900 Subject: [PATCH 3/8] Fix --- tests/fixture/docker-bake.hcl | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/fixture/docker-bake.hcl b/tests/fixture/docker-bake.hcl index 57bdf8b6..40772778 100644 --- a/tests/fixture/docker-bake.hcl +++ b/tests/fixture/docker-bake.hcl @@ -5,4 +5,5 @@ target "docker-build-cache-config-action" {} target "default" { inherits = ["docker-metadata-action", "docker-build-cache-config-action"] context = "." + dockerfile = "Dockerfile" } From ebb39e87e8e492eb7c9f508b393fccc9578e02c9 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sun, 8 Dec 2024 17:26:21 +0900 Subject: [PATCH 4/8] Fix --- tests/fixture/docker-bake.hcl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixture/docker-bake.hcl b/tests/fixture/docker-bake.hcl index 40772778..db75ff96 100644 --- a/tests/fixture/docker-bake.hcl +++ b/tests/fixture/docker-bake.hcl @@ -5,5 +5,5 @@ target "docker-build-cache-config-action" {} target "default" { inherits = ["docker-metadata-action", "docker-build-cache-config-action"] context = "." - dockerfile = "Dockerfile" + dockerfile = "./Dockerfile" } From 3ad5da02c5d0ba28e3fcda93f8fc0501e1b3176e Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sun, 8 Dec 2024 17:30:16 +0900 Subject: [PATCH 5/8] Fix --- .github/workflows/ts.yaml | 3 ++- src/main.ts | 4 +--- tests/fixture/docker-bake.hcl | 1 - 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ts.yaml b/.github/workflows/ts.yaml index 5edbe05c..c2b4e7ca 100644 --- a/.github/workflows/ts.yaml +++ b/.github/workflows/ts.yaml @@ -58,9 +58,10 @@ jobs: cache-to: ${{ steps.cache.outputs.cache-to }} - uses: docker/bake-action@3fc70e1131fee40a422dd8dd0ff22014ae20a1f3 # v5.11.0 with: + workdir: tests/fixture push: ${{ github.event_name == 'push' }} files: | - tests/fixture/docker-bake.hcl + docker-bake.hcl ${{ steps.metadata.outputs.bake-file }} ${{ steps.cache.outputs.bake-file }} diff --git a/src/main.ts b/src/main.ts index dc3dc36e..71c94e21 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,9 +24,7 @@ const main = async (): Promise => { context: github.context, octokit: github.getOctokit(core.getInput('token', { required: true })), }) - core.startGroup('Outputs') - core.info(JSON.stringify(outputs, undefined, 2)) - core.endGroup() + core.info(`Setting outputs: ${JSON.stringify(outputs, undefined, 2)}`) core.setOutput('cache-from', outputs.cacheFrom) core.setOutput('cache-to', outputs.cacheTo) core.setOutput('bake-file', outputs.bakeFile) diff --git a/tests/fixture/docker-bake.hcl b/tests/fixture/docker-bake.hcl index db75ff96..57bdf8b6 100644 --- a/tests/fixture/docker-bake.hcl +++ b/tests/fixture/docker-bake.hcl @@ -5,5 +5,4 @@ target "docker-build-cache-config-action" {} target "default" { inherits = ["docker-metadata-action", "docker-build-cache-config-action"] context = "." - dockerfile = "./Dockerfile" } From 698a7b5d97fc653e8cd03d6c2083470b0b9d6c9c Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sun, 8 Dec 2024 17:44:12 +0900 Subject: [PATCH 6/8] doc --- .github/workflows/ts.yaml | 2 +- README.md | 49 ++++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ts.yaml b/.github/workflows/ts.yaml index c2b4e7ca..3d07363e 100644 --- a/.github/workflows/ts.yaml +++ b/.github/workflows/ts.yaml @@ -61,7 +61,7 @@ jobs: workdir: tests/fixture push: ${{ github.event_name == 'push' }} files: | - docker-bake.hcl + ./docker-bake.hcl ${{ steps.metadata.outputs.bake-file }} ${{ steps.cache.outputs.bake-file }} diff --git a/README.md b/README.md index c3e62955..ce6ebe62 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,9 @@ This action generates the cache parameters by this strategy. ## Examples -Here is an example to store a cache into GHCR (GitHub Container Registry). +### Build with docker/build-push-action + +Here is an example to build a container image with [docker/build-push-action](https://github.com/docker/build-push-action). ```yaml - uses: docker/metadata-action@v3 @@ -110,20 +112,44 @@ ghcr.io/${{ github.repository }}/cache:main See [README_EXAMPLES.md](README_EXAMPLES.md) for more examples. +### Build with docker/bake-action + +Here is an example to build a container image with [docker/bake-action](https://github.com/docker/bake-action). + +```yaml +- uses: docker/metadata-action@v3 + id: metadata + with: + images: ghcr.io/${{ github.repository }} +- uses: int128/docker-build-cache-config-action@v1 + id: cache + with: + image: ghcr.io/${{ github.repository }}/cache +- uses: docker/bake-action@v5 + id: build + with: + push: true + files: | + ./docker-bake.hcl + ${{ steps.metadata.outputs.bake-file }} + ${{ steps.cache.outputs.bake-file }} +``` + ## Specification ### Inputs -| Name | Default | Description | -| -------------------- | ---------- | ---------------------------------------------------------------------------------------------- | -| `image` | (required) | Image repository to import/export cache | -| `cache-type` | `registry` | Type of cache backend (for source and destination). Can be registry, local, inline, gha and s3 | -| `flavor` | - | Flavor (multiline string) | -| `extra-cache-from` | - | Extra flag to `cache-from` | -| `extra-cache-to` | - | Extra flag to `cache-to` | -| `pull-request-cache` | - | Import and export a pull request cache | -| `cache-key` | - | Custom cache key | -| `cache-key-fallback` | - | Custom cache key to fallback | +| Name | Default | Description | +| -------------------- | ---------------------------------- | ---------------------------------------------------------------------------------------------- | +| `image` | (required) | Image repository to import/export cache | +| `cache-type` | `registry` | Type of cache backend (for source and destination). Can be registry, local, inline, gha and s3 | +| `flavor` | - | Flavor (multiline string) | +| `extra-cache-from` | - | Extra flag to `cache-from` | +| `extra-cache-to` | - | Extra flag to `cache-to` | +| `pull-request-cache` | - | Import and export a pull request cache | +| `cache-key` | - | Custom cache key | +| `cache-key-fallback` | - | Custom cache key to fallback | +| `bake-target` | `docker-build-cache-config-action` | Bake target name | `flavor` is mostly compatible with [docker/metadata-action](https://github.com/docker/metadata-action#flavor-input) except this action supports only `prefix` and `suffix`. @@ -139,6 +165,7 @@ The specification may change in the future. | ------------ | -------------------------------------- | | `cache-from` | Parameter for docker/build-push-action | | `cache-to` | Parameter for docker/build-push-action | +| `bake-file` | Bake definition file | ### Events From 5dfede1fae14191e59716cd3451365dd695f1938 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sun, 8 Dec 2024 17:56:15 +0900 Subject: [PATCH 7/8] doc --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index ce6ebe62..6396168c 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,17 @@ Here is an example to build a container image with [docker/bake-action](https:// ${{ steps.cache.outputs.bake-file }} ``` +```hcl +target "docker-metadata-action" {} + +target "docker-build-cache-config-action" {} + +target "default" { + inherits = ["docker-metadata-action", "docker-build-cache-config-action"] + context = "." +} +``` + ## Specification ### Inputs From 2714b3fe82e8db314f5d06f9bb71bdb61e05fc0c Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sun, 8 Dec 2024 17:57:01 +0900 Subject: [PATCH 8/8] doc --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6396168c..6dbe357e 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,7 @@ Here is an example to build a container image with [docker/bake-action](https:// ``` ```hcl +# docker-bake.hcl target "docker-metadata-action" {} target "docker-build-cache-config-action" {}