How do you build and push Docker images efficiently in GitHub Actions?
IntermediateConceptGitHub Actions
Answer
Use docker/setup-buildx-action, log in with docker/login-action, and docker/build-push-action with the GitHub Actions cache backend for layer caching. docker/metadata-action generates tags and labels.
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with: { registry: ghcr.io, username: '${{ github.actor }}', password: '${{ secrets.GITHUB_TOKEN }}' }
- uses: docker/build-push-action@v6
with:
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
More on GitHub Actions
What are workflows, jobs, steps and actions in GitHub Actions?BeginnerWhat events can trigger a workflow?BeginnerWhat is the difference between `pull_request` and `pull_request_target`?AdvancedWhat is a runner, and how do GitHub-hosted and self-hosted runners differ?BeginnerHow do you pass data between steps and between jobs?IntermediateHow does a build matrix work?Intermediate