diff --git a/.github/prompts/update-project.prompt.md b/.github/prompts/update-project.prompt.md index 2c51420..035ffcd 100644 --- a/.github/prompts/update-project.prompt.md +++ b/.github/prompts/update-project.prompt.md @@ -32,21 +32,14 @@ Always branch from latest `origin/main`. ## Phase 2 --- GitHub Actions Updates -1. Scan the following locations for pinned `uses:` action references: - - `.github/workflows/*.yml` - - `.github/actions/**/*.yml` - - `*/action.yml` (composite actions in subdirectories at the repository root) -2. For each external action (e.g. `actions/checkout@v6`, `docker/login-action@v4`), use the GitHub Releases API to find -the latest release tag. Check every action individually — do not assume that a major-version tag (e.g. `@v6`) is already -current. A newer major version may exist. - - If the reference uses a floating major-version tag (e.g. `@v6`) and that major version is already the latest, - leave it as-is — no update is needed. - - If the reference is pinned to a specific semver tag (e.g. `@v2.5.0`, `@v22.0.0`) and a newer version exists, - update it to the latest release tag. - - If the reference is pinned to a commit SHA, leave it as-is — do not convert SHA pins to tags. -3. Skip self-referencing actions (actions that reference the current repository, e.g. -`specsnl/github-actions/build-image@1.0.12`) — they are handled in Phase 4. -4. Update any outdated action versions in-place across all scanned files. +1. Scan `.github/workflows/*.yml` for `uses:` action references. They are pinned by commit SHA with the version in a +comment, e.g. `actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1`. +2. For each external action, use the GitHub Releases API to find the latest release tag. Check every action +individually — a newer major version may exist. +3. If a newer release exists, replace the SHA with the commit SHA of that release's tag and update the version comment. +Use the same SHA and comment everywhere the action is used. +4. Check the `rhysd/actionlint` and `zizmorcore/zizmor` versions too: update the version (and the image digest) in +`.github/workflows/lint.yml` and the matching variables in `Taskfile.dist.yml` together. 5. Check `git status` for all changed files. Commit separately if there are any changes. Commit message: `chore(deps): Update GitHub Actions versions`. 6. If there are no changes, skip the commit step and move on to the next phase. @@ -64,34 +57,15 @@ Abort on any failure. Commit message: `chore: Update copyright year`. 5. If there are no changes, skip the commit step. -## Phase 4 --- Self-referencing Version Bump - -1. Determine the current latest release tag of this repository using the GitHub Releases API (e.g., `1.0.12`). -2. Ask the user whether to bump the **patch** version (e.g., `1.0.12` → `1.0.13`) or the **minor** version -(e.g., `1.0.12` → `1.1.0`). -3. Compute the new version tag based on the user's choice. -4. Update all self-referencing action references across all scanned files to use the new version tag. - - Files to scan: same as Phase 2 (`.github/workflows/*.yml`, `.github/actions/**/*.yml`, - `*/action.yml`) - - Self-referencing actions are those that reference the current repository - (e.g., `specsnl/github-actions/*@`) - - Update every occurrence across all files before committing — all changes go into a single commit. -5. If any files were changed, stage them all and create one commit. - Commit message: `chore: Bump self-referencing actions to `. -6. If there are no changes, skip the commit step. - -Abort on any failure. - ------------------------------------------------------------------------ ## Final State Abort if there are no changes after all update steps, and inform the user that everything is already up to date. -- There could be up to 3 commits on the `vendor-updates` branch: +- There could be up to 2 commits on the `vendor-updates` branch: 1. GitHub Actions version updates 2. Copyright year update - 3. Self-referencing version bump - Branch: `vendor-updates` - Based on latest `origin/main`. - The working tree should be clean. @@ -106,9 +80,8 @@ Updated the following dependencies: - Updated GitHub Actions versions - Updated copyright year -- Bumped self-referencing actions to ## Post-merge tasks -- [ ] Create and push git tag `` +- [ ] Create and push the next patch tag ``` diff --git a/README.md b/README.md index a62f057..96a3ab4 100644 --- a/README.md +++ b/README.md @@ -1,40 +1,35 @@ -# Github Actions Workflows and Composite Actions +# Github Actions Workflows -This repository contains the Specsnl organisation collection of GitHub Actions workflows and composite actions that can -be reused to automate various tasks in GitHub repositories. +This repository contains the Specsnl organisation collection of reusable GitHub Actions workflows. -Consumers pin by tag, i.e. `specsnl/github-actions/.github/workflows/build.yml@3.1.0`. +Consumers pin by tag, i.e. `specsnl/github-actions/.github/workflows/bake.yml@4.0.0`. ## What is in here -| Kind | Name | Purpose | -|-------------------|------------------------|------------------------------------------------------------------------------| -| Composite action | `build-image` | Build one platform of an image and push it by digest, or load it for testing | -| Composite action | `create-manifest` | Merge the per-platform digests into one multi-arch manifest and tag it | -| Reusable workflow | `build.yml` | `build-image` plus the checkout / login / buildx boilerplate around it | -| Reusable workflow | `merge.yml` | `create-manifest` plus the same boilerplate | -| Reusable workflow | `build-php.yml` | Builds the PHP stages (`runtime`, `builder`) via `build.yml` | -| Reusable workflow | `merge-php.yml` | Merges those same stages via `merge.yml` | -| Reusable workflow | `build-go-cli.yml` | Builds a Go CLI image via `build.yml`, injecting the version as a build arg | -| Reusable workflow | `merge-go-cli.yml` | Merges it via `merge.yml` with the semver tag set a CLI release wants | -| Reusable workflow | `notify-slack-tag.yml` | Posts a Slack message when a tag is created | - -The language-specific workflows are thin shims: they hold only the part that is actually specific to that language and -delegate everything else to the generic pair. +| Kind | Name | Purpose | +|-------------------|------------------------|--------------------------------------------------------------------------| +| Reusable workflow | `bake.yml` | Build and publish every image a repository's `docker-bake.hcl` describes | +| Bake file | `bake/specs.hcl` | The shared tag, label and annotation rules, supplied by `bake.yml` | +| Reusable workflow | `notify-slack-tag.yml` | Posts a Slack message when a tag is created | + +What to build — Dockerfiles, stages, image names, platforms, build args — lives in the consuming repository's +`docker-bake.hcl`. This repository only provides how it is built and published in CI, so +`docker buildx bake --print` on a laptop shows exactly what CI will publish. ## Documentation -- [How the image pipeline works](docs/pipeline.md) — the two phases, digests, tags and what dependabot gets -- [Go CLI images](docs/go-cli.md) — version injection, the tag set, publishing variants -- [PHP images](docs/php.md) — which stages are published and under what name -- [Base images](docs/base-images.md) — why these workflows take a Dockerfile target and not a base image -- [Testing an image in a pull request](docs/testing-images.md) — building without pushing and running the result +- [Building images with bake](docs/bake.md) — the consumer's `docker-bake.hcl`, `specs.hcl`, and complete examples +- [How the image pipeline works](docs/pipeline.md) — plan, build, publish, status, and the frozen definition +- [Testing an image in a pull request](docs/testing-images.md) — building, loading and running a target +- [Base images](docs/base-images.md) — why the consumer owns its Dockerfile stages - [Reusable workflow reference](docs/workflows.md) — every input, per workflow -- [Composite action reference](docs/actions.md) — every input and output, per action +- [Migrating from 3.x to 4.0.0](docs/migration-4.0.md) — what changed and how to move a repository over - [Releasing this repository](docs/releasing.md) — cutting a tag consumers can pin -## Linting +## Development ```bash -task lint +task lint # actionlint and shellcheck +task zizmor # zizmor, online audits when GH_TOKEN is set +task test # specs.hcl fixture tests and the plan tests ``` diff --git a/docs/actions.md b/docs/actions.md deleted file mode 100644 index 3ba1ebe..0000000 --- a/docs/actions.md +++ /dev/null @@ -1,66 +0,0 @@ -# Composite action reference - -The two composite actions carry the actual work; the [reusable workflows](workflows.md) are the boilerplate around -them. Call a workflow unless you need the image in the same job — which is the case when -[testing an image in a pull request](testing-images.md). - -Both assume the job has already checked out, logged in to the registry and set up buildx. - -## `build-image` - -```yaml -- uses: specsnl/github-actions/build-image@3.1.0 -``` - -| Input | Default | Description | -|---------------|------------|----------------------------------------------------------------------------------------| -| `platform` | *required* | Platform, i.e. `linux/amd64` | -| `image-name` | *required* | Image name; must already be lowercase | -| `dockerfile` | *required* | Dockerfile path | -| `context` | `.` | Build context path | -| `target` | — | Dockerfile stage to build; omit for the default (last) stage | -| `title` | — | Overrides `org.opencontainers.image.title` | -| `description` | — | Overrides `org.opencontainers.image.description` | -| `secrets` | — | Secrets to expose to the build (multiline) | -| `build-args` | — | Build-time variables (multiline `KEY=value`) | -| `push` | — | `"true"` / `"false"`; defaults to pushing, except for dependabot and when `load` is on | -| `load` | `"false"` | Load the image into the local Docker daemon instead of pushing it by digest | -| `raw-tag` | `latest` | Tag used on `workflow_dispatch` runs, and as the local tag when `load` is on | -| `version` | — | Sets `org.opencontainers.image.version`; defaults to the version derived from the ref | - -| Output | Description | -|-----------|---------------------------------------------------------------------------------------------------| -| `image` | Reference of the loaded image, i.e. `ghcr.io/specsnl/specs-cli:latest`. Empty unless `load` is on | -| `imageid` | ID of the built image | -| `digest` | Digest of the built image | - -Note that `image-name` is not lowercased here — `build.yml` does that before calling. A `GHCR` path with an uppercase -character in it will be rejected by the registry. - -`push` and `load` cannot both be true; the action fails with an explicit error rather than letting buildx complain -about conflicting outputs. With `load` on, the digest export and artifact upload are skipped. - -The action derives tags with the same default directives as `create-manifest`, only to get -`org.opencontainers.image.version` for the labels and annotations; nothing is pushed under them. With `load` on, the -image is tagged only with the `image` output. If the matching `create-manifest` uses `raw-tags` or `flavor`, pass the -same `version` to both, or the per-platform images and the index can disagree. - -## `create-manifest` - -```yaml -- uses: specsnl/github-actions/create-manifest@3.1.0 -``` - -| Input | Default | Description | -|---------------|------------|--------------------------------------------------------------------------------------------| -| `image-name` | *required* | Image name. Multiline tags one manifest under several names; the first line is the primary | -| `target` | — | The target the digests were built from; must match the build | -| `title` | — | Overrides `org.opencontainers.image.title` | -| `description` | — | Overrides `org.opencontainers.image.description` | -| `version` | — | Sets `org.opencontainers.image.version` | -| `raw-tag` | `latest` | Tag used on `workflow_dispatch` runs | -| `raw-tags` | — | Extra `metadata-action` tag directives (multiline), appended to the defaults | -| `flavor` | — | `metadata-action` flavor directives (multiline), i.e. `latest=false` | - -The action downloads the digest artifacts itself, so it has to run in a job that comes after every build job. See -[How the image pipeline works](pipeline.md) for the artifact naming and the default tag set. diff --git a/docs/bake.md b/docs/bake.md new file mode 100644 index 0000000..7da9cde --- /dev/null +++ b/docs/bake.md @@ -0,0 +1,280 @@ +# Building images with bake + +A repository publishes its images with one call to [`bake.yml`](workflows.md#bakeyml). What to build lives in the +repository itself, in two bake files: + +| File | Owner | Contents | +|-------------------|--------------------------|--------------------------------------------------------------| +| `specs.hcl` | This repository | The tag, label and annotation rules, supplied by `bake.yml` | +| `docker-bake.hcl` | The consuming repository | The targets: Dockerfile, stage, platforms, args, image names | + +Read the [bake documentation](https://docs.docker.com/build/bake/) for the HCL syntax: variables, functions, +`inherits` and `matrix` are all used below. + +## Getting `specs.hcl` + +In CI you don't: `bake.yml` reads `bake/specs.hcl` from this repository, at the version of `bake.yml` you call, before +your own bake files. The consuming repository doesn't keep a copy, and moving `bake.yml` to a new tag moves the rules +with it. + +For a local build, fetch it at that same version to `.specs.hcl` and keep it out of git. A Taskfile task reads the +version from the `uses:` line of your workflow, so the tag stays pinned in one place: + +```yaml +tasks: + specs-hcl: + desc: Fetch specs.hcl at the version of bake.yml in use + vars: + REF: + sh: grep -ohE 'specsnl/github-actions/\.github/workflows/bake\.yml@[^[:space:]#]+' .github/workflows/*.y*ml | head -1 | cut -d@ -f2 + cmd: curl -fsSLo .specs.hcl https://raw.githubusercontent.com/specsnl/github-actions/{{.REF}}/bake/specs.hcl +``` + +## Building locally + +```bash +docker buildx bake -f .specs.hcl -f docker-bake.hcl --print # what would be built, with every tag and label +docker buildx bake -f .specs.hcl -f docker-bake.hcl # build it +``` + +Locally the variables below keep their defaults: every image is tagged `:local` and labelled with version `dev`. To see +what a release would publish, set the variables the plan job would set: + +```bash +EVENT=push REF_TYPE=tag VERSION=1.2.3 \ + docker buildx bake -f .specs.hcl -f docker-bake.hcl --print +``` + +## The variables CI sets + +The plan job works these out once per run and passes them to bake as environment variables. Use them in your own HCL +too — `VERSION` is the usual one, for a version build arg. + +| Variable | Local default | In CI | +|----------------|---------------|------------------------------------------------------------------------------------------| +| `VERSION` | `dev` | See below | +| `EVENT` | *empty* | The event name: `push`, `pull_request`, `schedule`, `workflow_dispatch`, … | +| `REF_TYPE` | *empty* | `tag` or `branch` | +| `REF_NAME` | *empty* | The tag or branch name | +| `PR_NUMBER` | *empty* | The pull request number | +| `DISPATCH_TAG` | `latest` | The `dispatch-tag` input, lowercased | +| `REVISION` | *empty* | The commit SHA | +| `SOURCE` | *empty* | The repository URL | +| `CREATED` | *empty* | One timestamp for the whole run, so every platform and every target carries the same one | + +A release is **stable** when it is a tag push whose version has no `-` in it. `specs.hcl` works that out from `EVENT`, +`REF_TYPE` and `VERSION`; only a stable release moves `1.2`, `1` and `latest`. + +`VERSION` is, in this order: + +1. The `version` input, if given. +2. On a tag push: the tag, without a leading `v` when a digit follows it — `v1.2.3` becomes `1.2.3`, `version-2` + stays `version-2`. This is the string GoReleaser injects too, so an image never disagrees with the binary cut + from the same tag. +3. On a pull request: `pr-`. On a branch push: the branch name. On a schedule: `nightly`. On + `workflow_dispatch`: the dispatch tag. +4. Anything else: empty, and the version label is left out. + +## The `specs.hcl` functions + +### `specs_tags(images, variant)` + +Every image name in `images`, lowercased, crossed with the tags this run publishes: + +| Run | Tags | +|--------------------------------------|----------------------------------------------------------------| +| Tag push, stable semver (`v1.2.3`) | `1.2.3`, `1.2`, `1`, `latest` | +| Tag push, `v0.x` (`v0.5.3`) | `0.5.3`, `0.5`, `latest` — `0` would promise nothing under 0.x | +| Tag push, `v0.0.x` (`v0.0.5`) | `0.0.5`, `latest` | +| Tag push, prerelease (`v1.2.3-rc.1`) | `1.2.3-rc.1` only: no moving tags, no `latest` | +| Tag push, not semver (`2026.11`) | The version, plus `latest` when it has no `-` | +| Pull request | `pr-` | +| Branch push | The branch name, i.e. `main` | +| Schedule | `nightly` | +| `workflow_dispatch` | The dispatch tag, `latest` by default | +| Local build | `local` | +| Any other event | None — nothing to push, so the run builds without pushing | + +A non-empty `variant` is appended to every tag as a suffix — `1.2.3-alpine`, `1.2-alpine`, `pr-12-alpine` — and on a +stable release the bare variant name takes the place of `latest`: `alpine`. That is how `node`, `python` and +`postgres` publish their variants: one image name, tag suffixes. + +Characters a tag can't hold are replaced with `-`, so the branch `feature/foo` is tagged `feature-foo`. + +### `specs_labels(title, description)` + +The `org.opencontainers.image.*` labels: `version`, `revision`, `source`, `url`, `created`, `title` and +`description` — the keys `docker/metadata-action` gave images built with 3.x and earlier, except `licenses`. + +An empty value is left out rather than written as `""`, so pass `""` for a title or description the image shouldn't +carry. `source` is what links the package to its repository on GHCR. + +### `specs_annotations(title, description)` + +The same values as `specs_labels`, as annotations on each platform's image manifest. The publish job copies the labels +onto the multi-platform index and its descriptors as well, so every level reports the same version. + +## Writing `docker-bake.hcl` + +- The `default` group lists what CI publishes. Use the `targets` input to build something else. +- Every target needs `platforms`: the build fans out over them, one job per platform. +- Set `tags`, `labels` and `annotations` with the functions above. The first image name of a target is the one it is + pushed under; the others are tagged from the same manifest. +- Don't set `output` or `cache-from` / `cache-to`: CI replaces them with a push by digest and a GitHub Actions cache + per image, target and platform. Locally they work as usual. +- Build secrets come from the `build-secrets` secret, as environment variables: + `secret = ["id=composer_auth,env=COMPOSER_AUTH"]`. +- Provenance and SBOM attestations are off by default. Turn them on per target with + `attest = ["type=provenance,mode=max", "type=sbom"]`; GHCR lists each one as an extra `unknown/unknown` platform. + +## Examples + +Each example is a complete `docker-bake.hcl`, read after `specs.hcl`. + +### A plain image + +One Dockerfile, one stage, one image — `specsnl/utils`: + +```hcl +target "default" { + platforms = ["linux/amd64", "linux/arm64"] + tags = specs_tags(["ghcr.io/specsnl/utils"], "") + labels = specs_labels("utils", "") + annotations = specs_annotations("utils", "") +} +``` + +The image is titled `utils` and has no description label. A tag push of `v1.19.0` publishes `utils:1.19.0`, +`utils:1.19`, `utils:1` and `utils:latest`. + +### A PHP image + +`specsnl/php84` publishes three flavours, each with a `runtime` and a `builder` stage, from three Dockerfiles. All six +images build from one call, and per platform in one job, so a flavour's two stages share their layers. + +```hcl +variable "IMAGE" { + default = "ghcr.io/specsnl/php84" +} + +variable "FLAVOURS" { + default = [ + { name = "fpm", image = "" }, + { name = "apache", image = "/apache" }, + { name = "frankenphp", image = "/frankenphp" }, + ] +} + +group "default" { + targets = ["runtime", "builder"] +} + +target "_php" { + platforms = ["linux/amd64", "linux/arm64"] +} + +target "runtime" { + name = "runtime-${flavour.name}" + matrix = { flavour = FLAVOURS } + inherits = ["_php"] + dockerfile = "${flavour.name}/Dockerfile" + target = "runtime" + tags = specs_tags(["${IMAGE}${flavour.image}"], "") + labels = specs_labels("PHP 8.4 ${flavour.name}", "") + annotations = specs_annotations("PHP 8.4 ${flavour.name}", "") +} + +target "builder" { + name = "builder-${flavour.name}" + matrix = { flavour = FLAVOURS } + inherits = ["_php"] + dockerfile = "${flavour.name}/Dockerfile" + target = "builder" + tags = specs_tags(["${IMAGE}${flavour.image}/builder"], "") + labels = specs_labels("PHP 8.4 ${flavour.name} builder", "") + annotations = specs_annotations("PHP 8.4 ${flavour.name} builder", "") +} +``` + +The runtime stage is the image itself; the builder is a separate, nested package with its own `:latest`. That is the +right shape here — a builder image is a different product from the runtime, not a variant of it. + +### A Go CLI with variants + +`specsnl/specs-cli` with a `debian` and an `alpine` stage, and its version passed to the Dockerfile as a build arg: + +```hcl +group "default" { + targets = ["cli"] +} + +target "cli" { + name = "cli-${variant}" + matrix = { variant = ["debian", "alpine"] } + target = variant + platforms = ["linux/amd64", "linux/arm64"] + args = { SPECS_VERSION = VERSION } + tags = specs_tags(["ghcr.io/specsnl/specs-cli"], variant == "debian" ? "" : variant) + labels = specs_labels("specs-cli", "") + annotations = specs_annotations("specs-cli", "") +} +``` + +A tag push of `v1.2.3` publishes `1.2.3`, `1.2`, `1` and `latest` from `debian`, and `1.2.3-alpine`, `1.2-alpine`, +`1-alpine` and `alpine` from `alpine`. A prerelease publishes only `1.2.3-rc.1` and `1.2.3-rc.1-alpine`. + +The Dockerfile keeps ownership of the ldflag, and its own fallback for local builds without bake: + +```dockerfile +ARG GO_MODULE=github.com/specsnl/specs-cli +ARG SPECS_VERSION=dev + +RUN CGO_ENABLED=0 go build \ + -ldflags "-s -w -X ${GO_MODULE}/internal/cmd.Version=${SPECS_VERSION}" -o ./specs +``` + +A wrong arg name shows up in `docker buildx bake --print`, and buildx warns about an unused build arg. Assert the +version in a [pull request test](testing-images.md) as well, so it can't ship. + +On an event without a version (see above) `VERSION` is empty, and `SPECS_VERSION = VERSION` passes it empty. Write +`args = VERSION == "" ? {} : { SPECS_VERSION = VERSION }` if the Dockerfile's fallback should stand then. + +## Calling the workflow + +```yaml +name: Image + +on: + push: + branches: [main] + tags: ['v*'] + pull_request: + workflow_dispatch: + inputs: + tag: + description: Tag to publish + default: latest + +permissions: + contents: read + packages: write + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + image: + uses: specsnl/github-actions/.github/workflows/bake.yml@4.0.0 + with: + dispatch-tag: ${{ inputs.tag }} + secrets: + build-secrets: | + COMPOSER_AUTH=${{ secrets.COMPOSER_AUTH }} +``` + +The caller grants `contents: read` and `packages: write`. Require the check `image / Image` — the calling job's name, +then the workflow's `Image` job — in the branch protection rules or ruleset. It covers every image the call publishes. + +See [How the image pipeline works](pipeline.md) for what happens inside, and the +[workflow reference](workflows.md#bakeyml) for every input. diff --git a/docs/base-images.md b/docs/base-images.md index 488beab..6d7a0cf 100644 --- a/docs/base-images.md +++ b/docs/base-images.md @@ -1,9 +1,9 @@ # Base images -These workflows take a `target` — the Dockerfile stage to publish — and nothing else. They deliberately do **not** -offer a menu of base images to pick from. +The consuming repository's `docker-bake.hcl` names the Dockerfile stage each target publishes. Neither `bake.yml` nor +`specs.hcl` offers a menu of base images to pick from, and they deliberately won't. -## Why there is no `base-image` input +## Why there is no `base-image` setting The tempting design is a menu: let the caller pick `scratch`, `debian`, `debian-slim` or `alpine`. That is the wrong abstraction, because the choice is not a preference. It is a property of what the binary does at runtime, and nothing @@ -24,7 +24,7 @@ what the binary needs. ## Variants are a per-repository decision too -The [capability to publish variants](go-cli.md#publishing-variants) lives here; whether to use it does not. +The [capability to publish variants](bake.md#a-go-cli-with-variants) lives here; whether to use it does not. An `alpine` variant of specs-cli would not be `apk add --no-cache bash` and done. That part is one line. The part that is not fixable in a line: template hooks are arbitrary user-written shell, and busybox `sed` and `grep` differ from the @@ -36,4 +36,4 @@ A Go CLI that does not shell out has no such problem and can publish both freely ## If a shared Dockerfile is wanted later That is a separate concern. It is worth starting by not encoding a choice that would then have to be unpicked — a -`target` input stays correct either way, because a shared Dockerfile still has stages. +bake target naming a stage stays correct either way, because a shared Dockerfile still has stages. diff --git a/docs/go-cli.md b/docs/go-cli.md deleted file mode 100644 index 84f98b0..0000000 --- a/docs/go-cli.md +++ /dev/null @@ -1,164 +0,0 @@ -# Go CLI images - -`build-go-cli.yml` and `merge-go-cli.yml` publish a Go binary as a container image. They are thin shims over -[`build.yml` and `merge.yml`](workflows.md): the build shim only composes the version build arg, the merge shim only -adds the tag set a CLI release wants. Everything else — checkout, login, buildx, digests, the manifest — is the -generic pipeline described in [How the image pipeline works](pipeline.md). - -## A release workflow - -```yaml -name: Release - -on: - push: - tags: ['v*'] - -permissions: - contents: read - packages: write - -jobs: - - build: - uses: specsnl/github-actions/.github/workflows/build-go-cli.yml@3.1.0 - strategy: - fail-fast: false - matrix: - runner: - - os: ubuntu-24.04 - platform: linux/amd64 - - os: ubuntu-24.04-arm - platform: linux/arm64 - with: - runs-on: ${{ matrix.runner.os }} - platform: ${{ matrix.runner.platform }} - image-name: ghcr.io/specsnl/specs-cli - target: debian - version-build-arg: SPECS_VERSION - - merge: - needs: build - uses: specsnl/github-actions/.github/workflows/merge-go-cli.yml@3.1.0 - with: - runs-on: ubuntu-24.04 - image-name: ghcr.io/specsnl/specs-cli - target: debian -``` - -`target` is the Dockerfile stage to publish. The consuming repository owns its stages — see -[Base images](base-images.md) for why that is not an input here. - -## Version injection - -Go CLIs carry their version in an ldflag, so it has to be known at build time or the image reports its fallback -(`dev`). `build-go-cli.yml` passes it to the Dockerfile as the build arg named by `version-build-arg`; the Dockerfile -keeps ownership of the ldflag itself, which is how every Go repository in the org is already written: - -```dockerfile -ARG GO_MODULE=github.com/specsnl/specs-cli -ARG SPECS_VERSION=dev - -RUN CGO_ENABLED=0 go build \ - -ldflags "-s -w -X ${GO_MODULE}/internal/cmd.Version=${SPECS_VERSION}" -o ./specs -``` - -On a tag push the version defaults to **the tag without its leading `v`** — `v1.2.3` becomes `1.2.3` — so the -examples above pass no `version` at all. That default is not cosmetic. Every Go repository in the org also releases -through GoReleaser, which strips the `v` when it injects the same ldflag; passing `${{ github.ref_name }}` straight -through would publish an image reporting `v1.2.3` while the binary from that very tag reports `1.2.3`. - -Pass `version` explicitly to override it — to build a release from something other than its tag, or to stamp a version -on a branch build. On any trigger that is not a tag push the default is empty, and the build arg is then omitted -rather than passed empty, so the Dockerfile's own fallback stands. - -`version-build-arg` has no default on purpose, and is the one input here that genuinely cannot have one. The arg is -named after the binary and differs per repository (`SPECS_VERSION`, `LABELSYNC_VERSION`, …), and a wrong value fails -silently: buildx warns about an unused build arg, the build succeeds, and the image reports `dev`. Assert the version -in a [pull-request guard](testing-images.md) so that stays impossible to ship. - -Anything else the Dockerfile needs goes through `build-args`, which is appended to the version arg. - -The same version is written to `org.opencontainers.image.version` on the per-platform images. `merge-go-cli.yml` -resolves its own `version` input the same way and stamps it on the index, so all of them agree — including a variant, -where the index reports `1.2.3` rather than the `1.2.3-alpine` tag. If you pass `version` to one shim, pass the same -value to the other. - -## Tags - -On top of what [`merge.yml` already emits](pipeline.md#tags): - -| Tag | When | -|------------------------------|------------------------------------------------------------------------| -| `1.2.3` | Every `v*` tag, prereleases included | -| `1.2`, `1` | Stable tags only; `1` is skipped for `v0.*` and `1.2` for `v0.0.*` | -| `latest` | Stable tags only — set `latest: false` to never move it | -| `v1.2.3` | From `merge.yml`'s `type=ref,event=tag`; the same digest, `v`-prefixed | -| `main`, `pr-12`, `` | From `merge.yml`, for branch, pull request and dispatch runs | - -A prerelease never moves `latest` and never publishes `1.2` or `1`: - -- `metadata-action` collapses every semver pattern to the full version when the tag is a prerelease, so - `pattern={{major}}` yields `1.2.0-rc.1` rather than `1`. -- `:latest` would otherwise still move, because `type=ref,event=tag` sets it under the default `latest=auto`. This - workflow passes `flavor: latest=false` and re-adds `latest` itself, guarded on the tag being stable. - -That guard treats any tag containing a `-` as a prerelease — `v1.2.0-rc.1`, `v1.2.0-beta.3`. - -A moving tag is a compatibility promise, so it is withheld where semver does not make one. `1` means nothing under -`v0.*` — every minor may break. `1.2` means nothing under `v0.0.*` for the same reason one level down, since `0.0.x` is -the range where every release is free to break. `v0.5.3` still publishes `0.5`, because patches within `0.5.x` are -expected to be compatible. - -Both `flavor` and `raw-tags` are still available and are appended *after* what this workflow sets, so a caller can -override the flavor (`latest=auto` restores the default behaviour) and add tags of its own. - -## Publishing variants - -A repository that publishes more than one base image variant publishes them as **tag suffixes on one image name**, the -way `node`, `python` and `postgres` do — not as a second, nested image name with its own `:latest`, which is what the -[PHP pair](php.md) does for its builder stages. A CLI is one thing; `alpine` is a property of the image it ships in, -not a different product. - -Run the build and merge jobs once per variant, each with its own `target`: - -```yaml - build-alpine: - uses: specsnl/github-actions/.github/workflows/build-go-cli.yml@3.1.0 - strategy: - fail-fast: false - matrix: - runner: - - os: ubuntu-24.04 - platform: linux/amd64 - - os: ubuntu-24.04-arm - platform: linux/arm64 - with: - runs-on: ${{ matrix.runner.os }} - platform: ${{ matrix.runner.platform }} - image-name: ghcr.io/specsnl/specs-cli - target: alpine - version-build-arg: SPECS_VERSION - - merge-alpine: - needs: build-alpine - uses: specsnl/github-actions/.github/workflows/merge-go-cli.yml@3.1.0 - with: - runs-on: ubuntu-24.04 - image-name: ghcr.io/specsnl/specs-cli - target: alpine - variant: alpine -``` - -`variant: alpine` yields `specs-cli:1.2.3-alpine` and a bare `specs-cli:alpine`, alongside the unsuffixed primary -variant from the jobs above. The digests do not collide: they are namespaced per target. - -Under the hood that is `flavor: suffix=-alpine,onlatest=true` for the version tags, plus a `type=raw` tag carrying an -empty `suffix=` so the bare `:alpine` does not come out as `alpine-alpine`. - -## Consumers - -- **specsnl/specs-cli** — the first consumer, debian-only for now. -- **specsnl/labelsync** — same layout and a `Dockerfile` already, no image pipeline yet. -- **specsnl/specsdeployd** — a daemon installed from a GitHub release onto a host systemd unit. Its `Dockerfile` stops - at `export` and ships only the binary, so it is not a consumer unless that changes. diff --git a/docs/migration-4.0.md b/docs/migration-4.0.md new file mode 100644 index 0000000..aee96a7 --- /dev/null +++ b/docs/migration-4.0.md @@ -0,0 +1,133 @@ +# Migrating from 3.x to 4.0.0 + +4.0.0 replaces every build and merge workflow, and both composite actions, with one reusable workflow, `bake.yml`. What +to build moves out of workflow inputs into a `docker-bake.hcl` in the consuming repository. A repository that hasn't +migrated keeps working on `@3.1.0`; there is no need to move every repository at once. + +The migration per repository: + +1. Fetch `specs.hcl` for local builds, and add `.specs.hcl` to `.gitignore` — see + [Getting `specs.hcl`](bake.md#getting-specshcl). CI needs no copy. +2. Write `docker-bake.hcl` from the old inputs (below). +3. Check it locally with `docker buildx bake -f .specs.hcl -f docker-bake.hcl --print`, with the + [CI variables](bake.md#building-locally) of a release set, against the tags the last 3.x release published. +4. Replace every build and merge job with one `bake.yml` call. +5. Update the required checks in the same change: `image / Image` replaces the `Build Image` and `Merge Images` + checks. + +## Before and after + +`specsnl/utils`, a plain image, had a build and a merge job in each of its `main.yml`, `pr.yml` and `tag.yml`: + +```yaml +jobs: + build: + name: Build + uses: specsnl/github-actions/.github/workflows/build.yml@3.1.0 + strategy: + fail-fast: false + matrix: + runs-on: + - os: ubuntu-26.04 + platform: linux/amd64 + - os: ubuntu-26.04-arm + platform: linux/arm64 + with: + runs-on: ${{ matrix.runs-on.os }} + platform: ${{ matrix.runs-on.platform }} + image-name: ghcr.io/${{ github.repository }} + + merge: + name: Merge + uses: specsnl/github-actions/.github/workflows/merge.yml@3.1.0 + needs: build + with: + runs-on: ubuntu-26.04 + image-name: ghcr.io/${{ github.repository }} +``` + +With 4.0.0 the platforms and the image name move to `docker-bake.hcl`: + +```hcl +target "default" { + platforms = ["linux/amd64", "linux/arm64"] + tags = specs_tags(["ghcr.io/specsnl/utils"], "") + labels = specs_labels("utils", "") + annotations = specs_annotations("utils", "") +} +``` + +and each workflow keeps one job: + +```yaml +jobs: + image: + uses: specsnl/github-actions/.github/workflows/bake.yml@4.0.0 +``` + +The runners `ubuntu-26.04` and `ubuntu-26.04-arm` are the defaults, so the runner matrix disappears entirely. + +The PHP and Go CLI repositories follow the same pattern with the [PHP](bake.md#a-php-image) and +[Go CLI](bake.md#a-go-cli-with-variants) examples. For `specsnl/php84` that turns a build matrix of three Dockerfiles +times two runners, and a merge matrix of three images, into one call. + +## From the old inputs to HCL + +| 3.x input | 4.0.0 | +|----------------------------------------------------|---------------------------------------------------------------------------------------------------------| +| `runs-on` + `platform` (the runner matrix) | `platforms` per target; `runners` input only to change a platform's runner | +| `image-name` | The image names given to `specs_tags`; lowercased for you | +| `image-name` with several lines | Several image names in one `specs_tags` call | +| `dockerfile` | `dockerfile` on the target | +| `context` | `context` on the target | +| `target` | `target` on the target | +| `build-args` | `args` on the target | +| `secrets` (string input) | The `build-secrets` secret, plus `secret = ["id=…,env=…"]` on the target | +| `version-build-arg` (Go CLI) | `args = { SPECS_VERSION = VERSION }` | +| `title`, `description`, `title-*`, `description-*` | The arguments of `specs_labels` and `specs_annotations` | +| `variant` (Go CLI) | The second argument of `specs_tags` | +| `latest` (Go CLI) | Gone: a stable release always moves `latest`. Leave out `specs_tags` and write your own tags to opt out | +| `raw-tag` | The `dispatch-tag` input | +| `raw-tags`, `flavor` | Gone: append tags in HCL, i.e. `concat(specs_tags(…), ["${IMAGE}:extra"])` | +| `version` | The `version` input, once, instead of on the build and merge separately | +| `push` | The `push` input, once | +| `load` (`build-image`) | `docker/bake-action` with `load: true`; see [Testing an image](testing-images.md) | +| `disable-apparmor` | The `disable-apparmor` input | + +## What was removed + +| Removed | Replaced by | +|-----------------------------------------------|---------------------------------------------------------------------| +| `build-image` action | The build job of `bake.yml`; for tests, `docker/bake-action` | +| `create-manifest` action | The publish job of `bake.yml` | +| `build.yml`, `merge.yml` | `bake.yml` | +| `build-php.yml`, `merge-php.yml` | `bake.yml` and the [PHP example](bake.md#a-php-image) | +| `build-go-cli.yml`, `merge-go-cli.yml` | `bake.yml` and the [Go CLI example](bake.md#a-go-cli-with-variants) | +| `notify-slack-tag.yml`'s `channel` input | Nothing: an incoming webhook always posts to its own channel | + +## What publishes differently + +Check these against your last 3.x release before you merge the migration: + +- **No `v`-prefixed tag.** 3.x tagged the git tag verbatim (`v1.2.3`); 4.0.0 tags the version without the `v`. +- **The semver set for every image.** `1.2.3`, `1.2` and `1` (withheld under `0.x` and `0.0.x`), which 3.x only gave + the Go CLI shims. Under 3.x a PHP tag `0.5.8` published `0.5.8` and `latest`; now it also publishes `0.5`. +- **A prerelease never moves `latest`**, for every image, not only Go CLIs. +- **`org.opencontainers.image.version` is the version without the `v`**, on a plain image too, and it is the same in + the config labels, the platform manifests, the index and its descriptors. +- **The same label keys, with the same value everywhere.** `specs_labels` sets the keys metadata-action did, except + `licenses`: `title`, `description`, `version`, `revision`, `source`, `url` and `created`. What changes is that the + index, its descriptors, the platform manifests and the config labels now agree: 3.x left `version` empty (or `latest`) + on the platform images, and wrote a different `created` in each place. +- **No repository fallback.** 3.x took an empty title or description, and the license, from the repository. 4.0.0 + leaves them out, so pass the title and description to `specs_labels`. +- **Push follows the event**, not `github.actor`: pull requests from forks and dependabot pull requests that a person + re-runs no longer try to push. See [when nothing is pushed](pipeline.md#when-nothing-is-pushed). + +## Checks and permissions + +There is now one call per repository, and one check to require: ` / Image` — with the job id `image` used +throughout these docs, `image / Image`. The old `Build Image (…)` and `Merge Images (…)` checks no longer exist, so +update the branch protection rules or ruleset in the same pull request, or nothing will satisfy them. + +The caller grants `contents: read` and `packages: write`. `actions: write` isn't needed. diff --git a/docs/php.md b/docs/php.md deleted file mode 100644 index 018b8dc..0000000 --- a/docs/php.md +++ /dev/null @@ -1,62 +0,0 @@ -# PHP images - -`build-php.yml` and `merge-php.yml` publish the two stages of a Specs PHP image. They are thin shims over -[`build.yml` and `merge.yml`](workflows.md), holding only the PHP-specific part: which stages get published, and under -what name. - -```yaml -jobs: - - build: - uses: specsnl/github-actions/.github/workflows/build-php.yml@3.1.0 - strategy: - fail-fast: false - matrix: - runner: - - os: ubuntu-24.04 - platform: linux/amd64 - - os: ubuntu-24.04-arm - platform: linux/arm64 - with: - runs-on: ${{ matrix.runner.os }} - platform: ${{ matrix.runner.platform }} - image-name: ghcr.io/${{ github.repository }} - - merge: - needs: build - uses: specsnl/github-actions/.github/workflows/merge-php.yml@3.1.0 - with: - runs-on: ubuntu-24.04 - image-name: ghcr.io/${{ github.repository }} -``` - -Each shim fans out into one `build.yml` / `merge.yml` call per stage, so a single call publishes both. - -A repository publishing several flavours of the same PHP version — `fpm`, `apache`, `frankenphp` — adds a second -matrix dimension over `dockerfile` / `image-name` pairs, and matches it with a matrix over `image-name` on the merge -job. `specsnl/php84` is the reference. - -## What gets published - -| Dockerfile target | Image name | -|-------------------|------------------------| -| `runtime` | `` | -| `builder` | `/builder` | - -The runtime stage is the image itself; the builder is a separate, nested package with its own `:latest`. That -is the right shape here — a builder image is a different product from the runtime, not a variant of it. A CLI that -ships several base images does the opposite and uses [tag suffixes on one name](go-cli.md#publishing-variants). - -The builder includes Node.js. It used to be split into a `builder` stage without Node.js and a `builder_nodejs` stage -with it, published as `/builder_nodejs` and `/node`. Those names are no longer published: their last images stay in -the registry but don't receive updates, so pull `/builder` instead. - -## Titles and descriptions - -`build-php.yml` takes one `title` / `description` pair for both stages. `merge-php.yml` takes them per stage — -`title-runtime`, `title-builder` and their `description-*` counterparts — because that is where the manifest is tagged -and where the labels end up mattering per package. -## AppArmor - -`disable-apparmor: true` disables AppArmor on the runner before building, for images whose build steps it interferes -with. It is forwarded to `build.yml` unchanged. diff --git a/docs/pipeline.md b/docs/pipeline.md index 3d6c5a8..41bfba7 100644 --- a/docs/pipeline.md +++ b/docs/pipeline.md @@ -1,100 +1,101 @@ # How the image pipeline works -Every image in this repository is published in the same two phases, whichever workflow pair you call. +`bake.yml` runs four jobs: plan, build, publish and status. -## Phase 1 — build, one job per platform +``` +plan ──► build linux/amd64 ──┐ + └─► build linux/arm64 ──┴─► publish ──► status ("Image") +``` -`build.yml` builds a single platform and pushes it to the registry **by digest**: no tags, just a manifest blob. It -then writes the digest to a file and uploads it as an artifact. +## Plan — work everything out once -Nothing is tagged yet, so a half-finished matrix never leaves a `:latest` pointing at one architecture. +The plan job works out everything that depends on the run, once: -## Phase 2 — merge, one job for all platforms +- the bake variables — `VERSION`, the event, the ref, the revision and one `CREATED` timestamp (see + [the variables CI sets](bake.md#the-variables-ci-sets)); +- whether to push (see [below](#when-nothing-is-pushed)). -`merge.yml` downloads every digest artifact from phase 1 and runs `docker buildx imagetools create` to build one -multi-arch manifest from them, tagged with everything `metadata-action` produced. It finishes by inspecting the result, -so the job log shows what was actually published. +It then runs `docker buildx bake --print` on the consumer's bake files with those variables set, and **freezes** the +result: the printed JSON as it is, for the publish job, and a definition per platform with only the targets that list +that platform, each target's `output` replaced by a push by digest to its first image name and its tags left out. They +all go into one artifact. -Run the build job once per platform (a matrix) and the merge job once, with `needs:` on the build job. +Every later job builds from that frozen definition instead of evaluating the HCL again. That is what guarantees every +platform and the publish job see identical tags and labels — including `created`, which would otherwise differ per +job. -## Digests are namespaced per image and per target +The plan job also: -The artifact is named `digests---`, with `/` replaced by `-` and an absent target -recorded as `_NO_TARGET_`. The merge job downloads with the pattern `digests---*`. +- outputs the platforms, the union over all targets, as the build matrix, each with its runner from the `runners` + input; +- reads `specs.hcl` from this repository, at the version of the workflow, before the consumer's bake files; +- fails early, with the target named, when a target has no `platforms`, or has no tags for an event it would push. -That namespacing is what lets a single run publish several images without them bleeding into each other — the two -PHP stages, or a `debian` and an `alpine` variant of the same CLI. Each build/merge pair only ever sees its own -digests. +The run summary shows every target, its platforms and its tags. -Artifacts are kept for one day. They are an implementation detail of a single run, not a build record. +## Build — one job per platform, all targets at once -## Tags +`build` runs once per platform, named `Build `. Each job checks out the repository and builds every target +in its platform's frozen definition in one `docker/bake-action` run, so stages of the same Dockerfile share their +layers. A target without that platform is skipped by that job, so targets can have +different platform sets. -Tagging happens entirely in phase 2, from `metadata-action`. `create-manifest` always emits: +Each image is pushed **by digest**: no tags, just the manifest. Nothing is tagged yet, so a half-finished run never +leaves a `:latest` pointing at one architecture. Bake's metadata, which holds the digests, is uploaded as one artifact +per platform. -| Directive | Produces | -|---------------------------|------------------------------------------------------| -| `type=schedule` | `nightly` on a scheduled run | -| `type=ref,event=branch` | The branch name, i.e. `main` | -| `type=ref,event=tag` | The tag name verbatim, i.e. `v1.2.3` or `2.2.0` | -| `type=ref,event=pr` | `pr-` | -| `type=raw`, on dispatch | The `raw-tag` input, lowercased, `latest` by default | +Each target gets its own GitHub Actions cache scope, `--`, so two images built from the same +Dockerfile and stage no longer overwrite each other's cache. -That last one is filtered with `enable=${{ github.event_name == 'workflow_dispatch' }}`, not -`event=workflow_dispatch`. `event=` belongs to `type=ref`; `type=raw` takes only `enable`, `priority`, `prefix`, -`suffix` and `value`. An `event=` written on a `type=raw` directive does not fail — metadata-action ignores the -attribute it does not know, and the tag silently becomes unconditional. That is worth remembering when editing -either tags block: the mistake has no symptom until a tag lands somewhere it should not. +## Publish — one job for all targets -`build-image` carries the same five directives, where they have no effect on the registry — that build pushes by -digest, so its computed tags are never applied. What they do decide is `org.opencontainers.image.version` on the -per-platform manifests and image configs. `imagetools create` cannot change those after the fact, as they are -addressed by their digest, so the version has to be right when they are built. +`publish` downloads the frozen definition and every platform's metadata, and for each target runs +`docker buildx imagetools create` with the target's digests, tags, and labels as `index:` and `manifest-descriptor:` +annotations. It then inspects the result by its first tag, so the log shows what was actually published. -`raw-tags` and `flavor` below exist only on `create-manifest`. When you use them, the version the merge derives can -differ from the build's — a `type=semver` tag turns `v1.2.3` into `1.2.3`, a `suffix=-alpine` appends to it. Pass the -same `version` to both the build and the merge to keep them identical; [`build-go-cli.yml` and -`merge-go-cli.yml`](go-cli.md) do that for you. +It doesn't check out the repository: it only needs the artifacts. -Two inputs extend that: +## Status — one check -- `raw-tags` is appended to the directives above, for anything else you want tagged — i.e. the semver set - `merge-go-cli.yml` adds. -- `flavor` is passed to `metadata-action`'s flavor block: `latest=false` to stop a tag from moving `:latest`, - `prefix=` / `suffix=` to publish a variant under the same image name. +`status`, named `Image`, always runs and fails unless plan succeeded and build and publish succeeded or were skipped. +Require that one check: there is one call per repository, so it covers every image the repository publishes. A caller +sees it as ` / Image`. -`:latest` is worth understanding. `metadata-action` defaults to `latest=auto`, and under `auto` a -`type=ref,event=tag` match sets it — for *any* tag, prereleases included. If you publish an `-rc` channel, pass -`flavor: latest=false` and add your own guarded `latest` tag through `raw-tags`, which is what -[`merge-go-cli.yml`](go-cli.md) does. +## Metadata, in every place -Note that `flavor: latest=false` only governs the implicit `:latest` that `type=ref` and `type=semver` bring with -them. It has no effect on an explicit `type=raw,value=latest`, which is why the dispatch directive above needs a -filter of its own rather than relying on the flavor. +The version and the other `org.opencontainers.image.*` values come from `specs_labels` and end up in four places, all +from the same frozen definition: -## Labels and annotations +| Where | Set by | +|------------------------------|-----------------------------------------------------------| +| The image config labels | The build, from the target's `labels` | +| The platform manifest | The build, from the target's `annotations` | +| The index | Publish, from the target's labels as `index:` annotations | +| The descriptors in the index | Publish, as `manifest-descriptor:` annotations | -`title`, `description` and `version` override the corresponding `org.opencontainers.image.*` values. Anything left -empty falls back to what `metadata-action` derives from the repository. +To check a published image: -`build-image` writes them as labels on the image config and as annotations on the per-platform manifest. -`create-manifest` applies them to the manifest as both `index:` and `manifest-descriptor:` annotations, so they show up -whether a tool reads the index or the descriptor pointing at it. Both drop a key whose value is empty instead of -writing `key=`, so an event without a derived version leaves `org.opencontainers.image.version` out altogether. +```bash +docker buildx imagetools inspect --raw | jq .annotations +d=$(docker buildx imagetools inspect --raw | jq -r '.manifests[0].digest') +docker buildx imagetools inspect @$d --raw | jq .annotations +docker buildx imagetools inspect --format '{{json .Image}}' | jq '."linux/amd64".config.Labels' +``` -## Several image names for one manifest +## When nothing is pushed -`create-manifest` accepts a multiline `image-name`. The first line is the primary name — the one the digests were -pushed under and the one the inspect step reports — and every line is tagged from the same manifest. +`push` defaults to `true`, except for: -## What dependabot gets +- a pull request opened by dependabot — also when a person re-runs its jobs, because the token stays read-only; +- a pull request from a fork, whose token is read-only too; +- a push to a `dependabot/…` branch; +- an event the [tag rules](bake.md#specs_tagsimages-variant) don't cover, such as `merge_group`. -Dependabot pull requests only ever build; they never publish. +Set the `push` input to override it. Without a push the build still runs — with `type=cacheonly` output, filling the +cache — and publish is skipped, so a dependency bump still has to build before it can be merged. `Image` reports +success. -- The registry login step in `build.yml` is skipped. -- `build-image` defaults `push` to false, so the build runs but writes nothing to the registry. -- `merge.yml` defaults `push` to false too, so its job runs but skips every step and nothing is tagged. It is not - skipped as a whole job: GitHub would report that under its unevaluated name, and a required `Merge Images` check would - never pass. +## Artifacts -The point is that the build itself still has to succeed before a dependency bump can be merged. +The artifacts are named `bake-definition` and `bake-metadata-`, so a run calls the workflow once. They are +kept for one day: they belong to a single run, not to a build record. diff --git a/docs/releasing.md b/docs/releasing.md index bd26271..186048a 100644 --- a/docs/releasing.md +++ b/docs/releasing.md @@ -1,44 +1,58 @@ # Releasing this repository -Consumers pin by tag — `…/build.yml@2.2.0`, never `@main` — so nothing here reaches anyone until a tag exists. +Consumers pin by tag — `…/bake.yml@4.0.0`, never `@main` — so nothing here reaches anyone until a tag exists. -Tags are plain semver with no `v` prefix: `2.2.0`, `2.1.0`, `2.0.0-rc.1`. +Tags are plain semver with no `v` prefix: `4.0.0`, `3.0.2`, `2.0.0-rc.1`. ## Cutting a release 1. Merge the pull request. This repository merges with merge commits, so every commit on the branch lands in `main`. 2. Tag the merge commit on `main` with the new version and push the tag. -Bumping the internal references is part of the change, not of the tagging: the reusable workflows call the composite -actions by tag too, so a pull request that changes an action **must** end with a commit moving -`.github/workflows/build.yml` and `merge.yml` to the version about to be cut. See commit `3765927` for the shape. +There is nothing to bump before tagging. `bake.yml` reaches its own scripts and `specs.hcl` through the composite +actions under `bake/`, called with the `$/` self-repository syntax, which always resolves to the commit that is +running. A tag can't ship with references to an older one, which is how `3.0.1` shipped without its fix. `lint.yml` +fails when a workflow starts referencing this repository by tag again. -That ordering means the reference is briefly ahead of reality — `build.yml` on `main` points at a tag that does not -exist yet. Cutting the tag immediately after merging closes that window. Until then, `main` is not usable, which is -another reason consumers pin. +## `specs.hcl` + +Consumers get `specs.hcl` from the version of `bake.yml` they call, so a change to it reaches them with the next tag +they move to. It changes what they publish, so check the fixture test diff as carefully as the change itself. +`task test:update` rewrites the expected output of the fixture tests. ## Versioning -| Bump | When | -|-------|----------------------------------------------------------------------------------------| -| Major | An input is removed or renamed, or a default changes what an existing caller publishes | -| Minor | A new input, workflow or action; new behaviour behind an opt-in | -| Patch | A fix that leaves every caller's published output the same | +| Bump | When | +|-------|-------------------------------------------------------------------------------------------------------------------------------------| +| Major | An input is removed or renamed, a job or check is renamed, or a tag rule in `specs.hcl` changes what an existing consumer publishes | +| Minor | A new input, workflow or `specs.hcl` function; new behaviour behind an opt-in | +| Patch | A fix that leaves every consumer's published output the same | -Adding an input with a default that preserves current behaviour is a minor bump — `build-image`'s `load` and `push` -are the example. Changing which tags an existing caller gets is not. +Changing which tags or labels an existing consumer gets is a major bump, even when it is a fix. ## Announcing -`notify-slack-tag.yml` posts the tag to `#releases`. A consuming repository calls it on `push: tags`; see the -[workflow reference](workflows.md#notify-slack-tagyml). +`notify-slack-tag.yml` posts the tag to the channel of the webhook. A consuming repository calls it on `push: tags`; +see the [workflow reference](workflows.md#notify-slack-tagyml). ## Keeping actions up to date -Dependabot runs daily against `github-actions` here and opens pull requests for the third-party actions used inside -the workflows and composite actions. `actionlint` is the only check that runs on them; nothing in this repository -builds an image, so a bump is only exercised for real once a consumer runs it. +Every action is pinned by commit SHA, with its version in a comment. Dependabot checks them weekly and groups the +updates into one pull request. `lint.yml` and `test-bake.yml` run on it; `test-bake.yml` builds a test image through +`bake.yml`, so a bump is exercised before it is released. + +The actionlint and zizmor versions are not updated by dependabot. Keep the ones in `lint.yml` in step with +`ACTIONLINT_TAG_VERSION` and `ZIZMOR_VERSION` in `Taskfile.dist.yml`. + +`.github/actionlint.yaml` works around what actionlint 1.7.12 doesn't know yet: the `ubuntu-26.04` runner labels, and +the `$/` self-repository syntax `test-bake.yml` uses to call `bake.yml` +([rhysd/actionlint#711](https://github.com/rhysd/actionlint/issues/711)). Remove each part once an actionlint release +supports it. + +`test-bake.yml` exercises the publish job too: on a pull request from this repository and on a push to `main` it +publishes `ghcr.io/specsnl/github-actions/e2e` and `…/e2e-cli`, and its `Verify` job checks the platforms and +annotations of what was published. A manual run publishes only with `push` on. -Such a bump still needs a release before it reaches anyone, and it is a normal change: it needs the internal reference -commit and a tag like any other. In the consuming repositories the same dependabot updates the pin on these workflows, -and those pull requests build without publishing — see [what dependabot gets](pipeline.md#what-dependabot-gets). +`cleanup-e2e.yml` runs every night and deletes the `pr-` images of closed pull requests, and the untagged platform +images no tag refers to any more. Untagged images younger than a day stay, so a run that is still publishing keeps its +platform images. Run it manually with `dry-run` on to see what it would delete. diff --git a/docs/testing-images.md b/docs/testing-images.md index 19e8262..cb1f858 100644 --- a/docs/testing-images.md +++ b/docs/testing-images.md @@ -1,60 +1,77 @@ # Testing an image in a pull request -`build.yml` pushes by digest, which is right for a release but not for a pull-request guard. The point of checking a -CLI image is *running* the binary: asserting `--version`, that the entrypoint works, that the non-root user can write -into a bind mount. An image that is only compiled and never executed misses most of what can break. +`bake.yml` pushes by digest, which is right for a release but not for a pull request test. The point of testing an +image is *running* it: asserting `--version`, that the entrypoint works, that the non-root user can write into a bind +mount. An image that is only built and never run misses most of what can break. -Use the `build-image` action directly for that. The image has to be built and run in the same job, so a reusable -workflow is the wrong level — it would load the image into a daemon the caller cannot reach. +The image has to be built and run in the same job, so a reusable workflow is the wrong level. Run +`docker/bake-action` yourself, with `load: true`, on the same bake files: ```yaml name: CI on: pull_request +permissions: + contents: read + jobs: image: - runs-on: ubuntu-24.04 + runs-on: ubuntu-26.04 steps: - uses: actions/checkout@v7 + with: + persist-credentials: false + - uses: docker/setup-buildx-action@v4 - - id: build - uses: specsnl/github-actions/build-image@3.1.0 + # specs.hcl at the version of bake.yml this repository calls; see docs/bake.md. + - name: Fetch specs.hcl + run: | + ref=$(grep -ohE 'specsnl/github-actions/\.github/workflows/bake\.yml@[^[:space:]#]+' .github/workflows/*.y*ml | head -1 | cut -d@ -f2) + curl -fsSLo .specs.hcl "https://raw.githubusercontent.com/specsnl/github-actions/$ref/bake/specs.hcl" + + - uses: docker/bake-action@v7 + env: + VERSION: ci with: - platform: linux/amd64 - image-name: ghcr.io/specsnl/specs-cli - dockerfile: Dockerfile - target: debian + source: . + files: | + .specs.hcl + docker-bake.hcl + targets: cli-debian load: true - build-args: SPECS_VERSION=ci + set: '*.platform=linux/amd64' # Proves the ldflag landed: a wrong build arg name would leave this at "dev". - name: Smoke test - env: - IMAGE: ${{ steps.build.outputs.image }} - run: test "$(docker run --rm "$IMAGE" --version)" = "ci" + run: test "$(docker run --rm ghcr.io/specsnl/specs-cli:local --version)" = "ci" ``` -## What `load` does +## What this does -- Builds a **single** platform and loads the result into the runner's Docker daemon, rather than pushing it. -- Tags it `:` — `raw-tag` defaults to `latest` — and reports that reference as the `image` - output, which is what the step above runs. -- Skips the digest export and artifact upload, since there is no merge phase to feed. +- `source: .` builds from the checked-out files, the same ones CI and a local build read, instead of bake-action's + default Git context. +- `load: true` loads the result into the runner's Docker daemon instead of pushing it. That works for one platform at + a time, hence `*.platform=linux/amd64`. +- None of the CI variables are set, so `specs_tags` gives the local tag: `:local`. That is the reference to + run. +- `VERSION` is a bake variable, read from the environment. `ci` above is deliberate: a literal the test can compare + against exactly, which fails if the build arg name is wrong and the Dockerfile falls back to `dev`. `${{ github.sha }}` + works just as well. -## `load` and `push` are mutually exclusive +## A dedicated test target -`load: true` implies `push: false`. Setting both fails the build with a clear error, because a single build cannot -write to the registry and to the local daemon at once. +When the test needs something the published image doesn't — test tools, a different entrypoint — give it a target of +its own, outside the `default` group so CI never publishes it: -`push: false` on its own is a different thing: it builds without writing to the registry, but nothing is loaded either, -so there is no image to run. That is what dependabot already gets by default. A guard that wants to execute the binary -wants `load`. - -## Pinning the version under test +```hcl +target "test" { + inherits = ["cli-debian"] + target = "test" + tags = ["specs-cli:test"] +} +``` -Pass whatever version the guard should assert through `build-args`. `SPECS_VERSION=ci` above is deliberate: a literal -the test can compare against exactly, which fails if the build arg name is wrong and the Dockerfile falls back to -`dev`. `${{ github.sha }}` works just as well. +and build that with `targets: test`. diff --git a/docs/workflows.md b/docs/workflows.md index f397210..70a336f 100644 --- a/docs/workflows.md +++ b/docs/workflows.md @@ -1,129 +1,81 @@ # Reusable workflow reference -Every input, per workflow. Call them with -`uses: specsnl/github-actions/.github/workflows/@`. - -## `build.yml` - -Builds one platform and pushes it by digest. Run it once per platform. - -| Input | Type | Default | Description | -|--------------------|---------|--------------|------------------------------------------------------------------| -| `runs-on` | string | *required* | Runner name, i.e. `ubuntu-24.04` or `ubuntu-24.04-arm` | -| `platform` | string | *required* | Platform, i.e. `linux/amd64` | -| `image-name` | string | *required* | Image name; lowercased before use | -| `dockerfile` | string | `Dockerfile` | Dockerfile path | -| `context` | string | `.` | Build context path | -| `target` | string | — | Dockerfile stage to build; omit for the default (last) stage | -| `title` | string | — | Overrides `org.opencontainers.image.title` | -| `description` | string | — | Overrides `org.opencontainers.image.description` | -| `version` | string | — | Sets `org.opencontainers.image.version`; match `merge.yml`'s | -| `secrets` | string | — | Secrets to expose to the build (multiline) | -| `build-args` | string | — | Build-time variables (multiline `KEY=value`) | -| `push` | string | — | `"true"` / `"false"`; defaults to pushing, except for dependabot | -| `disable-apparmor` | boolean | `false` | Disable AppArmor on the runner before building | - -The registry login step is skipped for dependabot, which is why `push` defaults the way it does. - -## `merge.yml` - -Merges the digests from `build.yml` into one tagged multi-arch manifest. Run it once, with `needs:` on the build job. -When `push` is off — by default for dependabot — the job still runs but skips every step, so a required `Merge Images` -check reports success. - -| Input | Type | Default | Description | -|---------------|--------|------------|----------------------------------------------------------------------| -| `runs-on` | string | *required* | Runner name | -| `image-name` | string | *required* | Image name; multiline tags one manifest under several names | -| `target` | string | — | The target the digests were built from; must match the build job | -| `title` | string | — | Overrides `org.opencontainers.image.title` | -| `description` | string | — | Overrides `org.opencontainers.image.description` | -| `version` | string | — | Sets `org.opencontainers.image.version` | -| `raw-tag` | string | `latest` | Tag used on `workflow_dispatch` runs | -| `raw-tags` | string | — | Extra `metadata-action` tag directives (multiline), appended | -| `flavor` | string | — | `metadata-action` flavor directives (multiline), i.e. `latest=false` | -| `push` | string | — | `"true"` / `"false"`; defaults to pushing, except for dependabot | - -See [How the image pipeline works](pipeline.md#tags) for what gets tagged by default. - -## `build-php.yml` - -Fans out into one `build.yml` call per PHP stage. See [PHP images](php.md). - -| Input | Type | Default | Description | -|--------------------|---------|--------------|------------------------------------------------| -| `runs-on` | string | *required* | Runner name | -| `platform` | string | *required* | Platform | -| `image-name` | string | *required* | Base image name; stages nest under it | -| `dockerfile` | string | `Dockerfile` | Dockerfile path | -| `title` | string | — | Applied to both stages | -| `description` | string | — | Applied to both stages | -| `secrets` | string | — | Secrets to expose to the build | -| `disable-apparmor` | boolean | `false` | Disable AppArmor on the runner before building | - -## `merge-php.yml` - -Fans out into one `merge.yml` call per PHP stage. - -| Input | Type | Default | Description | -|-----------------------|--------|------------|-----------------------------------| -| `runs-on` | string | *required* | Runner name | -| `image-name` | string | *required* | Base image name | -| `title-runtime` | string | — | Title for the runtime image | -| `description-runtime` | string | — | Description for the runtime image | -| `title-builder` | string | — | Title for the builder image | -| `description-builder` | string | — | Description for the builder image | -| `raw-tag` | string | `latest` | Tag used on `workflow_dispatch` | - -## `build-go-cli.yml` - -`build.yml` plus the version build arg. See [Go CLI images](go-cli.md). - -| Input | Type | Default | Description | -|---------------------|---------|--------------|------------------------------------------------------------------| -| `runs-on` | string | *required* | Runner name | -| `platform` | string | *required* | Platform | -| `image-name` | string | *required* | Image name | -| `version` | string | *required* | Version injected into the binary, i.e. the release tag | -| `version-build-arg` | string | *required* | Dockerfile ARG the version is passed to, i.e. `SPECS_VERSION` | -| `dockerfile` | string | `Dockerfile` | Dockerfile path | -| `context` | string | `.` | Build context path | -| `target` | string | — | Dockerfile stage to publish | -| `title` | string | — | Overrides `org.opencontainers.image.title` | -| `description` | string | — | Overrides `org.opencontainers.image.description` | -| `build-args` | string | — | Additional build-time variables, appended to the version arg | -| `secrets` | string | — | Secrets to expose to the build | -| `push` | string | — | `"true"` / `"false"`; defaults to pushing, except for dependabot | -| `disable-apparmor` | boolean | `false` | Disable AppArmor on the runner before building | - -## `merge-go-cli.yml` - -`merge.yml` plus the semver tag set a CLI release wants. - -| Input | Type | Default | Description | -|---------------|---------|------------|---------------------------------------------------------------------------| -| `runs-on` | string | *required* | Runner name | -| `image-name` | string | *required* | Image name | -| `target` | string | — | The target the digests were built from | -| `variant` | string | — | Publish as a tag-suffixed variant, i.e. `alpine` | -| `version` | string | — | Sets `org.opencontainers.image.version`; defaults to the tag without `v` | -| `title` | string | — | Overrides `org.opencontainers.image.title` | -| `description` | string | — | Overrides `org.opencontainers.image.description` | -| `latest` | boolean | `true` | Move `:latest` (or `:`); only ever applies to a stable tag | -| `raw-tag` | string | `latest` | Tag used on `workflow_dispatch` runs | -| `raw-tags` | string | — | Extra tag directives, appended after this workflow's | -| `flavor` | string | — | Extra flavor directives, appended after this workflow's, so they override | -| `push` | string | — | `"true"` / `"false"`; defaults to pushing, except for dependabot | +Every input, per workflow. Call them with `uses: specsnl/github-actions/.github/workflows/@`. + +## `bake.yml` + +Builds and publishes every target of the consumer's bake files. See [Building images with bake](bake.md) and +[How the image pipeline works](pipeline.md). + +| Input | Type | Default | Description | +|--------------------|---------|-------------------|----------------------------------------------------------------------------------------| +| `files` | string | `docker-bake.hcl` | The consumer's bake files (multiline), read after this version's `bake/specs.hcl` | +| `targets` | string | — | Targets or groups to build (multiline); the `default` group when empty | +| `version` | string | — | Overrides `VERSION`; see [how it is worked out](bake.md#the-variables-ci-sets) | +| `dispatch-tag` | string | `latest` | Tag published by a `workflow_dispatch` run; lowercased | +| `push` | string | — | `"true"` / `"false"`; see [when nothing is pushed](pipeline.md#when-nothing-is-pushed) | +| `runners` | string | — | JSON object mapping a platform to its runner, merged over the defaults below | +| `disable-apparmor` | boolean | `false` | Disable AppArmor on the build runners before building | +| `timeout-minutes` | number | `60` | Timeout of each build job | + +| Secret | Required | Description | +|-----------------|----------|--------------------------------------------------------------------------------------| +| `build-secrets` | no | `NAME=value` lines, each masked and exported to the build as an environment variable | + +| Output | Description | +|-----------|-------------------------------------------------------| +| `version` | The `VERSION` the images were built with | +| `push` | Whether the images were pushed (`"true"` / `"false"`) | + +The default runners are `ubuntu-26.04` for `linux/amd64` and `ubuntu-26.04-arm` for `linux/arm64`. A platform without +a runner fails the plan job. A runner may be a label or a list of labels: + +```yaml + with: + runners: '{"linux/arm64": ["self-hosted", "arm64"]}' +``` + +A build secret's value must fit on one line. The bake file refers to it by its name: +`secret = ["id=composer_auth,env=COMPOSER_AUTH"]`. + +The caller has to grant: + +```yaml +permissions: + contents: read + packages: write +``` + +| Job | Check name | Runs on | +|-----------|--------------------|------------------------------| +| `plan` | `Plan` | `ubuntu-26.04` | +| `build` | `Build ` | The platform's runner | +| `publish` | `Publish` | `ubuntu-26.04`, when pushing | +| `status` | `Image` | `ubuntu-26.04`, always | + +Require only `Image`. ## `notify-slack-tag.yml` -Posts a Slack message with the tag, repository, actor and a link to the release. +Posts a Slack message with the tag, repository, actor and a link to the release. The incoming webhook decides the +channel. -| Input | Type | Default | Description | -|-----------|--------|-------------|-------------------------| -| `runs-on` | string | *required* | Runner name | -| `channel` | string | `#releases` | Slack channel to notify | +| Input | Type | Default | Description | +|-----------|--------|----------------|-------------| +| `runs-on` | string | `ubuntu-26.04` | Runner name | | Secret | Required | Description | |---------------------|----------|----------------------------| | `slack-webhook-url` | yes | Slack incoming webhook URL | + +```yaml +on: + push: + tags: ['*'] + +jobs: + notify: + uses: specsnl/github-actions/.github/workflows/notify-slack-tag.yml@4.0.0 + secrets: + slack-webhook-url: ${{ secrets.SLACK_WEBHOOK_URL }} +```