CI/CD using Jenkins on AWS

Introduction

Jenkins is an open source automation server which enables developers around the world to reliably build, test, and deploy their software. In this article, we will demonstrate how to use Jenkins on AWS to deploy an application.

Solution Diagram

First of all, there is a prepared sample application, we have to clone it. The code will be added to Jenkins. Then we will demonstrate how to perform CI/CD using Jenkins.

Create Resources Using CloudFormation

Please click here to kick start the CloudFormation. This templates will create resources to build the infrastructure, including the Jenkins server, CodeBuild project, and CodeDeploy application. If you have no preferences, just follow my options. For KeyName, PublicSubnet1, PublicSubnet2, VpcId and YourIPrange, you have to prepare by yourself. But if you are not sure your public ip, just type 0.0.0.0/0. Click Create stack.

After creation, we can click Outputs and click the link of ELBDNSName, we are able to see a welcome page.

Access Jenkins Server

In CloudFormation, we already deployed a Jenkins server and we can check our Jenkins server by clicking the URL.

Then it will ask you a password. The password is located on Jenkins server.

Go to EC2 page, select Jenkins server and connect it through any methods you like.

Here we choose putty to access. Login with ec2-user than type below command.

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Then it will show the password here, copy and paste into the Jenkins web GUI and click continue.

It will ask you choose how to install, just choose install suggested plugins.

Type your own admin account and click save and continue.

Save and Finish.

Start using Jenkins.

Configure Jenkins

Click Manage Jenkins=>Manage Plugins.

Search for AWS CodeDeploy on Available tab and isntall withough restart. Then go back to Manage Plugins.

Do the same things to install AWS CodeBuild, Http Request and File Operations as well.

Jenkins Available

Check the “Restart Jenkins when installation is complete and no jobs are running” field.

After restart, we click new item.

Give a name to the item, choose Freestyle project then click OK.

In source code management, we choose Git and input our prepared repository. You may clone my repository to your own github first.

https://github.com/manbobo2002/sample-python-application.git

In Build Triggers, we choose Poll SCM with schedule H/2 * * * *. It means poll GitHub every two minutes for updates.

In Build Environment, we check “Delete workspace before build starts” to cleanup our workspace.

In Build Actions, we select AWS CodeBuild.

Details

Type the access key and secret key.

Type the region, project name you created before. Select AES256 for server side encryption algorithm.

Scroll down and select Add build step => File Operations.

Select File Delete and type * in file pattern.

Add HTTP Request.

The URL should be http://s3-us-east-1.amazonaws.com/{bucketname}/codebuild-artifact.zip and click Advanced.

Bucket Name can be found on CloudFormation=>Outputs.

Type codebuild-artifact.zip in output response to file under response.

Add one more File Operations and type codebuild-artifact.zip in zip file and include file pattern.

Finally, click Add post-build action and select deploy an application to AWS CodeDeploy.

Follow my input except S3 Bucket you have to input the name provided in CloudFormation Output.

Finally, select Deploy Revision and check “Wait for deployment to finish”. Click Save.

Test CI/CD With Jenkins

We choose a file to edit something and commit.

Deploy

We can see the build history is working now.

If we are success, we can see this page in our load balancer IP.

Cleanup

Delete the stack.

Leave a Reply