CI - CD Pipeline With Jenkins On AWS - by Edmar Barros - FAUN - Medium
CI - CD Pipeline With Jenkins On AWS - by Edmar Barros - FAUN - Medium
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 1/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
Manual tests and deployments can lead to multiple errors that a well de ned CI/CD
pipeline can avoid while testing your application, inspect the code quality, deploying
and… I could enumerate many others aspects of why CI/CD is a must have but let me
drive you through the setup process instead!
Jenkins is one of the most popular Open Source CI tools on the market. It o ers support
for multiples SCM and many other 3rd party apps via its plugins. And, with concepts like
Pipeline-as-Code, the entire build process can be checked into a SCM and versioned like
the rest of your code.
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 2/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
… What is a Pipeline?
A continuous delivery (CD) pipeline is an automated expression of your process for getting
software from version control right through to your users and customers.—
https://jenkins.io/doc/book/pipeline/
The Pipeline de nes a set of action that Jenkins will execute in order to test, build and
deploy your application. You can de ne Stages per branches, run commands in parallel,
de ne environment variables and much more.
In this article I’m going to de ne a Pipeline to test and deploy a static web application
into AWS S3.
First, let’s update the repositories and install Docker and Git:
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 3/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
To allow Jenkins to build our Docker images, we need to add the jenkins user to the
docker group:
Add the Jenkins and Docker services to start on boot and start both services:
As Jenkins typically uses port TCP/8080, we’ll con gure iptables routing rules to redirect
the tra c:
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 4/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
I’m not covering the usage of SSL certi cates, to use HTTPS you need to include rules to
redirect tra c for port 443:
You can nd more about SSL certi cates on this article from Paul Lessing:
medium.com
At this point you should be able to see the Jenkins home page using the public DNS
name of your EC2 instance(e.g. ec2–<ec2-public-ip-address>.compute-
1.amazonaws.com):
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 5/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
Install the recommended plugins and create an Admin account in the following steps.
At this point you should be able to login an see the following page:
At the Jenkins home page on the left menu select Manage Jenkins -> Manage Plugins
select the tab Available and search for the following plugins:
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 6/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
At the Jenkins home page on the left menu click at Credentials->System, select the scope
global and at the left menu again Add Credential:
Provide the AWS IAM Credentials to allow Jenkins Pipeline AWS Plugin to access your
S3 Bucket. At the above image, insert the created Access Key ID and the Secret Access Key.
You can de ne and ID that will be used at the Jenkins le con guration as the following
example:
withAWS(region:'<your-bucket-region>',credentials:'<Jenkins-
Credential-ID-AWS>') {
Jenkins Blue Ocean UI makes it easier to create and con gure a new pipeline.
2. Enter a Personal Access Token from GitHub to allow Jenkins to access and scan your
repositories.
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 7/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
3. Follow the instructions and the Blue Ocean will look for a Jenkins le, if does not
exists Blue Ocean allows you to de ne a new le and con gure the Pipeline.
If you create a Pipeline with Blue Ocean it will commit the Jenkins le to your repository
and it will have a structure similar to the following:
1 pipeline {
2 agent {
3 docker {
4 image 'node:10-alpine'
5 args '-p 20001-20100:3000'
6 }
7 }
8 environment {
9 CI = 'true'
10 HOME = '.'
11 npm_config_cache = 'npm-cache'
12 }
13 stages {
14 stage('Install Packages') {
15 steps {
16 sh 'npm install'
17 }
18 }
19 stage('Test and Build') {
20 parallel {
21 stage('Run Tests') {
22 steps {
23 sh 'npm run test'
24 }
25 }
26 stage('Create Build Artifacts') {
27 steps {
28 sh 'npm run build'
29 }
30 }
31 }
32 }
33 stage('Deployment') {
34 parallel {
35 stage('Staging') {
36 when {
37 branch 'staging'
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 8/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
38 }
39 steps {
40 withAWS(region:'<your-bucket-region>',credentials:'<AWS-Staging-Jenkins-Credential-ID
41 s3Delete(bucket: '<bucket-name>', path:'**/*')
42 s3Upload(bucket: '<bucket-name>', workingDir:'build', includePathPattern:'**/*');
43 }
44 mail(subject: 'Staging Build', body: 'New Deployment to Staging', to: 'jenkins-maili
45 }
46 }
47 stage('Production') {
48 when {
49 branch 'master'
50 }
51 steps {
52 withAWS(region:'<your-bucket-region>',credentials:'<AWS-Production-Jenkins-Credentia
53 s3Delete(bucket: '<bucket-name>', path:'**/*')
54 s3Upload(bucket: '<bucket-name>', workingDir:'build', includePathPattern:'**/*');
55 }
56 mail(subject: 'Production Build', body: 'New Deployment to Production', to: 'jenkins
57 }
58 }
59 }
60 }
61 }
62 }
The above Jenkins le has a pipeline as you can see at the following image:
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 9/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
The Pipeline above uses a docker agent to test the application and to create the nal
build les. The Deployment stage will remove the les from a given S3 bucket, upload
the new build les and send an email to inform that a new deployment was successfully
completed.
One thing to note is at the agent de nition it used a port range is used to avoid the job to
fail in case a given port is already in use
A WebHook is an HTTP callback: an HTTP POST that occurs when something happens;
a simple event-noti cation via HTTP POST. A GitHub Webhook allows Jenkins to
subscribe to repository events like Pushes and Pull Requests.
The Webhook will send a POST request to your Jenkins instance to notify that an action
was triggered(PR, Push, Commit…) and it should run a new job over the code changes.
Go to you repository Settings, select Webhooks on the left menu and click on the
button Add webhook.
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 10/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
Select the individual events that you want to be used to trigger a new Jenkins build. I
recommend to select Pushes and Pull Requests.
After that you will be able to see a similar message after a PR is created and it passes
Jenkins validation:
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 11/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
At your AWS console go to Services -> S3 and click the Create bucket button. Choose
you bucket name and region, untick both options under the Manage public bucket
polices for this bucket and nish the bucket creation. Open the bucket Properties, turn
on the Static website hosting and de ne the index.html to point to your entry le like the
following image:
To use a custom domain name you need to create a second bucket and under Static
website hosting select the Redirect requests option. That option allows you to de ne the
domain that will be used to access the hosting bucket. Bear in mind that this secondary
bucket will use a HTTP status code 301 to redirect the requests.
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 12/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
At this point you should be able to access your website using you custom domain you via
the bucket
http://<bucket-name>.s3-website-<region>.amazonaws.com
Conclusion
This process has multiple pros and cons and I would like to drop my two cents on this
and highlight a couple of them.
Pros
Cons
Cache! Updates may take a while to be propagated as it has to wait the cache to
expire.
Server side rendering. Bots and crawler won’t be able to get any metadata.
Despite the cache, that can be handled by setting to the index.html a short maxAge, this
solution can be considered robust and sustainable as it takes advantages of multiple
AWS resources to make its availability and scalability to follow high standards as it is
required nowadays.
. . .
References
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 13/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
Lessing, Paul. ‘Single-Page Apps on AWS, Part 1: Hosting a Website on S3’. Medium, 16
Apr. 2018, https://medium.com/@P_Lessing/single-page-apps-on-aws-part-1-hosting-
a-website-on-s3-3c9871f126.
. . .
Learn more
Code, Quick. ‘Top Tutorials To Learn Jenkins CI For Testing Automation’. Medium, 4
June 2018, https://medium.com/quick-code/top-tutorials-to-learn-jenkins-ci-for-
testing-automation-93c7ac068f66.
Join our community Slack and read our weekly Faun topics ⬇
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 👏 14/15
9/22/2020 CI/CD Pipeline with Jenkins on AWS | by Edmar Barros | FAUN | Medium
If this post was helpful, please click the clap 👏 button below a few times to
show your support for the author! ⬇
https://medium.com/faun/ci-cd-pipeline-with-jenkins-and-aws-s3-c08a3656d381 15/15