bashintermediate
GitHub Actions Docker Build and Push
GitHub Actions workflow to build a Docker image and push it to GitHub Container Registry on release.
bashPress ⌘/Ctrl + Shift + C to copy
# .github/workflows/docker-publish.yml
name: Docker Build & Push
on:
push:
tags: ['v*']
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/metadata-action@v5
id: meta
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha
- uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=maxUse Cases
- Automated Docker image publishing on release
- Versioned container images with semantic tags
- CI/CD pipeline for containerized applications
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
yamlintermediate
GitHub Actions — Docker Build and Publish
Build multi-platform Docker images and push to GitHub Container Registry with caching and tagging.
Best for: Publishing Docker images on release tags
#github-actions#docker
bashintermediate
GitHub Actions CI/CD Pipeline
Complete GitHub Actions workflow with test, build, and deploy stages for a Node.js application.
Best for: Automated testing and deployment on push
#github-actions#ci-cd
bashintermediate
GitHub Actions Test Matrix Strategy
GitHub Actions workflow with matrix strategy to test across multiple Node versions and OS platforms.
Best for: Cross-platform compatibility testing
#github-actions#testing
yamlbeginner
GitHub Actions — Node.js CI Pipeline
Complete CI workflow for Node.js: install, lint, test, build on every push and PR with caching.
Best for: Automated testing on every push and PR
#github-actions#ci-cd