GitHub Actions
This guide shows how to use pycontainer-build in GitHub Actions workflows to build and push container images without Docker.
Reusable Workflow
Section titled “Reusable Workflow”pycontainer-build provides a reusable GitHub Actions workflow that makes it easy to integrate container builds into your CI/CD pipeline.
Basic Usage
Section titled “Basic Usage”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: writeThis will:
- Check out your code
- Set up Python 3.11
- Check out and install pycontainer-build from the immutable commit that contains the called workflow
- Build your container image
- 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.
Package releases
Section titled “Package releases”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.
Complete Configuration
Section titled “Complete Configuration”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 }}Use Cases
Section titled “Use Cases”1. Build on Every Commit
Section titled “1. Build on Every Commit”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: write2. Build and Tag Releases
Section titled “2. Build and Tag Releases”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: write3. 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: write4. PR Preview Images (Don’t Push)
Section titled “4. PR Preview Images (Don’t Push)”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: readCustom Workflow (Direct Usage)
Section titled “Custom Workflow (Direct Usage)”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')"Caching Application Layers
Section titled “Caching Application Layers”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.jsonis a clean miss. Entries whose blobs are missing or corrupt are discarded and rebuilt. - A malformed
index.jsonfails the build withCacheIndexError. 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 usually0o644), and Windows checkouts withcore.autocrlfchange file content, so a cross-OS restore misses and rebuilds. Keeprunner.osin 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.
Authentication
Section titled “Authentication”GitHub Container Registry (ghcr.io)
Section titled “GitHub Container Registry (ghcr.io)”The workflow automatically uses GITHUB_TOKEN for authentication to ghcr.io. No additional configuration needed.
Other Registries
Section titled “Other Registries”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 }}Environment Variables
Section titled “Environment Variables”The workflow sets these environment variables automatically:
GITHUB_TOKEN- For ghcr.io authenticationREGISTRY_USERNAME- Custom registry username (if provided)REGISTRY_PASSWORD- Custom registry password (if provided)
Build Summary
Section titled “Build Summary”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
Troubleshooting
Section titled “Troubleshooting”Build Fails: “Module not found”
Section titled “Build Fails: “Module not found””Ensure your dependencies are properly specified:
- Include a
requirements.txtorpyproject.toml - Use
--include-depsflag - Or pre-install dependencies in your workflow
Push Fails: “Authentication required”
Section titled “Push Fails: “Authentication required””Check:
- Workflow has
packages: writepermission - For custom registries,
REGISTRY_USERNAMEandREGISTRY_PASSWORDsecrets are set - Registry URL is correct
Image Size Too Large
Section titled “Image Size Too Large”Optimize your base image:
- Use slim images:
python:3.11-sliminstead ofpython:3.11 - Consider distroless:
gcr.io/distroless/python3-debian12 - Exclude unnecessary files in your project
Benefits Over Docker-Based Builds
Section titled “Benefits Over Docker-Based Builds”- No Docker daemon required - Works in any CI environment
- Faster builds - No Docker layer caching overhead
- Simpler workflows - No Docker setup or Docker-in-Docker
- Better security - No privileged containers needed
- Cloud-native - Works in GitHub Codespaces, Azure DevOps, etc.
Next Steps
Section titled “Next Steps”- See example-build.yml.example for complete examples
- Read Configuration for all build options
- Explore the roadmap for planned work