GitOps 101 (Part 5)
In our previous article, we mentioned how ArgoCD applications can be created and briefly the App of Apps pattern. Now it’s time to define the deploy job definition, which is the last part of our CI/CD step, and see the results of an end-to-end GitOps approach.
First, let’s complete the last step of our CI/CD Pipeline. In the deploy job process, we expect that the information of our new docker image created in the pipeline should be added/updated in the ArgoCD repository, which also contains the manifests of ArgoCD. So our deploy script will consist of a series of bash and git commands.
The definition for Image was on “deployment.yaml”. We should add the definition of the current version and push the line of the image on this file to the ArgoCD repository.
deploy-job:
stage: deploy
image:
name: alpine/git:latest
entrypoint: [""]
variables:
DOCKER_REGISTRY: $CI_REGISTRY
DOCKER_IMAGE_URI: "${DOCKER_REGISTRY}/${CI_PROJECT_PATH}/${CI_COMMIT_REF_SLUG}"
needs:
- job: docker
artifacts: false
before_script:
- export DOCKER_IMAGE_TAG=${CI_COMMIT_TAG:="${CI_COMMIT_SHORT_SHA}"}
script:
- git clone --depth 1 https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/***/gitops-argo-cd.git
- IMAGE="${DOCKER_IMAGE_URI}:${DOCKER_IMAGE_TAG}"
- DEPLOYMENT_FILE="gitops-argo-cd/charts/gitops-react-app/deployment.yaml"
- 'sed -i "s#image: .*#image: $IMAGE#" $DEPLOYMENT_FILE'
- cd gitops-argo-cd
- git config user.email "***@***.com"
- git config user.name "CI Automation Bot"
- git commit -am "app updated to version:${CI_COMMIT_SHORT_SHA}"
- git remote set-url origin https://gitlab-ci-token:$PAT_TOKEN@$CI_SERVER_HOST/***/gitops-argo-cd.git
- git push
After the deploy job definition runs, the line for the image definition in the “deployment.yaml” file should have changed in the repository where the Kubernetes manifest files of the application are located.
Now that we have completed the deploy-job definition, we can now observe a summary of an end-to-end GitOps approach. First, let’s look at the final version of our “React Application” repository:
├── .gitignore
├── .gitlab-ci.yml
├── Dockerfile
├── README.md
├── package-lock.json
├── package.json
├── public
└── src
├── App.css
├── App.js
├── App.test.js
├── index.css
├── index.js
├── logo.svg
├── reportWebVitals.js
└── setupTests.js
We created and added the “.gitlab-ci.yml” file to our “React Application” so that our CI/CD pipeline could run, and the “Dockerfile” file to create the Docker image. When you look at the CI/CD -> Pipelines section from the GitLab menu, our pipeline will appear as formed.
Then we created our repository containing the manifest files that will run on ArgoCD. There are manifest files required for ArgoCD deployments in our “apps” folder, and thanks to these manifest files, we direct to Kubernetes manifest files of our applications. There were Kubernetes manifest files in the “gitops-react-app” folder in the “charts” folder to which we were redirecting. ArgoCD will pull these files from the repository and start the apply/replace operation in Kubernetes.
├── apps
│ ├── app-of-apps.yaml
│ └── projects
│ └── gitops-react-app.yaml
└── charts
└── gitops-react-app
├── autoscaler.yaml
├── configmap.yaml
├── credentials.yaml
├── deployment.yaml
├── ingress.yaml
├── kustomization.yaml
├── secret.yaml
└── service.yaml
After the processes are completed, we can see our application with the App of Apps pattern on ArgoCD.
In this series of articles, we tried to explain how we can create and use GitOps, one of the DevOps approaches, at a basic level. You can access the repository we created in this series on GitHub. In addition to this series of articles, you can also access our article about ArgoCD. Thank you for reading our articles.
You can access the repository of the sample application here and ArgoCD repository here