Skip to main content

Deployment automation with CI and gitops

So far, you have been learning about how Flux reconciles the desired state that is stored in a git repository onto the cluster.

In this section, you will learn a simple method to write to a gitops repository from CI. You will also get a glimpse of implementing simple deployment automation with CI.

For gitops to work, you have to write your deployments to a git repository so Flux can sync it to a cluster.

To be able to do this, you have to change your CI pipeline, and replace your deploy step with a gitops write step.

CI pipeline that writes the gitops repo

The steps for this are

  • checkout to gitops repository
  • commit changes to a local copy
  • push the gitops git repository to its origin

An example of this flow for Github Actions

    - name: Checkout GitOps repo
uses: actions/checkout@v2
with:
repository: mycompany/gitops
token: ${{ secrets.PAT }} # `PAT` is a secret that contains your Personal Access Token with `repo` scope
path: gitops

- name: Write to GitOps repository
run: |
mkdir templated-manifests
cat manifests/deployment.yaml | envsubst > templated-manifests/deployment.yaml
cat manifests/service.yaml | envsubst > templated-manifests/service.yaml

cp templated-manifests/ gitops/staging/dummy-app
(cd gitops/; git push origin main)

Using ready-made components to write to the gitops repository

The most commonly updated element in the gitops repository is the image tag to match the current build version.

There are ready-made components to do this bit in a gitops repository:

  • you can use Flux to react to new image versions in your image registry. This way, your CI pipeline can skip the gitops repo write part, as Flux will write the image update back to the gitops repository: https://fluxcd.io/docs/guides/image-update/