diff --git a/plugins/docker/.toolkitrc.yml b/plugins/docker/.toolkitrc.yml index 0a2ec5784..a8c614761 100644 --- a/plugins/docker/.toolkitrc.yml +++ b/plugins/docker/.toolkitrc.yml @@ -5,4 +5,9 @@ tasks: optionsSchema: './lib/schema' +options: + tasks: + DockerBuild: + buildArgs: + GIT_COMMIT: !toolkit/env 'GIT_COMMIT' version: 2 diff --git a/plugins/docker/readme.md b/plugins/docker/readme.md index 70bbf6e73..c09b07b92 100644 --- a/plugins/docker/readme.md +++ b/plugins/docker/readme.md @@ -24,9 +24,10 @@ plugins: Run `docker build` to create Docker images. #### Task options -| Property | Description | Type | Default | -| :------- | :------------------------------------------------------------------------------------------------------ | :-------- | :------ | -| `ssh` | whether to forward host's SSH agent, see https://docs.docker.com/reference/cli/docker/buildx/build/#ssh | `boolean` | `false` | +| Property | Description | Type | Default | +| :---------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :----------------------- | :------ | +| `ssh` | whether to forward host's SSH agent, see https://docs.docker.com/reference/cli/docker/buildx/build/#ssh | `boolean` | `false` | +| `buildArgs` | An object of Docker [build variables](https://docs.docker.com/build/building/variables/) to include when building the image. To use values from Tool Kit's environment (since we usually build Docker images on CircleCI, this would be the CI environment), you can use the `!toolkit/env` tag, e.g. `buildArgs: { GIT_COMMIT: !toolkit/env "GIT_COMMIT" }` | `Record` | `{}` | _All properties are optional._ diff --git a/plugins/docker/src/image-info.ts b/plugins/docker/src/image-info.ts index e0031cf35..ad16df5c1 100644 --- a/plugins/docker/src/image-info.ts +++ b/plugins/docker/src/image-info.ts @@ -32,10 +32,6 @@ export function generateImageLabels(systemCode: string): ImageLabels { } } -export function getCommitHash(ciState: CIState | null): string { - return ciState?.version || '' -} - export function getDeployTag(ciState: CIState | null): string { return ciState?.buildNumber ? `ci-${ciState.buildNumber}` diff --git a/plugins/docker/src/tasks/build.ts b/plugins/docker/src/tasks/build.ts index b4bd13fa1..69020c570 100644 --- a/plugins/docker/src/tasks/build.ts +++ b/plugins/docker/src/tasks/build.ts @@ -1,4 +1,4 @@ -import { buildImageName, generateImageLabels, getCommitHash, getDeployTag } from '../image-info' +import { buildImageName, generateImageLabels, getDeployTag } from '../image-info' import DockerSchema from '../schema' import * as z from 'zod' @@ -16,6 +16,12 @@ const DockerBuildSchema = z .default(false) .describe( "whether to forward host's SSH agent, see https://docs.docker.com/reference/cli/docker/buildx/build/#ssh" + ), + buildArgs: z + .record(z.string(), z.string()) + .default({}) + .describe( + 'An object of Docker [build variables](https://docs.docker.com/build/building/variables/) to include when building the image. To use values from Tool Kit\'s environment (since we usually build Docker images on CircleCI, this would be the CI environment), you can use the `!toolkit/env` tag, e.g. `buildArgs: { GIT_COMMIT: !toolkit/env "GIT_COMMIT" }`' ) }) .describe('Run `docker build` to create Docker images.') @@ -52,14 +58,15 @@ export default class DockerBuild extends Task<{ return ['--label', `${label}=${value}`] }) - const commitHash = getCommitHash(readState('ci')) + const buildArgs = Object.entries(this.options.buildArgs).flatMap(([key, value]) => { + return ['--build-arg', `${key}=${value}`] + }) const childBuild = spawn('docker', [ 'buildx', 'build', '--load', // Without this, the image is not stored and so we can't push it later - '--build-arg', - `GIT_COMMIT=${commitHash}`, + ...buildArgs, '--platform', imageOptions.platform, ...(this.options.ssh ? ['--ssh', 'default'] : []),