Setup pipeline using CircleCI, update GitHub Kubernetes manifest repo and push image on Docker
10 July, 2023
1
1
0
Contributors
πββοΈ Introduction
Hi everybody, my name is Ankit Jodhani. and I'm DevOps and Cloud enthusiast. I recently graduated from university and building my career in DevOps and cloud. I have written many blogs and projects on could and Devops you can see that on my Hashnode profile Ankit Jodhani.
β Prerequisites
- πΈ GitHub Acc
- βΊοΈ CircleCI Acc
- β Just need time to invest in learning
πΆ Note (Imp)
this is the 3rd blog of the Deploying Dockerized App on AWS EKS Cluster using ArgoCD and GitOps methodology with CircleCI
Blog 1 link: Click here
Blog 2 link: Click here
this is blog 3
Blog 4 link: Click here
π Synopsis
in this blog, we will configure a pipeline that triggers where there is a commit in the application code repository.
πΈ Story
When CircleCI notices any changes in the application code, it starts executing the jobs that we have set up. There are a total of four jobs:
- π§ͺ Test:
- This job tests the code. After the test job is completed, CircleCI proceeds to the next job.
- Note: I didn't add this job to save time. π
- ποΈ Build:
- In the build job, CircleCI pulls the base Docker images and packages our application code inside the image.
- πͺ Push:
- The push job pushes the newly generated images to Docker Hub with a new tag.
- π Update Manifest:
- After the successful completion of the push job, the last job is executed, which updates the Kubernetes manifest repository with the new tag. This enables ArgoCD to detect the change and apply it to the cluster.
By following this pipeline, we ensure that our application code is thoroughly tested, built into Docker images, and deployed with the updated manifest using the GitOps approach.
π Step-by-step guide
Step 1: Writing YAML file
Open application code folder inside any code editor, Create a folder with the name .circleci
and create one file inside that folder with the name config.yml
paste the below content inside the config.yml
file. if you need to help check out my GitHub repo. I've given all the links in Step 2.
version: 2.1
jobs:
build_and_push:
docker:
- image: cimg/node:20.3.1
steps:
- checkout
- setup_remote_docker
- run:
name: Lets build and push image
command: |
version="build-$CIRCLE_BUILD_NUM"
echo $version
docker build -t ankitjodhani/todo-app:$version .
echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin
docker push ankitjodhani/todo-app:$version
Update_menifest:
docker:
- image: cimg/base:2023.06
steps:
- checkout
- setup_remote_docker
- run:
name: Updating Menifest file
command: |
TAG=$CIRCLE_BUILD_NUM
((TAG--))
git clone https://github.com/AnkitJodhani/kube_manifest-4thWeekOfCloudOps.git
git config --global user.email "ankitjodhani1903@gmail.com"
git config --global user.name "AnkitJodhani"
cd kube_manifest-4thWeekOfCloudOps
sed -i "s/build-.*/build-$TAG/g" menifest/deployment.yaml
git add .
git commit -m "new build with imgTag build-$TAG"
git config credential.helper 'cache --timeout=120'
git push -q https://$GITHUB_PERSONAL_TOKEN@github.com/AnkitJodhani/kube_manifest-4thWeekOfCloudOps.git main
workflows:
GitOpsflow:
jobs:
- build_and_push
- Update_menifest:
requires:
- build_and_push
step 2: Host code on GitHub
I've three repositories in my local machine
- Terraform files to provision the EKS cluster LINK
- Kubernetes manifest fils LINK
- Todo-react (application code) LINK
we need to push the Kubernetes manifest repo and Todo-react(application code) on GitHub. so let's push those repositories.
Step 3: Generate GitHub Token
Now, Go into settings
β‘οΈ Developer settings
β‘οΈ personal access token
and create one personal access token. and copy that because CircleCI needs a token to update the Kubernetes manifest repo.
Step 4: Configure CircleCI and pipeline
Login into your CircleCI ACC and sync GitHub repos. here you will see all the repositories that are present in your GitHub account.
Select your application code repo and click on the Set Up project
button.
select main
branch. we have already created the .circleci/config.yml file so just click on the Fastest
radio button and setup the project.
when you set up the project automatically CircleCI starts running the pipeline. but it will fail because we need DOCKER_USERNAME
, DOCKER_PASSWORD
, and also GITHUB_PERSONAL_TOKEN
. these variables I've used in the config.yml file so we need to define that.
DO NOT RUN AGAIN because we need to environment variable
Step 5: Add the Environment variable
Now go inside the project setting and click on the Environment variable
tab on the left panel. here we can write our environment variable. in this way, we don't have to hardcode our credential data such as docker password, username, GitHub token, etc.. in the config.yml
file.
click on Add environment variable
and add all the environment variables which you want to utilize inside the config.yml file. in my case they were
- DOCKER_PASSWORD
- DOCKER_USERNAME
- GITHUB_PERSONAL_TOKEN
we did setup successfully so let's run the pipeline one more time. it will build a new image, push on hub.docker.com and update the manifest repo with a new TAG.
so here we set up our pipeline successfully.
its time to move on to part 4: Link
π Thank you for reading this blog
πββοΈ Ankit Jodhani.
π¨ reach me at ankitjodhani1903@gmail.com
π LinkedIn https://www.linkedin.com/in/ankit-jodhani/
π Github project repo https://github.com/AnkitJodhani/Todo_application
πΊ Github https://github.com/AnkitJodhani
π¦ Twitter https://twitter.com/Ankit__Jodhani
aws
azure
docker
git
github
devops
kubernetes
linux
yaml
developer
jenkins
learninpublic
cloud
argocd
terraform
flux
kyverno
fleet
100daysdevops
pulumi
linkerd
growtogether
circleci