Upload Docker Images to GitHub: A Simple Guide with GitHub Actions
Jack Kweyunga
Posted on September 6, 2024
We are going to create a GitHub action that, when triggered, builds and pushes our application image to the GitHub container registry, making it ready for production or testing. Triggers will be manual or on push.
Who is this for.
Docker enthusiasts
GitHub actions users
DevOps practitioners
The process.
First, create a Dockerfile for your project.
Test your Dockerfile to ensure it works and successfully builds a functional Docker image.
Create the following folder structure at the root of your application. Add a file named docker-image.yml.
my-project
.github
workflows
docker-image.yml
Add the following content to the docker-image.yml file. Change the branch name if it is different from the one you are targeting.
name: ci # Put the name of your choice
on:
workflow_dispatch:
push:
branches:
- "master" # Change to the branch name you are targeting
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }} # takes the name of the repository.
jobs:
build-publish-deploy:
name: build and push docker
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: checkout
uses: actions/checkout@v3
- name: Set up Docker Builds
uses: docker/setup-buildx-action@v2
- name: Login to Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@69f6fc9d46f2f8bf0d5491e4aabe0bb8c6a4678a
with:
images: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=ref,event=pr
type=sha
flavor: |
latest=auto
prefix=
suffix=
- name: Build and push hash tagged image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
cache-to: type=inline
Commit your changes and push them to GitHub.
After that, visit the Actions tab in your GitHub repository to see available and running GitHub actions. GitHub actions are displayed using the name specified in the docker-image.yml file. In our case, we named our action "ci". You can name it whatever you like.
By selecting the "ci" GitHub action, you can manually trigger it at any time to build and push your application Docker images to GitHub's container registry (ghcr.io) as packages.
Why would you do this?
This process automates your Docker builds using GitHub actions. Automating builds is a key step in setting up a complete CI/CD pipeline for your development workflow. CI/CD boosts the performance of any development team by cutting deployment time, reducing lead time, lowering the change failure rate, and enhancing the overall development experience.
All the best 😎
Posted on September 6, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.