|
| 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/ |
0 commit comments