Skip to content
Open
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
115 changes: 115 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,121 @@ jobs:
echo "::error::Expected outputs '${expected}', got '${OUTPUTS}'"
exit 1
fi
# release-image: the script (settings, the check of the version's
# image tag, the `latest` decision and the tagging, against stand-ins
# for `gh` and `docker`)
- name: Test release-image script
run: bash tests/release-image/test.sh
# release-image: the script's checks and tags against a local
# registry (two images, pushed by digest) and this repository's real
# releases (v1.0.0 has a published release with a lower version than
# the latest one; no version 999.0.0 or higher exists). Attesting and
# signing need `id-token: write` and would publish attestations and
# transparency log entries for test images, so they aren't run here
- name: Push images to a local registry
env:
IMAGE: localhost:5000/release-image-test
run: |
docker run --detach --publish 5000:5000 --name registry registry:3.0.0
# Until the registry accepts requests (at most 30 seconds)
for _ in $(seq 30); do
curl --silent --fail http://localhost:5000/v2/ > /dev/null && break
sleep 1
done
curl --silent --show-error --fail http://localhost:5000/v2/ > /dev/null
for name in a b; do
context="${RUNNER_TEMP}/image-${name}"
mkdir -p "${context}"
echo "${name}" > "${context}/${name}"
# A single image manifest (no attestations, so no index), which
# buildx would wrap in a new index unless told not to
printf 'FROM scratch\nCOPY %s /\n' "${name}" \
| docker build --provenance=false --sbom=false \
--tag "${IMAGE}:build-${name}" --file - "${context}"
docker push "${IMAGE}:build-${name}"
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
type="$(docker buildx imagetools inspect --raw "${IMAGE}:build-${name}" \
| jq -r .mediaType)"
if [[ "${type}" != *manifest.v* ]]; then
echo "::error::Expected a single image manifest, got ${type}"
exit 1
fi
digest="$(docker buildx imagetools inspect "${IMAGE}:build-${name}" \
--format '{{ json .Manifest }}' | jq -r .digest)"
echo "DIGEST_${name^^}=${digest}" >> "${GITHUB_ENV}"
done
- name: Check and tag images in the local registry
env:
GH_TOKEN: ${{ github.token }}
# Not the fork's, which has other releases (public data)
GH_REPO: elixir-cloud-aai/.github
IMAGE: localhost:5000/release-image-test
TAG_PREFIX: v
LATEST: auto
run: |
script=actions/release-image/release.py
# <image tag> <expected digest>
expect_tag() {
local actual
actual="$(docker buildx imagetools inspect "${IMAGE}:$1" \
--format '{{ json .Manifest }}' | jq -r .digest)"
if [[ "${actual}" != "$2" ]]; then
echo "::error::Expected ${IMAGE}:$1 at $2, got ${actual}"
exit 1
fi
}
# The highest release: the version and `latest`
TAG=v999.0.0 DIGEST="${DIGEST_A}" python3 -I "${script}" check
out="$(TAG=v999.0.0 DIGEST="${DIGEST_A}" python3 -I "${script}" tag)"
[[ "${out}" == *"latest=true"* ]] || { echo "::error::${out}"; exit 1; }
expect_tag 999.0.0 "${DIGEST_A}"
expect_tag latest "${DIGEST_A}"
# A re-run with the same digest passes; another digest fails
TAG=v999.0.0 DIGEST="${DIGEST_A}" python3 -I "${script}" check
if out="$(TAG=v999.0.0 DIGEST="${DIGEST_B}" \
python3 -I "${script}" check 2>&1)"; then
echo "::error::Expected another digest of 999.0.0 to fail"
exit 1
fi
[[ "${out}" == *"already points to ${DIGEST_A}"* ]] \
|| { echo "::error::${out}"; exit 1; }
# A lower release gets its version only; `latest` stays
out="$(TAG=v1.0.0 DIGEST="${DIGEST_B}" python3 -I "${script}" tag)"
[[ "${out}" == *"latest=false"* ]] || { echo "::error::${out}"; exit 1; }
expect_tag 1.0.0 "${DIGEST_B}"
expect_tag latest "${DIGEST_A}"
# Never `latest`, even for the highest version
out="$(TAG=v1000.0.0 DIGEST="${DIGEST_B}" LATEST=false GH_REPO='' \
python3 -I "${script}" tag)"
[[ "${out}" == *"latest=false"* ]] || { echo "::error::${out}"; exit 1; }
expect_tag 1000.0.0 "${DIGEST_B}"
expect_tag latest "${DIGEST_A}"
# The action itself, for its wiring (inputs reaching the script): it
# stops at its first step, as 999.0.0 points to another digest,
# before anything is attested or signed
- name: Release an image whose version points to another digest
id: moved-version
continue-on-error: true
uses: ./actions/release-image
with:
image: localhost:5000/release-image-test
digest: ${{ env.DIGEST_B }}
tag: v999.0.0
- name: Expect the release of the moved version to fail
env:
OUTCOME: ${{ steps.moved-version.outcome }}
IMAGE: localhost:5000/release-image-test
run: |
if [[ "${OUTCOME}" != failure ]]; then
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
echo "::error::Expected the release of the moved version to fail, got ${OUTCOME}"
exit 1
fi
# Stopped before tagging: 999.0.0 still points to the first image
actual="$(docker buildx imagetools inspect "${IMAGE}:999.0.0" \
--format '{{ json .Manifest }}' | jq -r .digest)"
if [[ "${actual}" != "${DIGEST_A}" ]]; then
echo "::error::Expected ${IMAGE}:999.0.0 at ${DIGEST_A}, got ${actual}"
exit 1
fi
# scan-image: the pip check (`check-pip.sh`), on made-up reports and
# on reports cut down from real Trivy scans
- name: Test scan-image pip check
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ and pin to the commit of a release:
- [`publish-release`](actions/publish-release/README.md): creates the
GitHub release of a tag with the curated notes followed by GitHub's
generated notes, the organisation's release notes format.
- [`release-image`](actions/release-image/README.md): releases an image
pushed by digest: checks that its version doesn't point to another
digest, attests and signs it, and tags it with the version and, for the
highest stable release, `latest`.
- [`scan-image`](actions/scan-image/README.md): scans Docker images with
Trivy.

Expand All @@ -133,8 +137,9 @@ hooks of `.pre-commit-config.yaml`: YAML syntax and GitHub's schemas (issue
forms, workflows, actions, Dependabot), Markdown style (markdownlint),
secrets (gitleaks), shell scripts (shellcheck) and the workflows
(actionlint). Its `test` job runs the tests of the composite actions
(`tests/<action>/test.sh`; those of `publish-release` and `check-release`
use Python's `unittest`, as their scripts use the standard library only),
(`tests/<action>/test.sh`; those of `publish-release`, `check-release`
and `release-image` use Python's `unittest`, as their scripts use the
standard library only),
the actions themselves and the tests of the release scripts
(`tests/release/test.sh`). Run them locally before you push:

Expand All @@ -148,6 +153,7 @@ bash tests/gate/test.sh
bash tests/labels/test.sh
bash tests/publish-release/test.sh
bash tests/check-release/test.sh
bash tests/release-image/test.sh
bash tests/scan-image/test.sh
bash tests/release/test.sh
```
Expand Down
239 changes: 239 additions & 0 deletions actions/release-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
# Release image

Releases an image that the release workflow pushed by digest (untagged)
and scanned, in the job that publishes it. For the image and the digest,
in this order:

1. **checks** the image name (fully qualified, without tag or digest), the
**digest** (`sha256:` and 64 hex digits), the tag (a release version
after `tag-prefix`, as [check-release](../check-release/README.md)
accepts it), and that the digest exists in the image's repository;
2. fails if the version's image tag (`X.Y.Z`) already points to **another
digest**: published versions never move (e.g., after "Re-run all jobs",
which builds a new digest). A re-run with the same digest passes;
3. **attests** the build provenance of the digest
(`actions/attest-build-provenance`, pushed to the registry);
4. **signs** the digest with cosign (keyless, with the workflow's OIDC
identity); the signature covers every tag of the digest;
5. checks the version's image tag again (another run may have set it in
the meantime), then **tags** the digest `X.Y.Z`, decides whether it is
the highest stable release, and if so tags it `latest` (see
[Latest](#latest)); tags copy the digest as is (no new index around a
single manifest), and each is checked to point to the digest.

The first step fails before anything is attested, signed or tagged.

## Latest

The image gets `latest` by the rule that
[publish-release](../publish-release/README.md) uses for the GitHub
release's "Latest" flag, with the same code (`is_latest` in
`publish.py`): if no published release (drafts don't count) has a higher
stable version than this one, and this one is stable. Stable versions have
no pre-release (`a`, `alpha`, `b`, `beta`, `rc`), so pre-releases never
become `latest`, and a patch of an older line (e.g., `1.2.1` after `1.3.0`)
doesn't move it back. From 1.0.0, a release counts as a pre-release by
GitHub's flag, as for publish-release. The decision is made after `X.Y.Z`
is tagged, from the releases as they are then, so that a re-run of an older
release doesn't move it back either.

From 1.0.0, the image's `latest` and the GitHub release marked "Latest"
are therefore the same release. Below 1.0.0, publish-release makes every
GitHub release a pre-release, which GitHub never marks "Latest"; the image
still gets `latest` for the highest stable version (e.g., `0.22.0`), as the
release workflows of the organisation did before, so that `latest` isn't
stuck before 1.0.0.

The GitHub release of a version is published after its image (in the
`publish` job), so a higher version can be in between: tagged in the
registry, but not released yet. A higher stable version tag of the
repository whose image tag exists counts as released, so that a release of
a lower version that runs in between doesn't take `latest` from it; a
higher tag without an image (e.g., a release that failed before tagging)
doesn't count. Two releases of one image that decide `latest` at the same
time can still both set it, the lower one last: registries have no atomic
"set if unchanged", so release jobs of one image run one at a time (see the
concurrency group in [Usage](#usage)).

With `tag-prefix`, only releases and tags that are the prefix followed
by a version as publish-release reads it count (e.g., `1.2.0` or `2.0`,
not `v1.2.0`; with `tag-prefix: chart-v`, a release `v9.0.0` doesn't keep `chart-v1.2.0`
from `latest`, but `chart-v1.3.0` does). With `latest: false`, the image
never gets `latest` (e.g., for images that operators set by digest, or one
repository with images of several packages), and the GitHub API isn't
read.

## Usage

Pin the commit and version of a
[release](https://github.com/elixir-cloud-aai/.github/releases) of this
repository; Dependabot (`github-actions` ecosystem) in the calling
repository bumps the pin to new releases (see
[RELEASING.md](../../RELEASING.md)). The action runs on GitHub-hosted Linux
runners (it needs Python 3.10 or later as `python3`, `gh`, and Docker with
Buildx for `docker buildx imagetools`) and needs no checkout. The caller
logs in to the registry, and the job needs `contents: read` (the releases
and tags, for `latest`), `id-token: write` (the signing certificate) and
`attestations: write`:

```yaml
env:
# Fully qualified (`docker.io/`), as the attestation needs it
IMAGE_NAME: docker.io/${{ vars.DOCKERHUB_ORG || 'elixircloud' }}/my-app

jobs:
# prepare (check-release), push (by digest), scan (scan-image) ...

release-image:
name: Release image
needs: [prepare, push, scan]
runs-on: ubuntu-latest
environment: release
# One release at a time, so that `latest` is decided and set by one run
# before the next one decides it
concurrency:
group: ${{ github.workflow }}-release-image
cancel-in-progress: false
permissions:
contents: read
id-token: write # Sigstore signing certificates
attestations: write # GitHub artifact attestations
steps:
- name: Log in to Docker Hub
uses: docker/login-action@<commit SHA> # vX.Y.Z
with:
username: ${{ secrets.DOCKERHUB_LOGIN }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Release image
uses: elixir-cloud-aai/.github/actions/release-image@<commit SHA> # vX.Y.Z
with:
image: ${{ env.IMAGE_NAME }}
digest: ${{ needs.push.outputs.digest }}
```

The `push` job passes the digest of `docker/build-push-action` as a job
output (`digest: ${{ steps.push.outputs.digest }}`). Each release runs the
action once per image.

### Matrix jobs

Outputs of a matrix job can't tell its entries apart, so a `push` job with
a matrix of images hands each digest over as an artifact named after its
entry, and the release job, with the same matrix, reads it back. The
action checks the digest's format, so reading the file's first line is
all that is needed:

```yaml
push:
strategy:
matrix:
image: [taskmaster, filer]
steps:
# ... build and push by digest (step `push`) ...
- name: Save image digest
env:
DIGEST: ${{ steps.push.outputs.digest }}
run: echo "${DIGEST}" > "${RUNNER_TEMP}/digest.txt"
- name: Upload image digest
uses: actions/upload-artifact@<commit SHA> # vX.Y.Z
with:
name: digest-${{ matrix.image }}
path: ${{ runner.temp }}/digest.txt
# A re-run of all jobs pushes a new digest, which replaces the
# artifact (and which the action then refuses)
overwrite: true
# Long enough for an approval of the `release` environment, or a
# re-run of failed jobs, days later
retention-days: 7

release-image:
needs: [prepare, push, scan]
strategy:
# A failing entry must not cancel another one between signing and
# tagging
fail-fast: false
matrix:
image: [taskmaster, filer]
# ... environment, concurrency, permissions, login as above ...
steps:
- name: Download image digest
uses: actions/download-artifact@<commit SHA> # vX.Y.Z
with:
name: digest-${{ matrix.image }}
path: ${{ runner.temp }}
- name: Read image digest
id: digest
# The first line only, so that the file can't set other outputs
run: |
read -r digest < "${RUNNER_TEMP}/digest.txt"
echo "digest=${digest}" >> "${GITHUB_OUTPUT}"
- name: Release image
uses: elixir-cloud-aai/.github/actions/release-image@<commit SHA> # vX.Y.Z
with:
image: docker.io/elixircloud/${{ matrix.image }}
digest: ${{ steps.digest.outputs.digest }}
```

In a matrix, the entries share a job-level concurrency group, and a group
holds one running and one pending job: put the entry's name in the group
(e.g., `${{ github.workflow }}-release-image-${{ matrix.image }}`), so that
releases of the same image run one at a time and the entries of one release
don't cancel each other. With or without a matrix, a third release that
queues for the group cancels the pending one: re-run the failed jobs of
the cancelled release (its digest is unchanged, so that is safe).
Independently of the group, the action checks the version's tag again right
before tagging.

Tags with a prefix set `tag-prefix` (e.g., foca-plugins:
`tag-prefix: foca-plugin-tes-data-crypt4gh-v` and `latest: "false"`); the
image tag is the version without the prefix.

## Inputs

- `image` (required): the fully qualified image name, without tag or
digest, e.g., `docker.io/elixircloud/tesk-api` (a registry host with a
`.` or a port, or `localhost`, then lower-case path components).
- `digest` (required): the digest to release, `sha256:` and 64 lower-case
hex digits.
- `tag`: the Git tag of the release; default is the tag of the workflow
run (`github.ref_name`).
- `tag-prefix`: the part of the tag before the version; default `v`. Empty
for tags without a prefix (e.g., `1.2.0`).
- `latest`: `auto` (default) to tag `latest` for the highest stable release
(see [Latest](#latest)), `false` never to.
- `token`: the token for the GitHub API, to read the releases and tags of
the repository for `latest`; default is the job's `GITHUB_TOKEN`, which
needs `contents: read`.

## Outputs

Set only if the image was released:

- `version`: the version, which is the image tag: the Git tag without
`tag-prefix` (e.g., `1.2.0`).
- `latest`: `true` if the image was tagged `latest`, else `false`.
- `tags`: the image references that were tagged, space-separated (e.g.,
`docker.io/elixircloud/tesk-api:1.2.0 docker.io/elixircloud/tesk-api:latest`).

## Tests

The action's script, [`release.py`](release.py), uses the Python standard
library, `gh` and `docker` only, and the version rules of check-release and
publish-release from their scripts (loaded from the actions next to it, so
that the rules can't drift apart). CI tests it on three levels:

- the script's unit tests in
[`tests/release-image/`](../../tests/release-image/) (`unittest`, against
stand-ins for `gh` and `docker`): settings, the version check, the
`latest` rule (also against publish-release's `is_latest`) and the tags;
- the script against a local registry with two images and this
repository's real releases: a new highest version (`latest`), a re-run,
another digest of a published version, a lower version and
`latest: false`, each with the tags it should leave;
- the action itself, once, with a version that points to another digest:
it fails at its first step and leaves the tags as they were.

Attesting and signing aren't run in CI (they need `id-token: write`, and
would publish attestations and transparency log entries of test images);
the first release of a repository with the action, or a dry run in a fork,
covers them.
Loading
Loading