Skip to content

Commit 0698ceb

Browse files
CICD example with Jenkins and Bitbucket
1 parent c3ee5ee commit 0698ceb

12 files changed

Lines changed: 641 additions & 0 deletions

CICD/build-steps.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
echo "This is a build step"
2+
3+
cd Containers/Kubernetes-examples/GCP/greetings
4+
docker build -t gcr.io/cloudark-test-gke/greetings .
5+
gcloud docker -- push gcr.io/cloudark-test-gke/greetings
6+
7+
echo "SUCCESS"

CICD/cicd-steps.txt

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
CI/CD workflow using Jenkins and Bitbucket
2+
-------------------------------------------
3+
4+
Setup:
5+
------
6+
Conceptually you need two different machines for this exercise.
7+
8+
Machine 1: This machine/VM will be your developer workspace where you are developing your application.
9+
Machine 2: This VM will be your Jenkins server (CI/CD server)
10+
11+
Machine 1 can be your personal Laptop or a cloud VM.
12+
13+
Machine 2 *has* to be a cloud VM (such as a EC2 instance).
14+
Feel free to choose either a EC2 instance or Google Compute Engine instance
15+
Make sure you allow TCP traffic to port 8080 on your VM instance.
16+
17+
Note: It is fine to use a single VM as Machine 1 and Machine 2. It is up to you.
18+
19+
20+
A] Setup your developer workspace/Machine 1:
21+
--------------------------------------------
22+
1) Install Google Cloud CLI
23+
- sudo apt-get install python
24+
- curl https://sdk.cloud.google.com | bash
25+
26+
2) Configure authentication for gcloud CLI
27+
- gcloud auth login --> Follow the prompts (you will have to open browser window and paste the generated link,
28+
then paste the generated code in the verification field in your console.)
29+
- Create Project in Google Cloud Console --> Note down the Project ID. Remember that Project ID is different than the Project's Name.
30+
You will need Project ID in subsequent steps.
31+
- export PROJECT_ID=<Project-ID-from-previous-step>
32+
- export CLOUDSDK_COMPUTE_ZONE=us-central1-b
33+
- gcloud config set project ${PROJECT_ID}
34+
35+
3) Create a Google Cloud SQL instance
36+
- gcloud sql instances create instance1 --tier=db-f1-micro --authorized-networks=0.0.0.0/0
37+
38+
4) Create testdb database on the Cloud SQL instance
39+
- gcloud sql users set-password root % --instance instance1 --password 'testpass123!@#'
40+
- sudo apt-get install mysql-client
41+
- gcloud sql instances list
42+
- mysql -h <IP-address-of-SQL-Instance> --user=root --password='testpass123!@#'
43+
- mysql>create database testdb;
44+
45+
5) Create a static IP address for our application
46+
- gcloud compute addresses create greetings-ip --global
47+
48+
6) Create your Bitbucket repository named "assignment6"
49+
- Make it Public
50+
- Generate a SSH Key pair and add it to your Bitbucket account
51+
- Account -> Settings -> Add Key
52+
- You can either generate an SSH key or add an existing key if you have one
53+
54+
7) Download Sample CI/CD project
55+
- git clone https://github.com/devdattakulkarni/CloudComputing
56+
- cd CloudComputing/CICD/greetings
57+
- Edit greetings-deployment.yaml and set the environment variables corresponding to
58+
the Google Cloud SQL instance
59+
(Note: In the image tag don't include any version.
60+
i.e.: Define your image tag as: "gcr.io/<project-id>/greetings" and NOT as
61+
"gcr.io/<project-id>/greetings:v1")
62+
63+
- Add a remote for your Bitbucket repository and Push the sample CI/CD project to that remote
64+
- cd ../../
65+
- git remote add origin1 git@bitbucket.org:<your-bitbucket-username>/assignment6.git
66+
- git push origin1 master
67+
68+
69+
70+
B] Setup your Jenkins Server/Machine 2:
71+
--------------------------------------
72+
1) Jenkins Server Installation preparation:
73+
- Login to your Cloud VM instance (Machine2)
74+
- Install Java (JRE and JDK)
75+
- sudo apt-add-repository ppa:openjdk-r/ppa
76+
- sudo apt-get install -y openjdk-8-jre openjdk-8-jdk wget
77+
- Install Google Cloud CLI
78+
- sudo apt-get install python
79+
- curl https://sdk.cloud.google.com | bash
80+
- Install Kubectl
81+
- gcloud components install kubectl
82+
- Install Docker
83+
- git clone https://github.com/devdattakulkarni/CloudComputing.git
84+
- cd Containers/Kubernetes-examples/GCP/
85+
- ./install-docker-ubuntu.sh
86+
- exit from Cloud VM instance
87+
- SSH back into Cloud VM instance
88+
89+
2) Download and Start Jenkins Server:
90+
- wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
91+
- nohup java -jar jenkins.war --httpPort=8080 &
92+
93+
3) Configure Jenkins Server
94+
- Point your browser to the DNS name of the Jenkins Server at port 8080
95+
https://<DNS-of-Jenkins-Server>:8080/
96+
- Keep the user as 'admin'
97+
- Install recommended plugins
98+
99+
4) Once Jenkins starts up do following:
100+
4.1) Login as admin user
101+
- Password is stored in: $HOME/.jenkins/secrets/initialAdminPassword
102+
4.2) Set the path in Jenkins for Git
103+
Jenkins -> Manage Jenkins -> Configure -> Global Properties
104+
- Check Tool Locations
105+
- List of tool locations (Choose Git)
106+
- Set the path of Git from your VM
107+
(You can find the path by running 'which git' on VM)
108+
4.3) Install the Post build task Plugin
109+
Jenkins -> Manage Jenkins -> Manage Plugins -> Post build task plugin
110+
4.4) Install Bitbucket Plugin
111+
Jenkins -> Manage Jenkins -> Manage Plugins -> Bitbucket
112+
113+
5) Add a WebHook to your Bitbucket repository
114+
5.1) Go to your Bitbucket repository -> Settings -> URL. E
115+
- http://<DNS-of-Jenkins-server>:8080/bitbucket-hook/
116+
(Note: The trailing slash '/' is important. Don't forget that!!)
117+
- Make Status as "Active"
118+
- Check "Skip certificate verification"
119+
- Triggers -> Choose from a full list of triggers
120+
- Select following:
121+
- Repository->Push, Pull Request->Created, Updated, Comment created, Comment updated, Comment deleted
122+
Issue->Created, Issue->Updated, Issue->Comment Created
123+
124+
6) Set up a Jenkins Job
125+
6.1) Make sure your Bitbucket repository ("assignment6") is Public
126+
- This will make it possible to be cloned by Jenkins.
127+
6.2) Jenkins -> New Item -> Give name -> Select Freestyle project
128+
-> Source Code Management -> Give Bitbucket Repository URL (in git)
129+
6.3) Build Triggers
130+
-> Choose "Build when a change is pushed to BitBucket"
131+
6.4) Buid Section -> Choose Execute Shell script
132+
-> In the Execute shell Command section:
133+
- Add contents of build-steps.txt
134+
- Notes:
135+
- First modify build-steps.txt to use your GCP Project ID
136+
- Make sure that the image tag that use here is what you defined
137+
in deployment.yaml (Part A, step 7)
138+
6.5) In Post-build Actions
139+
-> Tasks -> Log text "SUCCESS" -> Operation "-- OR --"
140+
-> In the Tasks -> Script section:
141+
- Add contents of deploy-steps.txt (modify the steps with your assignment's name)
142+
143+
144+
145+
CI/CD Experiment:
146+
-----------------
147+
1) Open the Jenkins URL in your browser and navigate to the job
148+
149+
2) On Machine 1 (developer workspace) make changes to your code and push the code:
150+
- Modify index.html
151+
- git add
152+
- git commit
153+
- git push origin1 master
154+
155+
3) In the Jenkins Job you should see a new build triggered (visible under "Build History")
156+
- Select the Build
157+
- Go to Console Output
158+
- Verify that the build and deploy steps have been executed
159+
160+
4) On Machine 2 (Jenkins server):
161+
- kubectl get ingress
162+
- Navigate to the IP address and verify that your changes
163+
(Note: It may take 6/7 minutes before the ingress filtering routes will take effect
164+
and your changes are visible.)
165+
166+
5) Repeat steps 2-3-4
167+
168+
169+
170+
Clean up:
171+
---------
172+
From Google Cloud Console:
173+
- Delete Kubernetes Cluster
174+
- Delete Google Cloud SQL Instance
175+
- Delete the Static IP address
176+
177+
178+
179+
180+
181+
182+
Troubleshooting:
183+
----------------
184+
1) Restarting Jenkins:
185+
- From EC2 instance find out Jenkins process
186+
- ps -eaf | grep java
187+
- kill <process-id>
188+
- Re-run Jenkins using the java command
189+
190+
2) Jenkins URL location
191+
192+
3)
193+
194+
195+
196+
197+
Reference:
198+
-----------
199+
- https://cloud.google.com/kubernetes-engine/docs/tutorials/http-balancer
200+
- https://blog.terrenceryan.com/index.php/making-kubernetes-ip-addresses-static-on-google-container-engine/
201+
- http://ec2-34-212-126-125.us-west-2.compute.amazonaws.com:8080/bitbucket-hook/

CICD/cicd-steps.txt~

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
Steps to set up CI/CD workflow using Jenkins with your Bitbucket repository.
2+
----------------------------------------------------------------------------
3+
4+
Setup:
5+
------
6+
Conceptually you need two different machines for this exercise.
7+
8+
Machine 1: This machine/VM will be your developer workspace where you are developing your application.
9+
Machine 2: This VM will be your Jenkins server
10+
11+
Machine 1 can be your personal Laptop or a cloud VM.
12+
13+
Machine 2 *has* to be a cloud VM (such as a EC2 instance).
14+
You will need to use a publicly available VM instance for Machine2
15+
Feel free to choose either a EC2 instance or / Google Compute Engine instance
16+
Make sure you allow TCP traffic to port 8080 on your VM instance.
17+
18+
19+
Setup of your developer workspace/Machine 1:
20+
---------------------------------------------
21+
1) Install Google Cloud CLI
22+
- sudo apt-get install python
23+
- curl https://sdk.cloud.google.com | bash
24+
25+
2) Configure authentication for gcloud CLI
26+
- gcloud auth login --> Follow the prompts (you will have to open browser window and paste the generated link,
27+
then paste the generated code in the verification field in your console.)
28+
- Create Project in Google Cloud Console --> Note down the Project ID. Remember that Project ID is different than the Project's Name.
29+
You will need Project ID in subsequent steps.
30+
- export PROJECT_ID=<Project-ID-from-previous-step>
31+
- export CLOUDSDK_COMPUTE_ZONE=us-central1-b
32+
- gcloud config set project ${PROJECT_ID}
33+
34+
3) Create a Google Cloud SQL instance
35+
- gcloud sql instances create instance1 --tier=db-f1-micro --authorized-networks=0.0.0.0/0
36+
37+
4) Create testdb database on the Cloud SQL instance
38+
- gcloud sql users set-password root % --instance instance1 --password 'testpass123!@#'
39+
- sudo apt-get install mysql-client
40+
- gcloud sql instances list
41+
- mysql -h <IP-address-of-SQL-Instance> --user=root --password='testpass123!@#'
42+
- mysql>create database testdb;
43+
44+
5) Create your Bitbucket repository named "assignment6"
45+
- Make it Public
46+
47+
6) Download Sample CI/CD project
48+
- git clone https://github.com/devdattakulkarni/CloudComputing
49+
- cd CICD/greetings
50+
- Edit greetings-deployment.yaml and set the environment variables corresponding to
51+
the Google Cloud SQL instance
52+
(Note: You don't need to change the image URI)
53+
- Add a remote for your Bitbucket repository and Push the sample CI/CD project to that remote
54+
- cd ..
55+
- git remote add origin1 git@bitbucket.org:<your-bitbucket-username>/assignment6.git
56+
- git push origin1 master
57+
58+
59+
60+
Setup of Jenkins Server/Machine 2:
61+
-----------------------------------
62+
1) Jenkins Server Installation preparation:
63+
- Login to your Cloud VM instance (Machine2)
64+
- Install Java (JRE and JDK)
65+
- sudo apt-add-repository ppa:openjdk-r/ppa
66+
- sudo apt-get install -y openjdk-8-jre openjdk-8-jdk wget
67+
- Install Google Cloud CLI
68+
- sudo apt-get install python
69+
- curl https://sdk.cloud.google.com | bash
70+
- Install Kubectl
71+
- gcloud components install kubectl
72+
- Install Docker
73+
- git clone https://github.com/devdattakulkarni/CloudComputing.git
74+
- cd Containers/Kubernetes-examples/GCP/
75+
- ./install-docker-ubuntu.sh
76+
- exit from Cloud VM instance
77+
- SSH back into Cloud VM EC2 instance
78+
79+
2) Download and Start Jenkins Server:
80+
- wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
81+
- nohup java -jar jenkins.war --httpPort=8080 &
82+
83+
3) Configure Jenkins Server
84+
- Point your browser to the DNS name of the Jenkins Server at port 8080
85+
https://<DNS-of-Jenkins-Server>:8080/
86+
- Keep the user as 'admin'
87+
- Install recommended plugins
88+
89+
4) Once Jenkins starts up do following:
90+
4.1) Login as admin user
91+
- Password is stored in: $HOME/.jenkins/secrets/initialAdminPassword
92+
4.2) Set the path in Jenkins for Git
93+
Jenkins -> Manage Jenkins -> Configure -> Global Properties
94+
- Check Tool Locations
95+
- List of tool locations (Choose Git)
96+
- Set the path of Git from your VM
97+
(You can find the path by running 'which git' on VM)
98+
4.3) Install the Post build task Plugin
99+
Jenkins -> Manage Jenkins -> Manage Plugins -> Post build task plugin
100+
4.4) Install Bitbucket Plugin
101+
Jenkins -> Manage Jenkins -> Manage Plugins -> Bitbucket
102+
103+
5) Add a WebHook to your Bitbucket repository
104+
5.1) Go to your Bitbucket repository -> Settings -> URL
105+
- http://ec2-34-212-126-125.us-west-2.compute.amazonaws.com:8080/bitbucket-hook/
106+
- Check Status to Active
107+
- Check "Skip certificate verification"
108+
- Triggers -> Choose from a full list of triggers
109+
- Select following:
110+
- Repository->Push, Pull Request->Created, Updated, Comment created, Comment updated, Comment deleted
111+
Issue->Created, Issue->Updated, Issue->Comment Created
112+
5.2) Generate a SSH Key pair and add it to your Bitbucket account
113+
- Account -> Settings -> Add Key
114+
- You can either generate an SSH key or add an existing key if you have one
115+
116+
6) Set up a Jenkins Job
117+
6.1) Make sure your Bitbucket repository ("assignment6") is Public
118+
- This will make it possible to be cloned by Jenkins.
119+
6.2) Jenkins -> New Item -> Give name -> Select Freestyle project
120+
-> Source Code Management -> Give Bitbucket Repository URL (in git)
121+
6.3) Build Triggers
122+
-> Choose "Build when a change is pushed to BitBucket"
123+
6.4) Buid Section -> Choose Execute Shell script
124+
-> In the Execute shell Command section:
125+
- Add contents of build-steps.txt (first modify build-steps.txt to use your GCP Project ID)
126+
6.5) In Post-build Actions
127+
-> Tasks -> Log text "SUCCESS" -> Operation "-- OR --"
128+
-> In the Tasks -> Script section:
129+
- Add contents of deploy-steps.txt (modify the steps with your assignment's name)
130+
131+
132+
133+
CI/CD Experiment:
134+
-----------------
135+
1) Make changes to your code and push the code:
136+
- Modify index.html
137+
- git add
138+
- git commit
139+
- git push origin1 master
140+
141+
2) Open
142+
143+
144+
145+
146+
147+
148+
149+
Help Commands:
150+
---------------
151+
1) Restarting Jenkins:
152+
- From EC2 instance find out Jenkins process
153+
- ps -eaf | grep java
154+
- kill <process-id>
155+
- Re-run Jenkins using the java command
156+
157+
158+
Reference:
159+
-----------
160+
- https://cloud.google.com/kubernetes-engine/docs/tutorials/http-balancer
161+
- https://blog.terrenceryan.com/index.php/making-kubernetes-ip-addresses-static-on-google-container-engine/
162+
163+

CICD/deploy-steps.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
echo "This is the deploy step"
2+
cd Containers/Kubernetes-examples/GCP/greetings
3+
kubectl delete deployment greetings-deployment || echo "greetings-deployment deployment does not exist"
4+
kubectl delete service greetings-deployment || echo "greetings-deployment service does not exist"
5+
kubectl delete ingress greetings-ingress || echo "greetings-ingress does not exist"
6+
7+
kubectl create -f greetings-deployment.yaml
8+
kubectl expose deployment greetings-deployment --target-port=5000 --type=NodePort
9+
10+
kubectl apply -f greetings-ingress.yaml
11+
12+
echo "Done deploying"

CICD/deploy-steps.txt~

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
echo "This is the deploy step"
2+
cd Containers/Kubernetes-examples/GCP/greetings
3+
kubectl delete deployment greetings-deployment || echo "greetings-deployment deployment does not exist"
4+
kubectl delete service greetings-deployment || echo "greetings-deployment service does not exist"
5+
kubectl delete ingress greetings-ingress-1 || echo "greetings-ingress does not exist"
6+
7+
kubectl create -f greetings-deployment.yaml
8+
kubectl expose deployment greetings-deployment --target-port=5000 --type=NodePort
9+
10+
kubectl apply -f greetings-ingress.yaml
11+
12+
echo "Done deploying"

0 commit comments

Comments
 (0)