Jenkins CI/CD with AWS CodeBuild and AWS CodeDeploy
Continuous integration and continuous deployment for a python flask web application.
Architecture
Installation
1. Launch an EC2 instance as config host, clone repository from Github
git clone https://github.com/dragonflly/Jenkins-AWS-CICD.git
Repository folder tree:
└── Jenkins-AWS-CICD/
├── application
├── cfn-template
├── iam-resource
├── images
└── README.md
2. Create flask web application repository in Github
- Create a new repository in your Gitbub account (Example: Jenkins-app)
- Clone Jenkins-app
git clone https://github.com/
/Jenkins-app.git
- Copy flask web application to Jenkins-app, then push
cp -r Jenkins-AWS-CICD/application/* Jenkins-app
cd Jenkins-app
git add .
git commit -m "flask web initial"
git push
3. Create SSH keyPair
- Create a KeyPair on AWS console (Example: KeyPair-us-east-1 in us-east-1)
4. Create a CFN templates S3 bucket and upload templates
- Create a S3 bucket (Example: jenkins-cicd-cfn-templates in us-east-1)
aws s3api create-bucket --bucket jenkins-cicd-cfn-templates --region us-east-1
- Copy CFN templates to the S3 bucket
aws s3 cp Jenkins-AWS-CICD s3://jenkins-cicd-cfn-templates/Jenkins-AWS-CICD --recursive
5. Create whole AWS infrastructure as Code (IaC) with CloudFormation
- Create stack from AWS CloudFormation console, specify template source by Amazon S3 URL
https://jenkins-cicd-cfn-templates.s3.amazonaws.com/Jenkins-AWS-CICD/cfn-template/cicd-Jenkins.yaml
- Specify stack input parameter from console
Stack name: Jenkins-cicd
KeyPairName: KeyPair-us-east-1
TemplateS3Bucket: jenkins-cicd-cfn-templates
It will takes approximately 6 minutes, and CloudFormation stack Outputs tab:
6. Unlock Jenkins server, and initialize Jenkins
cat /var/lib/jenkins/secrets/initialAdminPassword
-
Copy the JenkinsServerAddr value (Example: 34.205.33.197:8080) from the CloudFormation stack Outputs tab, and paste it into browser
-
Paste initialAdminPassword into Administrator password, unlock Jenkins server
-
Waiting a few minutes, Jenkins will be ready!
7. Add Jenkins plugin
- Click on Manage Jenkins, then Manage Plugins
- Search Available and select the below plugins, then choose Install without restart
AWS CodeDeploy
AWS CodeBuild
Http Request
File Operations
-
It will take couple of minutes to download the plugins then restart
8. Create a Jenkins CICD project
-
Choose New Item
Enter an item name (Example: Jenkins-CICD)
Select Freestyle project
-
Under Source Code Management
Choose Git
Enter HTTPS of Jenkins-app.git into Repository URL
Change Branch Specifier from master to main
-
Under Build Triggers
Select Poll SCM
Enter H/2 * * * * into Schedule, to poll GitHub every two minutes
-
Under Build Environment
Select Delete workspace before build starts
9. Add AWS CodeBuild
-
Under AWS Configuration
Choose Manually specify access and secret keys
Input AWS Access Key
Input AWS Secret Key
-
Under Project Configuration
Enter AWS Region (Example: us-east-1)
Enter CodeBuild Project Name, copy from CloudFormation Outputs tab
Select Use Jenkins source
10. Add File Operation
-
Under Build
Select Add build step
Choose File Operation -
Under File Delete
Input asterisk, to delete all files which from Github after zip and upload
11. Add HTTP Request
-
Under Build
Select Add build step
Choose HTTP Request -
Under HTTP Request
Enter URL: "https://" + "S3BucketName" + ".s3.amazonaws.com/codebuild-artifact.zip". S3BucketName is copied from CloudFormation Outputs tab (Example: https://jenkins-cicd-codedeploybucket-1v4is9an14r5p.s3.amazonaws.com/codebuild-artifact.zip)
Choose Yes for Ignore Ssl errors
-
Choose Advanced, under Response
Enter codebuild-artifact.zip into Output response to file
12. Add File Operation
-
Under Build
Select Add build step
Choose File Operation -
Under File Operation
Select Add, then Unzip
Select Add, then File Delete -
Under Zip File
Enter codebuild-artifact.zip into Zip File -
Under File Delete
Enter codebuild-artifact.zip into Include File Pattern
13. Add AWS CodeDeploy
-
Under Post-build Actions
Select Add post-build action
Choose Deploy an application to AWS CodeDeploy
-
Under Deploy an application to AWS CodeDeploy
Fill AWS CodeDeploy Application Name which copied from Outputs tab
Fill AWS CodeDeploy Deployment Group which copied from Outputs tab
Fill AWS CodeDeploy Deployment Config with CodeDeployDefault.OneAtATime
Fill AWS Region (Example: us-east-1)
Fill S3 Bucket which copied from Outputs tab
-
Select Deploy Revision
-
Click SAVE to finish configuration
14. Build and check
-
CICD finished, copy ALBURL from Outputs tab, paste into browser
Python flask web application should be
-
In flask application repository Jenkins-app
Change background-color from blue to red in application/templates/Jenkins-page.html
Commit change, trigger CI/CD in two minutes
License Summary
This code is made available under the MIT license. See the LICENSE file.