Combine GitHub Actions with FluxCD

I started my hobby project SausLink( a tinyURL like URL shortener ) a while ago, as it was really boring during covid lockdowns. The web app itself isn’t anything cutting-edge but I intended to implement full git-ops for this project.

For the CI pipeline, I chose GitHub Actions because it’s easy and also free for open source projects( for a limited builds per day ). The pipeline will be triggered by pushing to dev branch and will build a docker image and then tag it with a GitHub Actions job number. The last step will update the kustomize patch file to refer to the latest tag.

Here’s the pipeline definition along with my comments

# this is .github/workflows/dev.yaml
name: Build the dev image

    branches: [ dev ]
    branches: [ dev ]

    runs-on: [ ubuntu-20.04 ]
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      # save a tag like dev-123 to environment variable called build_number
      - name: build number
        run: |
          echo "build_number=dev-${GITHUB_RUN_ID}" >> $GITHUB_ENV

      # build the docker image and tag it with build_number
      - name: Docker build saus-gunicorn
        run: |
          docker build -t$build_number .

      # login to and push the docker image
      - name: Docker push
        run: |
          echo ${{ secrets.GHCR_PAT }} | docker login -u $GITHUB_ACTOR --password-stdin
          docker push$build_number

      # update the kustomize patch to use the latest tag, using sed command and regex capture
      - name: Update deployment version
        run: |
          sed -i -E "s|(image:*|\1$build_number|g" .kustomize/

      # add an auto commit with the updated patch file, so the FluxCD can pick it up and deploy with the latest tag
      - name: Auto commit & push changes
        run: |
          git config --global 'raynix Bot'
          git config --global '[email protected]'
          git commit -am "Automated commit"
          git push

At the CD pipeline run by FluxCD v2, a Kustomization resource is defined as follow

# this is the definition of the git repo where the kustomize templates are located
kind: GitRepository
  name: saus-dev
  namespace: flux-system
  interval: 4m0s # check the repo every 4 minutes, don't be greedy
    branch: dev # fixed to dev branch for dev environment

# this is the kustomization definition
kind: Kustomization
  name: saus-dev
  namespace: flux-system
  - kind: Deployment
    name: saus
    namespace: saus-dev
  interval: 4m0s # reconcile the deployment every 4 minutes
  path: .kustomize/ # directory where the main kustomization.yaml resides in
  prune: true
    kind: GitRepository
    name: saus-dev
  timeout: 2m0s
  validation: client
  force: true # force to re-deploy objects which have immutable fields such as jobs

For more FluxCD experiments please see my previous post here. The SausLink’s source code is here. Feel free to fork it and host your own URL shortener 🙂