Skip to content

GitHub Actions

This guide shows how to use pycontainer-build in GitHub Actions workflows to build and push container images without Docker.

pycontainer-build provides a reusable GitHub Actions workflow that makes it easy to integrate container builds into your CI/CD pipeline.

name: Build Container
on:
push:
branches: [main]
jobs:
build:
uses: spboyer/pycontainer-build/.github/workflows/pycontainer-build.yml@main
with:
tag: ghcr.io/${{ github.repository }}:${{ github.sha }}
push: true
permissions:
contents: read
packages: write

This will:

  1. Check out your code
  2. Set up Python 3.11
  3. Check out and install pycontainer-build from the immutable commit that contains the called workflow
  4. Build your container image
  5. Push to GitHub Container Registry (ghcr.io)

The reusable workflow deliberately keeps its immutable source install rather than using PyPI. It uses GitHub’s job.workflow_repository and job.workflow_sha values to install from the exact commit serving the workflow. Pin the uses reference to a commit or release tag when you also need a fixed workflow definition. Pinned Node 24 actions require Actions Runner 2.327.1 or later; the workflow’s ubuntu-latest runner satisfies this requirement.

The MIT-licensed core is published on v*.*.* tags via PyPI Trusted Publishing, starting with v0.4.0. The tag must exactly equal v plus [project].version; a mismatch fails before building or publishing. Released installs use pip install pycontainer-build or uv tool install pycontainer-build. Plugin publication is separate.

The release workflow runs hermetic tests, builds wheel and sdist once, checks their metadata with Twine, and smoke-installs those exact archives in clean environments. Only the resulting dist artifact is downloaded for the GitHub release and PyPI upload; the publishing job never rebuilds distributions. Native amd64 and arm64 runtime evidence must pass and be re-verified for the current commit, run and attempt before the GitHub release is created.

Ordering: GitHub release creation precedes PyPI publication. The final publish-pypi job depends on tests, distribution smoke checks, the runtime gate and the GitHub release, then waits for the protected pypi environment approval. A GitHub release alone is not proof of PyPI availability: confirm that the publishing job succeeded and both files and attestations appear on PyPI. This keeps failures in GitHub release creation ahead of immutable PyPI uploads.

The owner must register the PyPI pending trusted publisher with project pycontainer-build, GitHub owner spboyer, repository pycontainer-build, workflow filename release.yml, and environment pypi (case-sensitive). The GitHub environment requires owner review and permits only v*.*.* tags. No API token or PYPI_API_TOKEN secret is used. Only the publishing job receives id-token: write; only the GitHub release job receives contents: write. The pinned publisher action is v1.14.3 (including its certificate-security fix); its Linux/composite-action requirements and PEP 740 attestation inputs were reviewed against its release notes and action definition. Attestations are explicitly enabled.

Recovery: Never move a tag or blindly retry a PyPI upload. There is no skip-existing bypass: an existing filename fails rather than concealing a different artifact. If failure occurred before any PyPI upload, correct external publisher/approval settings and re-run all jobs, not failed jobs alone, so runtime evidence matches the new attempt. Build artifacts replace the earlier attempt’s artifact. If any upload succeeded or its outcome is uncertain, inspect PyPI’s file list, SHA-256 digests and attestations against the downloaded dist artifact first. Do not delete/re-upload files or replay the workflow. Preserve the run and artifacts for investigation; an incomplete or incorrect upload requires an owner-reviewed new patch version and new tag. If both files and attestations are present but the job lost its response, record that verified outcome rather than uploading again.

jobs:
build:
uses: spboyer/pycontainer-build/.github/workflows/pycontainer-build.yml@main
with:
# Python version for the build environment
python-version: '3.11'
# Container image tag (required)
tag: 'ghcr.io/myorg/myapp:v1.0.0'
# Base image for the container
base-image: 'python:3.11-slim'
# Build context directory
context: '.'
# Push to registry (true/false)
push: true
# Preview without pulling images or writing output
dry-run: false
# Include dependencies from venv/requirements.txt
include-deps: true
# Generate SBOM (spdx or cyclonedx)
sbom: 'spdx'
# Container registry
registry: 'ghcr.io'
secrets:
# Optional: Custom registry credentials
# If not provided, uses GITHUB_TOKEN for ghcr.io
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
name: Build Container
on:
push:
branches: [main, develop]
jobs:
build:
uses: spboyer/pycontainer-build/.github/workflows/pycontainer-build.yml@main
with:
tag: ghcr.io/${{ github.repository }}:${{ github.sha }}
push: true
permissions:
contents: read
packages: write
name: Release Container
on:
release:
types: [published]
jobs:
build:
uses: spboyer/pycontainer-build/.github/workflows/pycontainer-build.yml@main
with:
tag: ghcr.io/${{ github.repository }}:${{ github.event.release.tag_name }}
base-image: 'python:3.11-slim'
include-deps: true
sbom: 'spdx'
push: true
permissions:
contents: read
packages: write

3. Matrix Builds (Multiple Python Versions)

Section titled “3. Matrix Builds (Multiple Python Versions)”
name: Multi-Version Build
on:
push:
branches: [main]
jobs:
build:
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']
uses: spboyer/pycontainer-build/.github/workflows/pycontainer-build.yml@main
with:
python-version: ${{ matrix.python-version }}
tag: ghcr.io/${{ github.repository }}:py${{ matrix.python-version }}
base-image: python:${{ matrix.python-version }}-slim
push: true
permissions:
contents: read
packages: write
name: PR Build
on:
pull_request:
jobs:
build:
uses: spboyer/pycontainer-build/.github/workflows/pycontainer-build.yml@main
with:
tag: ghcr.io/${{ github.repository }}:pr-${{ github.event.pull_request.number }}
push: false # Just build, don't push
permissions:
contents: read

If you need more control, you can use pycontainer-build directly:

name: Custom Build
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Checkout a verified pycontainer-build source commit
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: spboyer/pycontainer-build
ref: d3ee7e0f5a1f342a42f9a43ad4efd335ec0497b7
path: .pycontainer-build
persist-credentials: false
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: '3.11'
- name: Install pycontainer-build
run: python -m pip install --no-deps ./.pycontainer-build
- name: Build and push container
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
pycontainer build \
--tag ghcr.io/${{ github.repository }}:${{ github.sha }} \
--base-image python:3.11-slim \
--include-deps \
--sbom spdx \
--push \
--verbose
- name: Run tests on container (optional)
run: |
# Example: pull and test the image
# Note: Requires Docker for testing, but not for building
docker pull ghcr.io/${{ github.repository }}:${{ github.sha }}
docker run ghcr.io/${{ github.repository }}:${{ github.sha }} python -c "import myapp; print('OK')"

The --cache-dir directory is the supported offline cache export/import path: save it with actions/cache (or any archive tool) and restore it in a later job. No separate export command or archive format is involved.

env:
# The pycontainer-build commit or release tag installed by this job.
PYCONTAINER_REF: d3ee7e0f5a1f342a42f9a43ad4efd335ec0497b7
steps:
# ... check out, set up Python and install pycontainer-build at PYCONTAINER_REF ...
- name: Restore the pycontainer layer cache
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ runner.temp }}/pycontainer-cache
# Caches are not portable across operating systems, and the cache key
# format may change between pycontainer-build versions.
key: pycontainer-${{ runner.os }}-${{ env.PYCONTAINER_REF }}-${{ hashFiles('**/*.py', 'pyproject.toml', 'requirements.txt') }}
restore-keys: |
pycontainer-${{ runner.os }}-${{ env.PYCONTAINER_REF }}-
- name: Build container
run: pycontainer build --tag myapp:${{ github.sha }} --cache-dir "${{ runner.temp }}/pycontainer-cache"

What to expect after a restore:

  • The cache stores relative source paths only, so a different runner workspace or cache path still hits and produces an identical OCI manifest, config and layers.
  • A restore with blobs but no index.json is a clean miss. Entries whose blobs are missing or corrupt are discarded and rebuilt.
  • A malformed index.json fails the build with CacheIndexError. Delete the reported file (the cached layers are rebuilt) or rerun with --no-cache.
  • Parallel builds can share a restored directory; index updates are locked.
  • Linux and Windows caches are not interchangeable. Source permission bits are part of the cache key and the layer (Windows reports 0o666, Linux usually 0o644), and Windows checkouts with core.autocrlf change file content, so a cross-OS restore misses and rebuilds. Keep runner.os in the key.
  • A partial restore key (restore-keys) reuses unchanged layers; stale blobs that no longer have an index entry are not evicted, so let the cache key roll over periodically to keep the saved directory small.

The workflow automatically uses GITHUB_TOKEN for authentication to ghcr.io. No additional configuration needed.

For Docker Hub, Azure Container Registry, or private registries:

jobs:
build:
uses: spboyer/pycontainer-build/.github/workflows/pycontainer-build.yml@main
with:
tag: 'myregistry.azurecr.io/myapp:latest'
registry: 'myregistry.azurecr.io'
push: true
secrets:
REGISTRY_USERNAME: ${{ secrets.ACR_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.ACR_PASSWORD }}

The workflow sets these environment variables automatically:

  • GITHUB_TOKEN - For ghcr.io authentication
  • REGISTRY_USERNAME - Custom registry username (if provided)
  • REGISTRY_PASSWORD - Custom registry password (if provided)

The workflow generates a build summary in the GitHub Actions UI showing:

  • Image tag
  • Base image used
  • Python version
  • Build context
  • Whether the image was pushed
  • SBOM generation status

Ensure your dependencies are properly specified:

  • Include a requirements.txt or pyproject.toml
  • Use --include-deps flag
  • Or pre-install dependencies in your workflow

Check:

  • Workflow has packages: write permission
  • For custom registries, REGISTRY_USERNAME and REGISTRY_PASSWORD secrets are set
  • Registry URL is correct

Optimize your base image:

  • Use slim images: python:3.11-slim instead of python:3.11
  • Consider distroless: gcr.io/distroless/python3-debian12
  • Exclude unnecessary files in your project
  1. No Docker daemon required - Works in any CI environment
  2. Faster builds - No Docker layer caching overhead
  3. Simpler workflows - No Docker setup or Docker-in-Docker
  4. Better security - No privileged containers needed
  5. Cloud-native - Works in GitHub Codespaces, Azure DevOps, etc.