πŸ’ΈSave up to $132K/month in CI costs!πŸ‘‰ Try Free✨
Skip to main content
← Back to workflows

How to Publish Docker Containers with GitHub Action

elgohr-Publish-Docker-Github-Action -
GitHub Action
v5
780
Contributors
Contributor - elgohr
Categories

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.

45% Faster Builds
60% Cost Reduction
Usage
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-action logo

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