Optimize Your CI/CD Pipeline
Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.
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