GitHub Action to Publish Docker Images on Harbor

Backend Sorcery & Finance Geeking
Search for a command to run...

Backend Sorcery & Finance Geeking
No comments yet. Be the first to comment.
In this series I've discussed about how I set up a custom CI/CD Pipeline using GitHub Actions, ArgoCD, Harbor Container Registry, Kubernetes, NGINX, Cloudflare
This pipeline is implemented in the backend system of ChaturMail: AI Email Generator Understanding Overall Pipeline Flow Code changes are pushed to the master branch on GitHub The repo has a Dockerfile. GitHub Actions does the following tasks Bui...
I joined Underdog Protocol as a developer in October 2023, after attending the Solana HackerHouses in Bangalore and Mumbai. Before that, I had worked on a contract basis and as a mentee under the LFX Mentorship throughout the year. This blog is about...

Create Short Name (SNS) for your Solana Public Key

We did so you don't have to

Rusly is a URL shortener built using the Rocket framework in Rust. Checkout RuslyCheckout the Rusly GitHub repo This blog lays out the system design thought process I used while designing and developing the system. Read about the Rust syntactical dev...

If you're willing to contribute to Harbor and/or need its code up & running on your local dev environment then follow along, this guide will get you started. Harbor is an open-source registry that secures artifacts with policies and role-based access...

This task is part of the Custom CI/CD Pipeline for the application ChaturMail: AI Email Generator
The GitHub action does the following tasks:
Builds & Pushes Docker Image to Harbor
Update ArgoCD Config
tl;dr following is the complete YAML for the action followed by an explanation
name: BuildAndPushImageOnHarborAndUpdateArgoCDConfig
on:
push:
branches: [ "master" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: docker/login-action@v1
with:
registry: harbor.example.com
username: ${{ secrets.HARBOR_USERNAME }}
password: ${{ secrets.HARBOR_PASSWORD }}
- uses: actions/checkout@v3
- name: BuildAndPushImageOnHarbor
run: |
docker build ./ -t harbor.example.com/chaturmail/chaturmail-backend:${{ github.run_number }}
docker push harbor.example.com/chaturmail/chaturmail-backend:${{ github.run_number }}
- name: Clone Repository
run: |
git clone <argocd-config-repo-url>
- name: Install yq
run: |
sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
sudo chmod a+x /usr/local/bin/yq
- name: Update YAML File
run: |
yq -i '.spec.template.spec.containers[0].image = "harbor.example.com/chaturmail/chaturmail-backend:${{ github.run_number }}"' 'argocd-configs/chaturmail-pod.yaml'
- name: Push to Repo
run: |
git config --global user.name "${{secrets.USERNAME_GITHUB}}"
git config --global user.email "${{secrets.EMAIL_GITHUB}}"
cd argocd-test-configs
git add .
git commit -m "Updated by GitHub Actions"
git push <argocd-config-repo-url> --all
Harbor is self-hosted on a VPS server and is exposed as a registry.
Login is done using the docker login action. The registry URI, username, and password need to be provided. Since Harbor is a docker registry the login works the same as for Docker Hub.
- uses: docker/login-action@v1
with:
registry: harbor.example.com
username: ${{ secrets.HARBOR_USERNAME }}
password: ${{ secrets.HARBOR_PASSWORD }}
Shell commands are executed to update and push.
The image is tagged with the desired string which is of the format<registry-uri>/<harbor-project-name>/<image-tag>:<github.run.number>
The github.run_number is added to keep the tags unique for each image.
Since the action runs on the master branch, the directory is specified as ./
- uses: actions/checkout@v3
- name: BuildAndPushImageOnHarbor
run: |
docker build ./ -t harbor.example.com/chaturmail/chaturmail-backend:${{ github.run_number }}
docker push harbor.example.com/chaturmail/chaturmail-backend:${{ github.run_number }}
YAML config for ArgoCD to execute is maintained in a GitHub repo. ArgoCD watches the repo. Any changes to the master branch of the config repo trigger a deployment on the server.
The action updates the image name in the YAML config. ArgoCD detects this change and does a deployment using the config file with the new image name.
A shell utility yq is used to edit YAML.
- name: Clone Repository
run: |
git clone <argocd-config-repo-url>
- name: Install yq
run: |
sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
sudo chmod a+x /usr/local/bin/yq
- name: Update YAML File
run: |
yq -i '.spec.template.spec.containers[0].image = "harbor.example.com/chaturmail/chaturmail-backend:${{ github.run_number }}"' 'argocd-configs/chaturmail-pod.yaml'
Shell commands to login into GitHub, add changes to git staging, commit & push
- name: Push to Repo
run: |
git config --global user.name "${{secrets.USERNAME_GITHUB}}"
git config --global user.email "${{secrets.EMAIL_GITHUB}}"
cd chaturmail-argocd-configs
git add .
git commit -m "Updated by GitHub Actions"
git push <argocd-config-repo-url> --all
That's all. Check out the pipeline series of blogs.
Check out ChaturMail: AI Email Generator
Keep your secrets safe :-)