name: Publish Docker
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
Publish Docker Containers
GitHub Action to build and push Docker containers using the Git branch as the Docker tag.
This GitHub Action allows you to build and push Docker containers automatically. It uses the Git branch as the Docker tag, making the master branch the “latest” tag by default. This makes it easy to handle container versioning and automate the process of publishing to a Docker registry.
How to Push Docker Images to a Custom Registry
You can push images to a custom registry by specifying the registry
option. For example, when pushing to GitHub Container Registry:
with:
name: owner/repository/image
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: ghcr.io
How to Use Snapshot Tagging in Docker Builds
The snapshot
option allows you to push an additional image tagged with a unique timestamp and Git SHA. This helps avoid overwriting older builds with the same SHA, particularly when external dependencies are involved.
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
snapshot: true
How to Use a Different Default Branch for Docker Builds
If you're using a branch other than master
as the default, you can specify it using the default_branch
option. This allows for proper tagging of images when pushing from branches like trunk
or main
.
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
default_branch: trunk
How to Build with a Specific Dockerfile
If you have multiple Dockerfiles and want to explicitly build a specific one, use the dockerfile
option to specify the file's name.
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
dockerfile: MyDockerFileName
How to Use a Different Working Directory for Docker Builds
If your project is located in a subdirectory, you can change the working directory for building the image by using the workdir
option.
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
workdir: mySubDirectory
How to Change the Docker Build Context
The context
option allows you to change the Docker build context, which is useful when your build files are located in a different directory.
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
context: myContextDirectory
How to Pass Build Arguments to Docker
The buildargs
option lets you pass a list of environment variables as build arguments. These variables are masked in logs for security.
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
env:
MY_FIRST: variableContent
MY_SECOND: variableContent
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
buildargs: MY_FIRST,MY_SECOND
How to Use Additional Build Options in Docker
You can configure additional build options using the buildoptions
argument. For example, you can compress images or remove intermediate containers with specific flags.
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
buildoptions: "--compress --force-rm"
How to Build Multi-Platform Docker Images
To build multi-platform Docker images, you can specify target architectures with the platforms
option. This is especially useful for ensuring compatibility across different hardware setups.
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
platforms: linux/amd64,linux/arm64
Monitoring GitHub Actions Workflows
CICube is a GitHub Actions monitoring tool that provides you with detailed insights into your workflows to further optimize your CI/CD pipeline. With CICube, you will be able to track your workflow runs, understand where the bottlenecks are, and tease out the best from your build times. Go to cicube.io now and create a free account to better optimize your GitHub Actions workflows!