Studying Time: 4 mins
Hellooo other folks of the interwebs, on this put up, I can be appearing how I’ve learnt tips on how to automate my docker picture builds the use of a GitHub Motion with more than one branches. So let me provide you with some high-level background, the TDLR is that there’s a procedure at my paintings which is simply too hands-on and we need to take away the palms and unencumber engineering time to be extra productive in different places, thus we will ‘Automate All The Issues!’ Beginning with the construct procedure.
Scope of Venture
These days, the docker construct procedure is a guide step and calls for any person to run about 3 instructions in principle, at the premise that the GitHub repository is already cloned to their system, the next steps are.
Step One : Authenticate to Container Registry
$PatToken = "<insert-github-pat-token-here>"
$PatToken | docker login ghcr.io/<username>/<repository> -u <username> --password-stdin
Step Two : Construct a brand new Docker Symbol from the Docker record
docker construct -t ghcr.io/<username>/<repository>:1.0.0 .
Step 3 : Push stated new picture to the Container Registry
docker push ghcr.io/<username>/<repository>:1.0.0
These days, this complete procedure can take round 10-Quarter-hour, because of the construct picture and applications required for the picture after which add time to the container registry, I may just make like 5 coffees in that point!! – So Sufficient, we will be able to automate this.
The GitHub Repository Breakdown
So for this case, I sought after to check out and be slick and suave and get a divorce the GitHub repository into branches, So have the next.
Department Identify | Department Description |
major | readme and assessment recordsdata |
container-image | Dockerfile |
modules | Automation Modules, Which can get clones into the following yaml record |
NOTE, there may well be higher or extra environment friendly techniques to try this, However figured I’d be told some additional about department regulate on the similar time 🙂
YAML Timeeee
Adequate, So you need to peer the YAML, Earlier than we get to the YAML record let me provide you with an ideal fast rundown of what the motion does.
Step One: Activity Begins
Step Two : Test Out the Department “Container-Symbol”
Step 3 : Test the Contence of Checkout, echo “pwd” and “ls”
Step 4: Get the Present Package deal model from Container Registry (The usage of GitHub API)
Step 5: Build up the model Quantity N+1 (This will likely be progressed afterward – no longer on this put up even though)
Step Six: Log into the GitHub Container Registry
Step Seven: Construct Time (Create Symbol from Dockerfile) after which Push to GHCR
Step 8: Test Symbol has been driven effectively to GHCR (The usage of GitHub API)
Step 9: Replace GitHub Repository Variable for picture for subsequent yaml record reference (The usage of GitHub API)
Step Ten : Put up Cleanup of Github Motion
The YAML
Please word, If you’re going to use this it is very important change the ${{secret.PAT_TOKEN}} worth or create one in your Motion
title: Replace Container Symbol
on:
workflow_dispatch:
push:
branches:
- container-image
time table:
- cron: '0 0 1 */3 *'
jobs:
Update_Container_Image:
runs-on: ubuntu-latest
steps:
- title: Checkout repository
makes use of: movements/checkout@v3
with:
ref: container-image
- title: Test Listing Contents
run: |
pwd
ls
- title: Get Present Package deal Model
run: |
reaction=$(curl -s -H "Settle for: utility/vnd.github.v3+json" -H "Authorization: Bearer ${{ secrets and techniques.PAT_TOKEN }}"
"https://api.github.com/consumer/applications/container/${{ github.match.repository.title }}/variations" | jq '.[0]')
# Verbose
current_version=$(echo "$reaction" | jq -r '.metadata.container.tags[0]')
echo "Present Package deal Model: $current_version"
echo "CURRENT_VERSION=$current_version" >> $GITHUB_ENV
- title: Increment Package deal Model Tag
run: |
current_version=${{ env.CURRENT_VERSION }}
IFS='.' learn -ra version_parts <<< "$current_version"
main="${version_parts[0]}"
minor="${version_parts[1]}"
patch="${version_parts[2]}"
incremented_patch=$((patch + 1))
incremented_version="$main.$minor.$incremented_patch"
echo "Incremented Package deal Model: $incremented_version"
echo "NEXT_VERSION=$incremented_version" >> $GITHUB_ENV
- title: Login to GitHub Container Registry
makes use of: docker/login-action@v2.2.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets and techniques.PAT_TOKEN }}
- title: Construct and Push Up to date Container to GHCR
makes use of: docker/build-push-action@v4
with:
tags: ghcr.io/${{ github.repository }}:${{ env.NEXT_VERSION }}
context: /house/runner/paintings/${{ github.match.repository.title }}/${{ github.match.repository.title }}
record: ./Dockerfile
push: true
env:
github_token: ${{ secrets and techniques.PAT_TOKEN }}
- title: Test Up to date Container Model in GitHub Container Registry
run: |
reaction=$(curl -s -H "Settle for: utility/vnd.github.v3+json" -H "Authorization: Bearer ${{ secrets and techniques.PAT_TOKEN }}"
"https://api.github.com/consumer/applications/container/${{ github.match.repository.title }}/variations" | jq '.[0]')
# Verbose
updated_version=$(echo "$reaction" | jq -r '.metadata.container.tags[0]')
echo "Up to date Package deal Model: $updated_version"
echo "UPDATED_VERSION=$updated_version" >> $GITHUB_ENV
- title: Replace GitHub Variable [CONTAINER_IMAGE]
run: |
echo "ghcr.io/${{ github.repository }}:${{ env.UPDATED_VERSION }}"
curl -X PATCH
-H "Authorization: Bearer ${{ secrets and techniques.PAT_TOKEN }}" -H "Settle for: utility/vnd.github.v3+json"
-d '{"title": "CONTAINER_IMAGE", "worth": "ghcr.io/${{ github.repository }}:${{ env.UPDATED_VERSION }}" }'
"https://api.github.com/repos/${{ github.repository }}/movements/variables/CONTAINER_IMAGE"
Replace GitHub Variable Step
Simply to present some higher context for this level (for individuals who may well be more moderen to Git, YAML and Motion), Within the GitHub Repository I’ve a variable named CONTAINER_IMAGE. This permits for zero-touch updates to the YAML Document for the true deployment steps, As it’s going to simply reference the price and imply I don’t need to manually stay updating issues 🥳
As soon as the GitHub Motion has been both manually brought on – by means of an individual or mechanically in response to a push to the department or the cronjob.

it’s going to replace the variable. simply 👏 like 👏 that 👏
Wrap Up
So this gorgeous a lot covers this v1 liberate of this YAML Document, There’s some development I need to make to model regulate and tagging, however as a read-to-run record which would possibly lend a hand other folks get began in automating a few of their container builds, I’m hoping this is helping.
If you wish to talk about this additional, you’ll in finding me on Twitter as @smoon_lee and till then I shall see you subsequent time 👋