How I Setup A Simple CI/ CD Process With Jenkins And GKE Part 2

Falgi Faisal
6 min readJul 1, 2020

--

In my previous article (How I Setup A Simple CI/ CD Process With Jenkins And GKE Part 1), i already setup jenkins and GKE. And now i will share how i setup for my simple CI / CD

First, to i want to connect the jenkins server with my kubernetes machines so i installed kubectl from the jenkins server

After installed, i clicked “Kubernetes Engine” and “Clusters”

Next, i clicked “Connect” in my cluster (in my case, the cluster name is “testing”) and copy the command that shown in the console and paste it into my jenkins server

And then, to test is it already connected to my cluster or no, i try to run “kubectl get nodes” and this is the result

Now, i installed docker in the jenkins server. This is used when i want to build the docker image. After installed the docker, i runned “docker run hello-world” to test it

After installed the docker, i runned “gcloud auth configure-docker” so i can push and pull the docker image from Google Container Registry

Next, i setup the jenkins and my repository so when i do something to my repository like push to my repository, it will trigger my jenkins to do something to cluster

First, after logged in to my account, i clicked “Settings”

Next, go to “Developer Settings”

Next, i choosed “Personal Access Tokens” and “Generate new token” and clicked the options that accordance with my needs

Then i clicked “Generate token”. And this is the result for my token

I will use this token for my Dockerfile, so i can clone my repository without entering my credentials.

Next, from my repository i choosed “Settings” and “Webhooks”. This is i use to trigger the jenkins server if there is an event like push or pull requests

Next, i clicked “Add webhook” and filled the form that accordance with my needs

Next, i clicked “Add webhook”

Now, time to setup the jenkins.

I logged in to my jenkins server and click “New Item”

Next, i filled the form with “test” and choosed “Freestyle Project”

Next, i filled the form

In “Repositories” section, i filled with my private repository

To connect with my private repository, i need to add my credential so i clicked “Add” and “Jenkins”. And filled the form with my username and password

And in the “Build Trigger” section, i choosed “GitHub hook trigger for GITScm polling”

And in the “Build” section i clicked “Add build step” and “Execute shell”

And run bash script to process ci /cd where the process of this script is

a. Create timestamp for versioning of docker image

b. Build docker image with — no-cache option and tag the image with versioning of timestamp that already created in step a.

c. Push the docker image into google container registry

d. Update the image with newer version in kubernetes

Here is the script that i saved in deploy.sh

date "+%Y%m%d%H%M%S" > time-build.txt
docker build --no-cache -t asia.gcr.io/latihan-281707/testing-deployment:production-$(cat time-build.txt) .
docker push asia.gcr.io/latihan-281707/testing-deployment:production-$(cat time-build.txt)
kubectl set image deployment/testing-deployment testing-deployment=asia.gcr.io/latihan-281707/testing-deployment:production-$(cat time-build.txt)

And then click “Save”

Now, i created a simple deployment with my simple image with command “kubectl create deployment testing-deployment — image=asia.gcr.io/latihan-281707/testing-deployment:production-20200629143158”

And this is the result

Based on the picture, after created the deployment, i run curl ip-of-the-pod:port-of-my-container (in this case i run curl 10.24.2.27:3000), the output give me “Hello World version production 2”.

Now, i will update the code in my repository from version 2 to version 3.

After click “Commit Changes”, this will trigger the job that already created in jenkins. You can find it in “Build History”. In my case, the update i did at number 20

Then, i clicked this build history and this is the output after i clicked “Console Output” to see the output of this job

Now, i will check into my server and run curl ip-of-the-pod:port-of-my-container (in this case i run curl 10.24.1.39:3000), the output give me “Hello World version production 3”. The output shows the latest code in the repository.

And this is the output when i want to check the image in my deployment

I think it’s enough for a very simple ci / cd and i still need to improve this to make the performance better than now.

Please correct me if you find a mistake.

Thank you.

--

--