How to CI/CD AWS With Github using Jenkins
- Mohammad Abu Mattar
- Cloud Computing , DevOps
- 07 Dec, 2022
- 13 Mins read
Introduction
In previous posts, I have shown you how to setup Jenkins on AWS EC2 instance. You can check the post here.
In this post, I will show you how to setup a CI/CD pipeline using Jenkins and Github to deploy a simple PHP application to Devlopment and Production environments on AWS. With this setup, you can deploy your application to AWS with a single click.
Prerequisites
- AWS CLI installed and configured
- IAM user with the following permissions:
- AmazonVPCFullAccess
- AmazonEC2FullAccess
Setup AWS Infrastructure for Devlopment Environment
Create VPC
Step 1: Create VPC
To create a VPC, run the following command:
Step 2: Modify your custom VPC and enable DNS hostname support, and DNS support
To modify your custom VPC and enable DNS hostname support, and DNS support, run the following command:
Step 3: Create a Public Subnet
To create a public subnet, run the following command:
Step 4: Enable Auto-assign Public IP on the subnet
To enable auto-assign public IP on the subnet, run the following command:
Step 5: Create an Internet Gateway
To create an internet gateway, run the following command:
Step 6: Attach the Internet Gateway to your VPC
To attach the internet gateway to your VPC, run the following command:
Step 7: Create a Route Table
To create a route table, run the following command:
Step 8: Create a custom route table association
To create a route in the route table, run the following command:
Step 9: Associate the subnet with route table, making it a public subnet
To associate the subnet with route table, making it a public subnet, run the following command:
Step 10: Create a Security Group
To create a security group, run the following command:
Step 11: Add inbound rules to the security group
To add inbound rules to the security group, run the following command:
Create a Two EC2 Instances
Step 1: Get the latest AMI ID for Amazon Linux 2
To get the latest AMI ID for Amazon Linux 2, run the following command:
Step 2: Create a Key Pair
To create a key pair, run the following command:
Step 3: Create a user data script
To create a user data script, run the following command:
Step 4: Create an EC2 instance
To create an EC2 instance, run the following command:
Step 5: Create an Elastic IP
To create an Elastic IP, run the following command:
Step 6: Associate the Elastic IP with the EC2 Instance
To associate the Elastic IP with the EC2 Instance, run the following command:
Step 7: Get the Public IP Address of the EC2 Instance
To get the public IP address of the EC2 Instance, run the following command:
Setup Simple PHP Application
Create a GitHub Repository
Step 1: Create a GitHub repository
To create a GitHub repository, run the following command:
or you can create a repository from the GitHub website, go to Create a new repository and create a new repository.
- Repository name: quick-test-jenkins
- Description: Quick Test Jenkins
- Public/Private: Private
- Initialize this repository with a README: No
- Add .gitignore: None
- Add a license: None
Step 2: Clone the GitHub repository
To clone the GitHub repository, run the following command:
Step 3: Create a new branch
To create a new branch, run the following command:
Create a Simple PHP Application
Step 1: Create the structure of the application
To create the structure of the application, run the following command:
Step 2: Create the env.php
file
To create the config.php
file, run the following command:
Step 3: Create the connection.php
file
To create the connection.php
file, run the following command:
Step 4: Create the controller file
To create the controller file, run the following command:
Step 5: Create the view file
To create the view file, run the following command:
Step 6: Create the index.php
file
To create the index.php
file, run the following command:
Step 7: Create the normalize.css
file
To create the normalize.css
file, run the following command:
Click to see the code of the `normalize.css` file
Step 8: Create the main.css
file
To create the main.css
file, run the following command:
Click to expand the code of the `main.css` file
Step 8: Add the files to the GitHub repository
To add the files to the GitHub repository, run the following command:
Create a Jenkins Pipeline
Step 1: Create a new Jenkins pipeline
To create a new Jenkins pipeline, run the following command:
Step 2: Add the code to the Jenkins pipeline
To add the code to the Jenkins pipeline, run the following command:
Step 3: Add the files to the GitHub repository
To add the files to the GitHub repository, run the following command:
Create a Jenkins Job
Step 1: Create a new Jenkins job
- Open the Jenkins dashboard at EC2 instance from previous blog post. Go to
http://<public-ip-address>:8080/
and login with the credentials you created in the previous blog post.
- Click on
New Item
and enter the name of the job. SelectPipeline
and click onOK
.
- Name:
Quick Test Jenkins Devlopment
- Select:
Pipeline
- Click on:
OK
- Configure the job.
- Description:
Quick Test Jenkins Devlopment
- Select:
Pipeline
- Definition:
Pipeline script from SCM
- SCM:
Git
- Repository URL:
https://github.com/MKAbuMattar/quick-test-jenkins.git
- Credentials:
Add
>Jenkins
>Global credentials (unrestricted)
>Add Credentials
>Username with password
>Kind: Username with password
>Username: <your-github-username>
>Password: <your-github-password>
>ID: <your-github-username>
>Description: <your-github-username>
>OK
>OK
>OK
- Branches to build:
*/devlopment
- Repository URL:
- Definition:
- Click on:
Save
Note: How to Add Git Credentials in Jenkins for private repositories on GitHub
Step 2: Build the Jenkins job
Before building the Jenkins job, you need to add the key pair to the Jenkins server.
- Connect to the Jenkins server using SSH.
- Login to the Jenkins server.
- Create a
.ssh
directory.
- Create a
aws-key-pair.pem
file.
- Change the permission of the
aws-key-pair.pem
file.
- Exit the Jenkins server.
- Click on
Build Now
to build the Jenkins job.
- Click on
Console Output
to see the build logs.
Step 3: Build the Jenkins job Automatically on GitHub push
- Go to
GitHub
> go toquick-test-jenkins
repository- Click on
Settings
- Click on
Webhooks
- Click on
Add webhook
- Payload URL:
http://<public-ip-address>:8080/github-webhook/
- Content type:
application/x-www-form-urlencoded
- Which events would you like to trigger this webhook?:
Just the push event.
- Active:
Yes
- Click on:
Add webhook
- Payload URL:
- Click on
- Click on
- Click on
- Go to
Jenkins
>Quick Test Jenkins Devlopment
job- Click on
Configure
- Select:
GitHub hook trigger for GITScm polling
- Click on:
Save
- Select:
- Click on
Step 4: Test the Jenkins job Automatically on GitHub push
We’ll add new files to the devlopment
branch and push them to the GitHub repository.
Before we push the files to the GitHub repository, we’ll open the EC2 instance in the browser to see the changes.
- Go to
Jenkins
>Quick Test Jenkins Devlopment
job >Build History
.
- Click on
Build #2
to see the build details.
- Open the EC2 instance in the browser to see the changes.
Step 5: We’ll do the Same Changes for Jenkinsfile
file
Step 6: Do Some Changes at Jenkins Configure for Development Jenkins Job
- Go to
Jenkins
>Quick Test Jenkins Devlopment
job >Configure
. - Scroll down to
Script Path
and change the value toJenkinsfile
to.build/dev/Jenkinsfile
.
Step 7: Setup the Production Jenkins job
the Production Jenkins job will be triggered automatically when the Development Jenkins job is successful merge to the main
branch.
- Click on
New Item
and enter the name of the job. SelectPipeline
and click onOK
.
- Name:
Quick Test Jenkins Production
- Select:
Pipeline
- Click on:
OK
- Configure the job.
- Description:
Quick Test Jenkins Production
- Select:
Poll SCM
- Schedule:
H/5 * * * *
- Select:
Ignore post-commit hooks
- Schedule:
- Select:
Pipeline
- Definition:
Pipeline script from SCM
- SCM:
Git
- Repository URL:
https://github.com/MKAbuMattar/quick-test-jenkins.git
- Credentials:
Add
>Jenkins
>Global credentials (unrestricted)
>Add Credentials
>Username with password
>Kind: Username with password
>Username: <your-github-username>
>Password: <your-github-password>
>ID: <your-github-username>
>Description: <your-github-username>
>OK
>OK
>OK
- Branches to build:
*/main
- Repository URL:
- Definition:
- Click on:
Save
Step 8: Go to GitHub and Merge the devlopment
branch to the main
branch
- Go to
GitHub
>quick-test-jenkins
repository >devlopment
branch >Pull requests
>New pull request
.
Check the EC2 instance in the browser to see the changes, and you’ll see the changes.
Conclusion
In this tutorial, we learned how to setup a CI/CD pipeline using Jenkins and GitHub. We also learned how to setup a Jenkins job to automatically deploy the code to the AWS EC2 instance.