cover-img

Setup pipeline using CircleCI, update GitHub Kubernetes manifest repo and push image on Docker

10 July, 2023

1

1

0

πŸ™‹β€β™‚οΈ 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

  1. Terraform files to provision the EKS cluster LINK
  2. Kubernetes manifest fils LINK
  3. Todo-react (application code) LINK

git kubernetes circleci argocd setup

we need to push the Kubernetes manifest repo and Todo-react(application code) on GitHub. so let's push those repositories.

Application code repository

Kubernetes manifest files repo

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.

generate github token

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.

circleci setup

select main branch. we have already created the .circleci/config.yml file so just click on the Fastest radio button and setup the project.

setup the project on circleci

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

circleci pipeline

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.

environment variable in circleci

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

all the environment variable

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.

successfully setup the pipeline

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

1

1

0

aws

azure

docker

git

github

devops

kubernetes

linux

yaml

developer

jenkins

learninpublic

cloud

argocd

terraform

flux

kyverno

fleet

100daysdevops

pulumi

linkerd

growtogether

circleci

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

Β© Copyright 2024. Showcase Creators Inc. All rights reserved.