Skip to content

Commit

Permalink
Add push option for dry-run (#326)
Browse files Browse the repository at this point in the history
* Add `push` option for dry-run

* Fix up
  • Loading branch information
int128 committed Dec 18, 2023
1 parent 237d7cb commit 4cfef00
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 12 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/e2e-docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ jobs:
- run: yarn package

# run the action
- name: docker-manifest-create-action (dry-run)
uses: ./
with:
push: false
sources: |
${{ needs.build-linux-amd64.outputs.image-uri }}
${{ needs.build-linux-arm64.outputs.image-uri }}
- uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
with:
registry: ghcr.io
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/e2e-kaniko.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ jobs:
- run: yarn package

# run the action
- name: docker-manifest-create-action (dry-run)
uses: ./
with:
push: false
sources: |
${{ needs.build-linux-amd64.outputs.image-uri }}
- uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
with:
registry: ghcr.io
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,13 @@ This action requires Docker Buildx.

### Inputs

| Name | Default | Description |
| --------- | ---------- | ---------------------------------------------- |
| `tags` | (required) | Tags of destination images (multi-line string) |
| `sources` | (required) | Image URIs of sources (multi-line string) |
| Name | Default | Description |
| --------- | ---------------------------- | -------------------------------------------------- |
| `push` | `true` | Push the manifest to the registry |
| `tags` | (required if `push` is true) | Tags of the destination images (multi-line string) |
| `sources` | (required) | Image URIs of the sources (multi-line string) |

If `push` is false, this action runs `docker buildx imagetools create --dry-run`.

### Outputs

Expand Down
10 changes: 7 additions & 3 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ name: docker-manifest-create-action
description: Create a multi-architecture Docker image in GitHub Actions

inputs:
push:
description: Push the manifest to the registry
required: false
default: 'true'
tags:
description: Tags of destination images (multi-line string)
required: true
description: Tags of the destination images (multi-line string)
required: false
sources:
description: Image URIs of sources (multi-line string)
description: Image URIs of the sources (multi-line string)
required: true

outputs:
Expand Down
9 changes: 6 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { run } from './run'

const main = async (): Promise<void> => {
const outputs = await run({
tags: core.getMultilineInput('tags', { required: true }),
push: core.getBooleanInput('push', { required: true }),
tags: core.getMultilineInput('tags'),
sources: core.getMultilineInput('sources', { required: true }),
})
core.info(`Setting outputs: ${JSON.stringify(outputs, undefined, 2)}`)
core.setOutput('digest', outputs.digest)
if (outputs.digest) {
core.info(`Setting outputs.digest=${outputs.digest}`)
core.setOutput('digest', outputs.digest)
}
}

main().catch((e: Error) => {
Expand Down
15 changes: 13 additions & 2 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
import assert from 'assert'
import * as core from '@actions/core'
import * as exec from '@actions/exec'

type Inputs = {
push: boolean
tags: string[]
sources: string[]
}

type Outputs = {
digest: string
digest: string | undefined
}

export const run = async (inputs: Inputs): Promise<Outputs> => {
core.startGroup('Buildx version')
await exec.exec('docker', ['buildx', 'version'])
core.endGroup()

if (!inputs.push) {
await dryRunCreateManifest(inputs.sources)
return { digest: undefined }
}

assert(inputs.tags.length > 0, 'tags must be set')
for (const tag of inputs.tags) {
await createManifest(tag, inputs.sources)
}

const digest = await getDigest(inputs.tags[0])
return { digest }
}

const dryRunCreateManifest = async (sources: string[]) => {
await exec.exec('docker', ['buildx', 'imagetools', 'create', '--dry-run', ...sources])
}

const createManifest = async (destination: string, sources: string[]) => {
await exec.exec('docker', ['buildx', 'imagetools', 'create', '-t', destination, ...sources])
await exec.exec('docker', ['buildx', 'imagetools', 'inspect', destination])
Expand Down
22 changes: 22 additions & 0 deletions tests/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ it('should run docker buildx imagetools', async () => {
})

const outputs = await run({
push: true,
tags: ['ghcr.io/int128/docker-manifest-create-action:main'],
sources: [
'ghcr.io/int128/docker-manifest-create-action@sha256:0000000000000000000000000000000000000000000000000000000000000000',
Expand All @@ -39,3 +40,24 @@ it('should run docker buildx imagetools', async () => {
'ghcr.io/int128/docker-manifest-create-action:main',
])
})

it('should run docker buildx imagetools --dry-run if push is false', async () => {
const outputs = await run({
push: false,
tags: [],
sources: [
'ghcr.io/int128/docker-manifest-create-action@sha256:0000000000000000000000000000000000000000000000000000000000000000',
'ghcr.io/int128/docker-manifest-create-action@sha256:0000000000000000000000000000000000000000000000000000000000000001',
],
})
expect(outputs).toStrictEqual({ digest: undefined })

expect(exec.exec).toHaveBeenCalledWith('docker', [
'buildx',
'imagetools',
'create',
'--dry-run',
'ghcr.io/int128/docker-manifest-create-action@sha256:0000000000000000000000000000000000000000000000000000000000000000',
'ghcr.io/int128/docker-manifest-create-action@sha256:0000000000000000000000000000000000000000000000000000000000000001',
])
})

0 comments on commit 4cfef00

Please sign in to comment.