Jenkins Pipeline

Building Pipeline on Jenkins

As a best practice for DevOps, it is important to setup a Pipeline that provides the developers with end-end automation from the code repository to the deploy stage. Also, it offers a visibility into the various stages of the Pipeline.
We will now build a Pipeline Job on Jenkins, which enables you to track the status of the individual stages in a Job as shown below: dashboard For that, we will use a pre-built Jenkinsfile that is written in groovy. You may check the GitHub repo provided below to dig in into the Jenkinsfile to understand how the stages are built.

Step1: Install Pipeline Plugin
First we will install plugin for the Pipeline jobs. Go to Manage Jenkins -> Manage Plugins -> Go to Available tab -> Type in Filter search textbox for Pipeline -> Select it -> Install and Restart Jenkins.
dashboard

Step2: Create AWS Credentials entry in Jenkins

Note: If you are using AWS Event Engine (a platform to provide accounts for AWS customers to run workshops), then you may need to run this extra step. Go to Services -> IAM -> Add User -> Provide Administrator permissions by assigning Administrator Policy -> Note down the Access and Secret keys. We will use these keys instead to create Credentials on Jenkins as per below steps.

Go to Jenkins Main Menu -> Select Credentials -> Select System from left pane -> Click Global credentials (unrestricted) -> Select Add Credentials -> Select Kind to be AWS Credentials -> Fill up your Access Key and Secret Access key textbox -> OK
dashboard Note down the ID of the newly created credentials. dashboard

Step3: Setting up GitHub Repo
We will setup the GitHub to send a WebHook signal to Jenkins Server whenever there is a new code push. Follow these steps:

(a) Fork the Repo and edit the credentials:
Login to your GitHub account and fork the repository from this URL: https://github.com/aws-samples/amazon-eks-cdk-blue-green-cicd dashboard Once done, go to the forked repo in your GitHub account -> access the amazon-eks-cdk-blue-green-cicd/cicd/Jenkinsfile -> Edit the file
Replace the value of the credentialsId with the above ID noted in 4 places (stages). Once done, save -> commit with a comment. dashboard (b) Setup WebHook:
Go to your Repository and select Settings, then from left pane WebHook, then click Add Webhook
dashboard dashboard dashboard In the Payload URL, type http://jenkins-elb-dns-name/github-webhook/
In the Content Type, select application/json dashboard Complete the saving of configuration and now your Webhook is setup to send the push notification to your Jenkins server.

Step4: Configure Pipeline Job
Once installed, now configure the pipeline job as shown below. Select New Item -> Give the name and select Pipeline -> OK
dashboard

Select GitHub Project and type in the URL pointing to the forked repo inyour GitHub account as shown below: dashboard Also, check mark the field GitHub hook trigger for GITScm polling

Select Pipeline script from SCM -> Select Git
dashboard In the field Repository URL, type: https://github.com/YOUR-USER-ID/amazon-eks-cdk-blue-green-cicd.git, leave Credentials as none -> branch as Master
Select Additional Behaviours -> Check out to a sub-directory -> Type in amazon-eks-cicd-codebuild-eks-alb-bg
dashboard In the Script Path, type cicd/Jenkinsfile -> Save dashboard

Step5: Run the Pipeline Job
You can trigger the build job by either making a code change in your GitHub account OR by manually triggerring the build. To make the code change, you may edit the file: flask-docker-app/templates/hello.html as shown below to change the background color to Yellow by modifying string to "background-color: #FFFF00" OR Red color using string "background-color: #b03043".
dashboard If need to manually trigger, go to the Jenkins dashboard and select the build job: CICD-to-EKS-job. Click Build Now and observe the progress across the stages: dashboard

At the last stage, the Pipeline will stall and wait for user-inputs to swap the Blue/Green service on the EKS as shown below: dashboard dashboard

Depending upon the version of Jenkins, you may have to go to the Console Output of the Job and click Proceed to complete the last stage. dashboard

Once completed, the build pipeline will show all stages in green as below: dashboard

You may now check the output from your Flask ALB Load Balancer DNS: dashboard