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
27 changes: 22 additions & 5 deletions .github/workflows/container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ jobs:
PR_NUMBER: ${{ github.event.pull_request.number }}
TRACK: ${{ matrix.track }}
COMMIT_SHA: ${{ github.sha }}
IMAGE_VERSION: ${{ matrix.image-version }}
PLATFORMS: ${{ matrix.platforms }}
run: |
image="ghcr.io/${IMAGE_REPOSITORY,,}-pr"
tag="pr-${PR_NUMBER}-${TRACK}"
{
echo "image=$image"
echo "package-version=${IMAGE_VERSION}-${tag}-${COMMIT_SHA:0:7}"
echo "platform=${PLATFORMS%%,*}"
echo 'tags<<PR_IMAGE_TAGS'
echo "$image:$tag"
Expand Down Expand Up @@ -146,6 +148,7 @@ jobs:
platforms: ${{ matrix.platforms }}
build-args: |
${{ matrix.build-args }}
PACKAGE_VERSION=${{ steps.preview.outputs.package-version }}
BUILDKIT_DOCKERFILE_CHECK=skip=InvalidDefaultArgInFrom
pull: true
outputs: type=oci,dest=${{ runner.temp }}/container-${{ matrix.track }}.tar
Expand All @@ -160,7 +163,7 @@ jobs:
labels: |
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.version=${{ matrix.image-version }}
org.opencontainers.image.version=${{ steps.preview.outputs.package-version }}
# Check current package availability on every architecture, including
# reruns after an upstream repository finishes publishing a version.
no-cache: true
Expand All @@ -172,6 +175,7 @@ jobs:
PLATFORMS: ${{ matrix.platforms }}
APP_VERSION: ${{ matrix.version }}
TRACK: ${{ matrix.track }}
PACKAGE_VERSION: ${{ steps.preview.outputs.package-version }}
run: '"$RUNNER_TEMP/container-artifact" test "$RUNNER_TEMP/container-$TRACK.tar" "$PLATFORMS" "$APP_VERSION"'
- name: Generate and validate synthetic release metadata
env:
Expand Down Expand Up @@ -374,6 +378,17 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends skopeo
- name: Reserve revision and create draft release before building
id: release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
APP_VERSION: ${{ matrix.version }}
IMAGE_VERSION: ${{ matrix.image-version }}
TRACK: ${{ matrix.track }}
TAGS: ${{ matrix.tags }}
PRERELEASE: ${{ matrix.prerelease }}
IMAGE: ${{ steps.metadata.outputs.image }}
run: '"$RUNNER_TEMP/container-release" reserve'
- name: Build all platforms into a local OCI archive
id: build
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7
Expand All @@ -383,6 +398,7 @@ jobs:
platforms: ${{ matrix.platforms }}
build-args: |
${{ matrix.build-args }}
PACKAGE_VERSION=${{ steps.release.outputs.package-version }}
BUILDKIT_DOCKERFILE_CHECK=skip=InvalidDefaultArgInFrom
# Pull alone does not refresh a cached apt-get layer. Every release
# must actually install the currently available security updates.
Expand All @@ -396,14 +412,15 @@ jobs:
labels: |
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.version=${{ matrix.image-version }}
org.opencontainers.image.version=${{ steps.release.outputs.package-version }}
cache-to: type=gha,mode=max,scope=container-${{ matrix.track }}
- name: Test every platform from the built archive
id: tested
env:
PLATFORMS: ${{ matrix.platforms }}
APP_VERSION: ${{ matrix.version }}
TRACK: ${{ matrix.track }}
PACKAGE_VERSION: ${{ steps.release.outputs.package-version }}
run: '"$RUNNER_TEMP/container-artifact" test "$RUNNER_TEMP/container-$TRACK.tar" "$PLATFORMS" "$APP_VERSION"'
- name: Validate final image OS metadata before upload
env:
Expand Down Expand Up @@ -431,8 +448,7 @@ jobs:
IMAGE: ${{ steps.metadata.outputs.image }}
TRACK: ${{ matrix.track }}
run: '"$RUNNER_TEMP/container-artifact" publish "$RUNNER_TEMP/container-$TRACK.tar" "$IMAGE"'
- name: Reserve revision and create draft release
id: release
- name: Finalize reserved draft with tested artifact metadata
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
APP_VERSION: ${{ matrix.version }}
Expand All @@ -443,7 +459,8 @@ jobs:
IMAGE: ${{ steps.metadata.outputs.image }}
IMAGE_DIGEST: ${{ steps.tested.outputs.digest }}
PLATFORMS: ${{ matrix.platforms }}
run: '"$RUNNER_TEMP/container-release" reserve'
RELEASE_TAG: ${{ steps.release.outputs.tag }}
run: '"$RUNNER_TEMP/container-release" finalize'
- name: Attach machine-readable release metadata
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
87 changes: 87 additions & 0 deletions cmd/container-release/package_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"os"
"strings"
"testing"

"go.yaml.in/yaml/v3"
)

func TestWorkflowPackageVersionAndRevisionLabels(t *testing.T) {
data, err := os.ReadFile("../../.github/workflows/container.yml")
if err != nil {
t.Fatal(err)
}
var workflow struct {
Jobs map[string]struct {
Steps []struct {
ID string `yaml:"id"`
Run string `yaml:"run"`
Uses string `yaml:"uses"`
Env map[string]string `yaml:"env"`
With map[string]string `yaml:"with"`
} `yaml:"steps"`
} `yaml:"jobs"`
}
if err := yaml.Unmarshal(data, &workflow); err != nil {
t.Fatal(err)
}
for _, job := range []string{"pull-request", "release"} {
reserve, build, test, upload, finalize, manifest, tags := -1, -1, -1, -1, -1, -1, -1
packageVersion := "${{ steps.release.outputs.package-version }}"
if job == "pull-request" {
packageVersion = "${{ steps.preview.outputs.package-version }}"
}
for i, s := range workflow.Jobs[job].Steps {
if strings.Contains(s.Run, `container-release" reserve`) {
reserve = i
if s.ID != "release" || s.Env["IMAGE_DIGEST"] != "" {
t.Fatal("reservation must run before the build has a digest")
}
}
if strings.HasPrefix(s.Uses, "docker/build-push-action@") {
build = i
if !strings.Contains(s.With["build-args"], "PACKAGE_VERSION="+packageVersion) {
t.Fatalf("%s is missing the package version build argument", job)
}
for _, label := range []string{"org.opencontainers.image.version=" + packageVersion, "org.opencontainers.image.revision=${{ github.sha }}"} {
if !strings.Contains(s.With["labels"], label+"\n") {
t.Fatalf("%s is missing label %s", job, label)
}
}
}
if s.ID == "tested" {
test = i
if s.Env["PACKAGE_VERSION"] != packageVersion {
t.Fatalf("%s must give smoke tests the exact built package version", job)
}
}
if strings.Contains(s.Run, `container-artifact" publish`) {
upload = i
}
if strings.Contains(s.Run, `container-release" finalize`) {
finalize = i
if s.Env["RELEASE_TAG"] != "${{ steps.release.outputs.tag }}" || s.Env["IMAGE_DIGEST"] != "${{ steps.tested.outputs.digest }}" {
t.Fatal("finalization must use the reserved tag and tested digest")
}
}
if strings.Contains(s.Run, `container-release" manifest`) {
manifest = i
}
if s.Env["IMAGE_TAGS"] == "${{ steps.release.outputs.tags }}" {
tags = i
}
}
if build < 0 || test < 0 {
t.Fatalf("%s is missing builds or tests", job)
}
if job == "pull-request" {
if reserve != -1 || finalize != -1 {
t.Fatal("PRs must never reserve or finalize production revisions")
}
} else if !(reserve >= 0 && reserve < build && build < test && test < upload && upload < finalize && finalize < manifest && manifest < tags) {
t.Fatalf("production must reserve -> build -> test -> upload -> finalize -> manifest -> tags; got %d %d %d %d %d %d %d", reserve, build, test, upload, finalize, manifest, tags)
}
}
}
6 changes: 3 additions & 3 deletions cmd/container-release/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,15 @@ func TestWorkflowPRImages(t *testing.T) {
switch {
case s.ID == "preview":
prepared = true
if s.Env["IMAGE_REPOSITORY"] != "${{ github.repository }}" || s.Env["PR_NUMBER"] != "${{ github.event.pull_request.number }}" || s.Env["TRACK"] != "${{ matrix.track }}" || s.Env["COMMIT_SHA"] != "${{ github.sha }}" || s.Env["PLATFORMS"] != "${{ matrix.platforms }}" {
if s.Env["IMAGE_REPOSITORY"] != "${{ github.repository }}" || s.Env["PR_NUMBER"] != "${{ github.event.pull_request.number }}" || s.Env["TRACK"] != "${{ matrix.track }}" || s.Env["COMMIT_SHA"] != "${{ github.sha }}" || s.Env["PLATFORMS"] != "${{ matrix.platforms }}" || s.Env["IMAGE_VERSION"] != "${{ matrix.image-version }}" {
t.Fatal("preview tags must use caller repository, PR number, track, and tested merge commit")
}
for _, track := range []string{"stable", "preview"} {
t.Run(track, func(t *testing.T) {
output := filepath.Join(t.TempDir(), "output")
sha := strings.Repeat("a", 40)
cmd := exec.Command("bash", "-e", "-o", "pipefail", "-c", s.Run)
cmd.Env = append(os.Environ(), "IMAGE_REPOSITORY=Tuxcrate/Zrepl", "PR_NUMBER=12", "TRACK="+track, "COMMIT_SHA="+sha, "GITHUB_OUTPUT="+output, "PLATFORMS=linux/arm64,linux/amd64")
cmd.Env = append(os.Environ(), "IMAGE_REPOSITORY=Tuxcrate/Zrepl", "PR_NUMBER=12", "TRACK="+track, "COMMIT_SHA="+sha, "GITHUB_OUTPUT="+output, "PLATFORMS=linux/arm64,linux/amd64", "IMAGE_VERSION=1.2.3")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("prepare tags: %v: %s", err, out)
}
Expand All @@ -398,7 +398,7 @@ func TestWorkflowPRImages(t *testing.T) {
}
image := "ghcr.io/tuxcrate/zrepl-pr"
tag := "pr-12-" + track
want := "image=" + image + "\nplatform=linux/arm64\ntags<<PR_IMAGE_TAGS\n" + image + ":" + tag + "\n" + image + ":" + tag + "-" + sha + "\nPR_IMAGE_TAGS\n"
want := "image=" + image + "\npackage-version=1.2.3-" + tag + "-" + sha[:7] + "\nplatform=linux/arm64\ntags<<PR_IMAGE_TAGS\n" + image + ":" + tag + "\n" + image + ":" + tag + "-" + sha + "\nPR_IMAGE_TAGS\n"
if string(got) != want {
t.Fatalf("unexpected preview tags: %s", got)
}
Expand Down
4 changes: 3 additions & 1 deletion internal/containerrelease/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func (a app) execute(args []string) error {
return a.metadata()
case "reserve":
return a.reserve()
case "finalize":
return a.finalize()
case "plan":
return a.plan()
case "release-plan":
Expand All @@ -29,5 +31,5 @@ func (a app) execute(args []string) error {
return a.checkManifest()
}
}
return errors.New("usage: container-release {plan|release-plan|metadata|reserve|manifest|check-manifest}")
return errors.New("usage: container-release {plan|release-plan|metadata|reserve|finalize|manifest|check-manifest}")
}
Loading