How do I set up GitHub Actions to automatically create and publish Docker images to GHCR on new tags? #191075
Replies: 3 comments
-
|
To publish Docker images to GitHub Container Registry (GHCR) automatically on new tags, create a workflow in .github/workflows/docker-publish.yml that triggers on push: tags: ['v*']. Use GITHUB_TOKEN for authentication by adding permissions: { packages: write, contents: read } to the job, then log in with docker/login-action (registry: ghcr.io, username: ${{ github.actor }}, password: ${{ secrets.GITHUB_TOKEN }}). Finally, use docker/build-push-action to build from your Dockerfile context, tagging as ghcr.io/${{ github.repository }}:${{ github.ref_name }} and optionally :latest with push: true. Enable "Inherit access from repository" in repo settings > Packages, and test by pushing a v1.0.0 tag multi-platform builds work by adding platforms: linux/amd64,linux/arm64 |
Beta Was this translation helpful? Give feedback.
-
|
To automate the process of building and publishing Docker images to the GitHub Container Registry (GHCR), you can use a GitHub Actions workflow triggered by tags. This is the standard way to handle versioned releases. Prerequisites Write permissions for packages (enabled by default for the GITHUB_TOKEN in most repositories). The Workflow Configuration YAML on: env: jobs: Key Components Explained Permissions: The packages: write permission is crucial. It allows the GITHUB_TOKEN (automatically generated for the job) to upload the image to your repository's registry. Docker Metadata Action: This is a "quality of life" step. It automatically generates tags for your image. If you push tag v1.2.3, it creates an image tagged ghcr.io/username/repo:1.2.3 and adds a unique SHA tag. Security: Using ${{ secrets.GITHUB_TOKEN }} ensures you don't have to manually create or manage Personal Access Tokens (PATs) for internal registry access. How to trigger it Bash |
Beta Was this translation helpful? Give feedback.
-
|
Here’s a minimal, production-ready workflow that builds and pushes Docker images to GHCR on new tags, using name: Publish to GHCR
on:
push:
tags:
- 'v*' # Triggers on tags |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
🏷️ Discussion Type
Question
💬 Feature/Topic Area
Code Search and Navigation
Body
I'm trying to containerize my Python app and push images to GitHub Container Registry (GHCR) via Actions, but I'm stuck on authentication and tagging. What's the minimal workflow YAML for building on push tags (e.g., v1.0.0), tagging as owner/repo:tag, and using GITHUB_TOKEN? Any gotchas with multi-platform builds or repo permissions?
Beta Was this translation helpful? Give feedback.
All reactions