Skip to content

5. Containers and releases ​

Home · Full index

5.1 B01 · Build a pull request image without credentials ​

GitHub Actions · Full workflow. Requires a root Dockerfile. The GitHub-hosted runner has Docker.

yaml
name: Container build check
on:
  pull_request:
    branches: [main]
permissions:
  contents: read
jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false
      - run: docker build --tag app:pr .

Verify. Break the Dockerfile on a scratch PR and confirm that the build fails before merge.

5.2 B02 · Test the running image ​

Linux Bash · Script fragment. Requires Docker and curl. The supplied health service listens on container port 8080 and returns 200 on /healthz. Build app:ci first. An ephemeral loopback port makes concurrent runs independent.

bash
set -euo pipefail
container_id=$(docker run --detach --publish 127.0.0.1::8080 app:ci)
trap 'docker rm -f "$container_id" >/dev/null' EXIT
endpoint=$(docker port "$container_id" 8080/tcp)
if ! curl --fail --silent --show-error \
  --connect-timeout 2 --max-time 3 --retry 8 --retry-delay 1 --retry-all-errors \
  "http://$endpoint/healthz"; then
  docker logs "$container_id"
  exit 1
fi

Verify. Change the path to a missing endpoint. The command fails, emits diagnostics and removes the disposable container.

5.3 B03 · Publish to GHCR only after tests pass ​

GitHub Actions · Full workflow. Requires a root Dockerfile, a committed uv lockfile and pytest in the default dev group. The GHCR package must allow this repository to write. Choose the platform(s) that match your consumers. The sample builds the runner's native architecture.

yaml
name: Test and publish container
on:
  pull_request:
    branches: [main]
  push:
    branches: [main]
permissions:
  contents: read
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false
      - uses: astral-sh/setup-uv@v6
        with:
          enable-cache: false
      - run: uv run --locked --python 3.12 python -m pytest -q
  publish:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    needs: test
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    outputs:
      image: ${{ steps.image.outputs.name }}@${{ steps.build.outputs.digest }}
    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false
      - id: image
        run: printf 'name=ghcr.io/%s\n' "${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
      - uses: docker/setup-buildx-action@v3
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - id: build
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ${{ steps.image.outputs.name }}:${{ github.sha }}
      - env:
          DIGEST_REF: ${{ steps.image.outputs.name }}@${{ steps.build.outputs.digest }}
        run: printf 'Published %s\n' "$DIGEST_REF" >> "$GITHUB_STEP_SUMMARY"

Verify. A PR run skips publishing. A trusted main push with passing tests publishes a SHA-tagged image and reports the digest. The pushed image here is built after unit tests. The capstone smoke-tests the exact local image before pushing it.

5.4 B04 · Publish to Gitea's OCI registry ​

Gitea · Job fragment. Place under jobs in a workflow that runs on main pushes and has a passing test job. Register a dedicated trusted-docker runner that can reach the registry and run Docker. Set REGISTRY_HOST (host and optional port), lowercase REGISTRY_OWNER and REGISTRY_USER as repository variables. Set REGISTRY_TOKEN to a limited package-write credential. Keep untrusted workflows away from this runner and credential. Use TLS on the registry.

yaml
publish:
  needs: test
  if: github.event_name == 'push' && github.ref == 'refs/heads/main'
  runs-on: trusted-docker
  steps:
    - uses: actions/checkout@v4
      with:
        persist-credentials: false
    - name: Build and push
      env:
        REGISTRY_HOST: ${{ vars.REGISTRY_HOST }}
        REGISTRY_OWNER: ${{ vars.REGISTRY_OWNER }}
        REGISTRY_USER: ${{ vars.REGISTRY_USER }}
        REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
      shell: bash
      run: |
        set -euo pipefail
        : "${REGISTRY_HOST:?missing registry host}"
        : "${REGISTRY_OWNER:?missing registry owner}"
        : "${REGISTRY_USER:?missing registry user}"
        : "${REGISTRY_TOKEN:?missing registry token}"
        export DOCKER_CONFIG
        DOCKER_CONFIG=$(mktemp -d)
        trap 'rm -rf -- "$DOCKER_CONFIG"' EXIT
        image="$REGISTRY_HOST/$REGISTRY_OWNER/app:$GITHUB_SHA"
        docker build --tag "$image" .
        printf '%s' "$REGISTRY_TOKEN" |
          docker login "$REGISTRY_HOST" --username "$REGISTRY_USER" --password-stdin
        docker push "$image"

Verify. Confirm the image appears in the package registry under the intended owner. Note that the protected branch and runner access controls must be configured outside the YAML.

5.5 B05 · Build an amd64 and arm64 image ​

GitHub Actions · Steps fragment. Replace the corresponding Buildx steps in the trusted publish job from B03. Retain checkout, registry login and the existing image-name step.

yaml
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- id: build
  uses: docker/build-push-action@v6
  with:
    context: .
    platforms: linux/amd64,linux/arm64
    push: true
    tags: ${{ steps.image.outputs.name }}:${{ github.sha }}

Verify. Run docker buildx imagetools inspect <image>@<digest> with appropriate registry access and confirm both runtime platforms. Test each target architecture before using this as a production release gate.

5.6 B06 · Publish a release from a protected version tag ​

GitHub Actions · Full workflow. Requires a buildable uv project with pytest and permission to create releases. Set a tag ruleset for v* and restrict tag creation and main-branch changes. The reachability check permits a tested commit from the history of main. This example publishes a wheel to GitHub Releases.

yaml
name: Release wheel
on:
  push:
    tags: ['v*']
permissions:
  contents: read
jobs:
  package:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false
          fetch-depth: 0
      - name: Require a version tag on main history
        run: |
          [[ "$GITHUB_REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]
          git merge-base --is-ancestor HEAD origin/main
      - uses: astral-sh/setup-uv@v6
        with:
          enable-cache: false
      - run: uv run --locked --python 3.12 python -m pytest -q
      - run: uv build --python 3.12 --wheel
      - uses: actions/upload-artifact@v4
        with:
          name: release-wheel
          path: dist/*.whl
          if-no-files-found: error
  release:
    needs: package
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: release-wheel
          path: dist
      - env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GH_REPO: ${{ github.repository }}
        run: gh release create "$GITHUB_REF_NAME" dist/*.whl --verify-tag --generate-notes

Verify. Confirm that the version inside the package matches the tag before releasing. A retry after successful release creation reports an existing release. Review the existing release and assets before changing them.

Reference · GitHub container publishing, Gitea container registry, Docker Buildx in GitHub Actions.