Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions plugins/docker/.toolkitrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ tasks:

optionsSchema: './lib/schema'

options:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! A few questions here:

  1. is buildArgs a plugin option or a task option?
  2. if someone were to specify custom buildArgs in their tool kit config, will it override this one or get merged with it?
  3. should we leave it to users eg storytelling team to implement this build arg in their toolkit config rather than making it a default here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if someone were to specify custom buildArgs in their tool kit config, will it override this one or get merged with it?

they'd override these options

should we leave it to users eg storytelling team to implement this build arg in their toolkit config rather than making it a default here?

removing GIT_COMMIT and asking teams to configure it themselves would be a breaking change for the docker plugin which would be good to avoid, that's why i suggested configuring it in the plugin.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they'd override these options

this is why I was wondering if it was better to not have any defaults that break unexpectedly when new buildArgs options are specified?

removing GIT_COMMIT and asking teams to configure it themselves would be a breaking change for the docker plugin which would be good to avoid, that's why i suggested configuring it in the plugin.

yeah that makes sense. we can have a deprecation notice period but we should eventually remove it when we have more stronger case for a breaking change. internally, i suspect only the storytelling team uses it at the moment

tasks:
DockerBuild:
buildArgs:
GIT_COMMIT: !toolkit/env 'GIT_COMMIT'
version: 2
7 changes: 4 additions & 3 deletions plugins/docker/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>` | `{}` |

_All properties are optional._

Expand Down
4 changes: 0 additions & 4 deletions plugins/docker/src/image-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
15 changes: 11 additions & 4 deletions plugins/docker/src/tasks/build.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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.')
Expand Down Expand Up @@ -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'] : []),
Expand Down