Skip to content

GitHub Actions CI Workflow: Build Caching and Image Builds

Interactive Diagram

μƒˆ νƒ­ β†—

To optimize the Application build process and improve performance, there are a few options:

  • GitHub Actions native caching
  • Persistent Volume-based caching on a Self-Hosted Runner
  • Container Registry image-based caching

In this project we wanted to unify the CI pipeline’s container image build process on a single approach and apply it consistently to both Backend and Frontend. We didn’t consider the caching offered by Public GitHub Actions because of its resource limits, and we avoided a dedicated Volume for caching to keep Self-Hosted Runner management to a minimum. For those reasons, we went with Container Registry image-based caching.

The Container image’s cache layer is stored in the Container Registry, and builds use that cache to cut build time and stay optimized. The Registry Cache lives in the Container Registry and can be reused across multiple builds.

Here’s an example of ACR (Azure Container Registries)-based image cache management, building with docker buildx (which uses buildkit internally) from a github action workflow.

- name: Build and push Container image
uses: docker/build-push-action@v6.5.0
env:
IMAGE_TAG: ${{ env.IMAGE_TAG }}
with:
context: .
file: ./Dockerfile
push: true
cache-from: type=registry,ref=${{ vars.ACR_LOGIN_SERVER }}/${{ vars.ACR_REPOSITORY }}-cache
cache-to: type=registry,ref=${{ vars.ACR_LOGIN_SERVER }}/${{ vars.ACR_REPOSITORY }}-cache,mode=max
tags: |
${{ vars.ACR_LOGIN_SERVER }}/${{ vars.ACR_REPOSITORY }}-${{ inputs.environment }}:${{ env.IMAGE_TAG }}
${{ vars.ACR_LOGIN_SERVER }}/${{ vars.ACR_REPOSITORY }}-${{ inputs.environment }}:latest
  • context: the build context (current directory .)
  • file: path to the Dockerfile to use
  • push: whether to push the built image to the Container Registry
  • cache-from: uses a cache previously stored in the Container Registry (specified with type=registry)
  • cache-to: stores a new cache in the Container Registry (mode=max stores as much cache data as possible)
  • tags: applies the IMAGE_TAG and latest tags

Full GitHub Actions Workflow example:

jobs:
ci:
name: (${{ inputs.environment }}) Push Container image and Update K8s Manifests
runs-on: "${{ inputs.runner }}"
steps:
- name: Check out the repo
uses: actions/checkout@v4.1.7
with:
token: ${{ secrets.GH_ADMIN_TOKEN }}
fetch-depth: 1
- name: Get image tag
run: echo "IMAGE_TAG=$(TZ=Asia/Seoul date +%Y%m%d%H%M%S)" >> $GITHUB_ENV
- name: Log in to Azure CLI
env:
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS_COMMON }}
run: |
echo "$AZURE_CREDENTIALS" > azure_credentials.json
az login \
--service-principal \
--username $(jq -r .clientId azure_credentials.json) \
-p=$(jq -r .clientSecret azure_credentials.json) \
--tenant $(jq -r .tenantId azure_credentials.json)
az account set --subscription $(jq -r .subscriptionId azure_credentials.json)
- name: Login to ACR Private
run: |
az acr login --name ${{ vars.ACR_NAME }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.6.1
- name: Build and push Container image
uses: docker/build-push-action@v6.5.0
env:
IMAGE_TAG: ${{ env.IMAGE_TAG }}
with:
context: .
file: ./Dockerfile
push: true
cache-from: type=registry,ref=${{ vars.ACR_LOGIN_SERVER }}/${{ vars.ACR_REPOSITORY }}-cache
cache-to: type=registry,ref=${{ vars.ACR_LOGIN_SERVER }}/${{ vars.ACR_REPOSITORY }}-cache,mode=max
tags: |
${{ vars.ACR_LOGIN_SERVER }}/${{ vars.ACR_REPOSITORY }}-${{ inputs.environment }}:${{ env.IMAGE_TAG }}
${{ vars.ACR_LOGIN_SERVER }}/${{ vars.ACR_REPOSITORY }}-${{ inputs.environment }}:latest

Key design considerations for the CI/CD pipeline: it works on both GitHub-hosted and self-hosted runners; since the CI Pipeline’s steps can vary by development language/framework, it’s managed separately per application. The CD Job runs from the repository that manages GitOps manifests, and must be able to talk to the k8s cluster where Argo CD is installed.

CI/CD Workflow Diagram

The CD Workflow is built as a Custom Action so it can be reused, and since both Backend and Frontend deploy the same way as Argo CD Applications on the k8s cluster, the flow is identical for both.

CD Job detailed workflow

Let’s look at the scripts that define the GitHub Actions Workflow reflecting the Application CI/CD pipeline design above.

  • Build Container Image and Push
  • Set up per application repository’s workflow, since it varies by application characteristics

The main steps run in this order: checkout GitOps repository β†’ configure Tag/Branch environment variables β†’ install NodeJS and run npm install β†’ run Lint β†’ ACR Login β†’ install Docker Buildx β†’ Docker Build & Push β†’ set Git Tag β†’ call the CD Action.

- name: Check out the repo
uses: actions/checkout@v4
- name: Set Tag for Build and Artifacts
run: |
echo "IMAGE_TAG=$(TZ=UTC-9 date +'%y%m%d.%H%M').$(printf 'v%03d' ${{github.run_number}})" >> $GITHUB_ENV
- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
id: extract_branch
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20.14
- name: Install Node.js dependencies
run: npm install
- name: Run linters
run: npm run lint ## Run Lint
- name: ACR login
uses: azure/docker-login@v1
with:
login-server: ${{ secrets.ACR_LOGIN_SERVER }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.6.1
- name: Build and push Container image
uses: docker/build-push-action@v6.5.0
env:
IMAGE_TAG: ${{ env.IMAGE_TAG }}
with:
context: .
file: ./Dockerfile
push: true
cache-from: type=registry,ref=${{ secrets.ACR_LOGIN_SERVER }}/${{ github.event.repository.name }}-cache
cache-to: type=registry,ref=${{ secrets.ACR_LOGIN_SERVER }}/${{ github.event.repository.name }}-cache,mode=max
tags: |
${{ secrets.ACR_LOGIN_SERVER }}/${{ github.event.repository.name }}-${{ steps.extract_branch.outputs.branch }}:${{ env.IMAGE_TAG }}
${{ secrets.ACR_LOGIN_SERVER }}/${{ github.event.repository.name }}-${{ steps.extract_branch.outputs.branch }}:latest
- name: Add Git tag to source repository and setup output"
env:
IMAGE_TAG: ${{ env.IMAGE_TAG }}
REPO_TAG: ${{ steps.extract_branch.outputs.branch }}.${{ github.event.repository.name }}.${{ env.IMAGE_TAG }}
run: |
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
git config --global user.name "${{ github.actor }}"
git tag -a "${{ env.REPO_TAG }}" -m "Push tag ${{ env.REPO_TAG }}"
git push origin "${{ env.REPO_TAG }}"
echo "image_tag=$(echo ${{ env.IMAGE_TAG }})" >> $GITHUB_OUTPUT
echo "repo_tag=$(echo ${{ env.REPO_TAG }})" >> $GITHUB_OUTPUT
id: set_tag
- name: Run CD actions
uses: example-org/platform-argocd-apps/.github/actions/cd@develop
with:
gitops_repo_name: ${{ vars.GITOPS_REPO_NAME }}
gitops_token: ${{ secrets.GITOPS_TOKEN }}
branch: ${{ needs.ci.outputs.branch }}
argocd_server: ${{ vars.ARGOCD_SERVER }}
argocd_admin_user: ${{ secrets.ARGOCD_ADMIN_USER }}
argocd_admin_pass: ${{ secrets.ARGOCD_ADMIN_PASS }}
argocd_app_name: ${{ github.event.repository.name }}
acr_login_server: ${{ secrets.ACR_LOGIN_SERVER }}
image_tag: ${{ needs.ci.outputs.image_tag }}
repo_tag: ${{ needs.ci.outputs.repo_tag }}

Full pipeline manifest:

name: Build and Deploy
on:
workflow_dispatch:
## Manually triggered from the GitHub Actions tab
jobs:
ci:
name: Build and push container image
runs-on: ubuntu-latest ## github managed runner
# runs-on: arc-runner-platform ## self-hosted runner
outputs:
branch: ${{ steps.extract_branch.outputs.branch }}
image_tag: ${{ steps.set_tag.outputs.image_tag }}
repo_tag: ${{ steps.set_tag.outputs.repo_tag }}
steps:
- name: Check out the repo
uses: actions/checkout@v4 ## check out the source using the actions/checkout action
# Tagging rule
# [yy].[m].[d].[H][M].v[actions build number]
- name: Set Tag for Build and Artifacts ## build the tag to set on the Docker image
run: |
echo "IMAGE_TAG=$(TZ=UTC-9 date +'%y%m%d.%H%M').$(printf 'v%03d' ${{github.run_number}})" >> $GITHUB_ENV
- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
id: extract_branch
- name: Set up Node.js
uses: actions/setup-node@v4 ## set up Node via actions/setup-node to run tests in the pipeline
with:
node-version: 20.14
- name: Install Node.js dependencies
run: npm install
- name: Run linters
run: npm run lint ## Run Lint
- name: ACR login
uses: azure/docker-login@v1 ## log in to ACR via the azure/docker-login action to pull/push docker images
with:
login-server: ${{ secrets.ACR_LOGIN_SERVER }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.6.1 ## set up the environment needed to build/push via docker/setup-buildx-action
- name: Build and push Container image
uses: docker/build-push-action@v6.5.0
env:
IMAGE_TAG: ${{ env.IMAGE_TAG }}
with:
context: .
file: ./Dockerfile
push: true
cache-from: type=registry,ref=${{ secrets.ACR_LOGIN_SERVER }}/${{ github.event.repository.name }}-cache
cache-to: type=registry,ref=${{ secrets.ACR_LOGIN_SERVER }}/${{ github.event.repository.name }}-cache,mode=max
tags: |
${{ secrets.ACR_LOGIN_SERVER }}/${{ github.event.repository.name }}-${{ steps.extract_branch.outputs.branch }}:${{ env.IMAGE_TAG }}
${{ secrets.ACR_LOGIN_SERVER }}/${{ github.event.repository.name }}-${{ steps.extract_branch.outputs.branch }}:latest
## tag the docker image with the tag from the extract_branch step, then push it to the logged-in ACR
- name: Add Git tag to source repository and setup output"
env:
IMAGE_TAG: ${{ env.IMAGE_TAG }}
REPO_TAG: ${{ steps.extract_branch.outputs.branch }}.${{ github.event.repository.name }}.${{ env.IMAGE_TAG }}
run: |
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
git config --global user.name "${{ github.actor }}"
git tag -a "${{ env.REPO_TAG }}" -m "Push tag ${{ env.REPO_TAG }}"
git push origin "${{ env.REPO_TAG }}"
echo "image_tag=$(echo ${{ env.IMAGE_TAG }})" >> $GITHUB_OUTPUT
echo "repo_tag=$(echo ${{ env.REPO_TAG }})" >> $GITHUB_OUTPUT
id: set_tag
## set the git commit tag in the form (branch_name).(service_name).(Docker Tag)
cd:
name: Update manifests and sync argocd app ## job that deploys the docker image pushed in the ci job via Argo CD
needs: ci
# runs-on: ubuntu-latest
runs-on: arc-runner-platform ## deploy via Argo CD using a self-hosted runner
steps:
- name: Run CD actions
uses: example-org/platform-argocd-apps/.github/actions/cd@develop
with:
gitops_repo_name: ${{ vars.GITOPS_REPO_NAME }}
gitops_token: ${{ secrets.GITOPS_TOKEN }}
branch: ${{ needs.ci.outputs.branch }}
argocd_server: ${{ vars.ARGOCD_SERVER }}
argocd_admin_user: ${{ secrets.ARGOCD_ADMIN_USER }}
argocd_admin_pass: ${{ secrets.ARGOCD_ADMIN_PASS }}
argocd_app_name: ${{ github.event.repository.name }}
acr_login_server: ${{ secrets.ACR_LOGIN_SERVER }}
image_tag: ${{ needs.ci.outputs.image_tag }}
repo_tag: ${{ needs.ci.outputs.repo_tag }}

We use the example-org/platform-argocd-apps/.github/actions/cd@develop GitHub Action to update GitOps manifests and sync the Argo CD application.

InputRequiredDefaultDescription
gitops_repo_nameβœ…""Name of the repository that stores the GitOps manifests
gitops_tokenβœ…""GitHub Token used to access the GitOps repository
branchβœ…""Branch to update the GitOps manifests on
argocd_repoβœ…https://github.com/example-org/platform-argocd-apps.gitArgo CD GitOps manifests repository
argocd_dest_serverβœ…https://kubernetes.default.svcTarget Kubernetes cluster that Argo CD deploys to
argocd_serverβœ…""Argo CD server address
argocd_admin_userβœ…""Argo CD admin account
argocd_admin_passβœ…""Argo CD admin password
argocd_app_nameβœ…""Argo CD application name
acr_login_server❌""ACR (Azure Container Registry) login server
image_tag❌""Container image tag to deploy
repo_tag❌""Git tag

The main steps run in this order: checkout GitOps repository β†’ install Kustomize β†’ set the deployment path (dev/prd) based on branch β†’ apply the image tag β†’ push a Git Tag to the manifest repository β†’ install/log in with the Argo CD CLI β†’ configure Application β†’ Sync β†’ update the repo_tag options.

- name: Checkout GitOps manifests repository
uses: actions/checkout@v4.2.2
with:
repository: ${{ github.repository_owner }}/${{ inputs.gitops_repo_name }}
token: ${{ inputs.gitops_token }}
- name: Setup kustomize
uses: imranismail/setup-kustomize@v2
- name: Set gitops directory path based on branch
run: |
if [[ "${{ inputs.branch }}" == "main" ]]; then
echo "GITOPS_PATH=prd" >> $GITHUB_ENV
elif [[ "${{ inputs.branch }}" == "develop" ]]; then
echo "GITOPS_PATH=dev" >> $GITHUB_ENV
fi
shell: bash
- name: Update Kubernetes Resources with New Docker Image Tag
run: |
cd ${{ inputs.argocd_app_name }}/overlays/${{ env.GITOPS_PATH }}
if [[ -n "${{ inputs.image_tag }}" ]]; then
kustomize edit set image \
${{ inputs.argocd_app_name }}=${{ inputs.acr_login_server }}/${{ inputs.argocd_app_name }}-${{ inputs.branch }}:${{ inputs.image_tag }}
kustomize build .
fi
shell: bash
- name: Add Git Tag to Manifest Repository
run: |
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
git config --global user.name "${{ github.actor }}"
if [[ -n "${{ inputs.image_tag }}" ]]; then
git add .
git commit --allow-empty -m 'update version tag ${{ inputs.repo_tag }} from GitHub Action'
git push origin ${{ inputs.branch }} -f
git tag -af "${{ inputs.repo_tag }}" -m "Update ${{ inputs.repo_tag }}"
git push origin "${{ inputs.repo_tag }}" -f
fi
shell: bash
- name: Set-up Argo CD CLI
run: |
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
rm argocd-linux-amd64
argocd login --grpc-web --insecure --plaintext ${{ inputs.argocd_server }} \
--username ${{ inputs.argocd_admin_user }} \
--password ${{ inputs.argocd_admin_pass }}
shell: bash
- name: Set Argo CD Application
run: |
argocd app set ${{ inputs.argocd_app_name }} \
--path ${{ inputs.argocd_app_name }}/overlays/${{ env.GITOPS_PATH }} \
--repo ${{ inputs.argocd_repo }} \
--dest-server ${{ inputs.argocd_dest_server }} \
--revision ${{ inputs.repo_tag }} \
--sync-option Prune=true
shell: bash
- name: Sync Argo CD Application
run: |
argocd app sync --prune \
${{ inputs.argocd_app_name }}
shell: bash
- name: Update repo_tag options in workflow_dispatch
run: |
WORKFLOW_FILE=".github/workflows/argocd.yml"
git fetch --tags
TAGS=$(git tag --sort=-v:refname | head -20)
echo "Latest Tags: $TAGS"
TAGS_YAML=$(echo "$TAGS" | sed 's/^/ - /')
yq eval -i '
.on.workflow_dispatch.inputs.repo_tag.options = ["=== select ==="] + (env(TAGS_YAML))
' "$WORKFLOW_FILE"
git add "$WORKFLOW_FILE"
git commit --allow-empty -m "Update repo_tag options with latest tags"
git push
shell: bash

Full custom action manifest (example-org/platform-argocd-apps/.github/actions/cd@develop):

name: CD with argocd
description: Update gitops manifests and sync argocd app
inputs:
gitops_repo_name:
required: true
default: ""
gitops_token:
required: true
default: ""
branch:
required: true
default: ""
argocd_repo:
required: true
default: "https://github.com/example-org/platform-argocd-apps.git"
argocd_dest_server:
required: true
default: "https://kubernetes.default.svc"
argocd_server:
required: true
default: ""
argocd_admin_user:
required: true
default: ""
argocd_admin_pass:
required: true
default: ""
argocd_app_name:
required: true
default: ""
acr_login_server:
required: false
deafult: ""
image_tag:
required: false
default: ""
repo_tag:
required: false
default: ""
runs:
using: "composite" ## bundle multiple steps into one Action so it can be used externally
steps:
- name: Checkout GitOps manifests repository
uses: actions/checkout@v4.2.2
with:
repository: ${{ github.repository_owner }}/${{ inputs.gitops_repo_name }}
token: ${{ inputs.gitops_token }}
- name: Setup kustomize
uses: imranismail/setup-kustomize@v2
- name: Set gitops directory path based on branch
run: |
if [[ "${{ inputs.branch }}" == "main" ]]; then
echo "Setting path to prd"
echo "GITOPS_PATH=prd" >> $GITHUB_ENV
elif [[ "${{ inputs.branch }}" == "develop" ]]; then
echo "Setting path to dev"
echo "GITOPS_PATH=dev" >> $GITHUB_ENV
else
echo "Setting default path"
fi
shell: bash
- name: Update Kubernetes Resources with New Docker Image Tag
env:
GITOPS_PATH: ${{ env.GITOPS_PATH }}
run: |
cd ${{ inputs.argocd_app_name }}/overlays/${{ env.GITOPS_PATH }}
if [[ -n "${{ inputs.image_tag }}" ]]; then
kustomize edit set image \
${{ inputs.argocd_app_name }}=${{ inputs.acr_login_server }}/${{ inputs.argocd_app_name }}-${{ inputs.branch }}:${{ inputs.image_tag }}
kustomize build .
else
echo "Skipping image update as image_tag is empty."
fi
shell: bash
- name: Add Git Tag to Manifest Repository "${{ inputs.repo_tag }}"
run: |
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
git config --global user.name "${{ github.actor }}"
if [[ -n "${{ inputs.image_tag }}" ]]; then
git add .
git commit --allow-empty -m 'update version tag ${{ inputs.repo_tag }} from GitHub Action'
git push origin ${{ inputs.branch }} -f
git tag -af "${{ inputs.repo_tag }}" -m "Update ${{ inputs.repo_tag }}"
git push origin "${{ inputs.repo_tag }}" -f
else
echo "Skipping repository commit push and tag. (without build process)"
fi
shell: bash
- name: Set-up Argo CD CLI
run: |
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
rm argocd-linux-amd64
argocd login --grpc-web --insecure --plaintext ${{ inputs.argocd_server }} \
--username ${{ inputs.argocd_admin_user }} \
--password ${{ inputs.argocd_admin_pass }}
shell: bash
- name: Set Argo CD Application
env:
GITOPS_PATH: ${{ env.GITOPS_PATH }}
run: |
argocd app set ${{ inputs.argocd_app_name }} \
--path ${{ inputs.argocd_app_name }}/overlays/${{ env.GITOPS_PATH }} \
--repo ${{ inputs.argocd_repo }} \
--dest-server ${{ inputs.argocd_dest_server }} \
--revision ${{ inputs.repo_tag }} \
--sync-option Prune=true
shell: bash
- name: Sync Argo CD Application
run: |
argocd app sync --prune \
${{ inputs.argocd_app_name }}
shell: bash
- name: Update repo_tag options in workflow_dispatch
run: |
WORKFLOW_FILE=".github/workflows/argocd.yml"
# yq install
if ! command -v yq &> /dev/null; then
curl -sSL -o yq_linux_amd64 https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
sudo install -m 555 yq_linux_amd64 /usr/local/bin/yq
rm yq_linux_amd64
fi
# Get latest tags
git fetch --tags
TAGS=$(git tag --sort=-v:refname | head -20)
echo "Latest Tags: $TAGS_ARRAY"
# Update inputs.repo_tag options
TAGS_YAML=$(echo "$TAGS" | sed 's/^/ - /') yq eval -i '
.on.workflow_dispatch.inputs.repo_tag.options = ["=== select ==="] + (env(TAGS_YAML))
' "$WORKFLOW_FILE"
# Commit and push the change to Git
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
git config --global user.name "${{ github.actor }}"
git add "$WORKFLOW_FILE"
git commit --allow-empty -m "Update repo_tag options with latest tags"
git push
shell: bash

Continue with GitOps CD Workflow: Image Signing and Smoke Testing.