Day 80-Project 1 (DevOps)
2 min read
Jenkins CI/CD pipeline with GitHub webhook integration for Deploying Docker applications on EC2 instances
prerequisite:
AWS EC2 instance with docker and Jenkins installed in it.
For creating an EC2 instance:
· AMI: ubuntu.
· Instance type: t2.micro (free tier).
· Key pair login: Create > docker.pem.
· Allow HTTP.
· Allow HTTPS.
. Docker installation:
sudo apt install Docker.io -y
. Jenkins installation: Link
. Allow TCP on ports 8000 and 8080 by editing the inbound security group.
SSH into the EC2 instance just created. Create SSH private and public keys by
ssh-keygen
command.cd .ssh
to see the keys.Setup Jenkins, you can check my blog on it: (You can check the Jenkin server at
http://<public_ip_addr_ec2>:8080/
)https://sri766.hashnode.dev/jenkins
After configuring Jenkins, just log in with a username and password
Click on Create an Item, Name: Todo-app, FreeStyle project.
fill description👇
add git URL: In my case it's
https://github.com/sri766/node-todo-cicd.git
(fork it:
https://github.com/LondheShubham153/node-todo-cicd
)add credential: You can add an access token as a password as I did here!👇
Add build Steps Execute Shell
sudo docker build -t todo-app . sudo docker run -p 8000:8000 -d todo-app
Hit save and Run Build.
Note: Make sure no docker container is running on port 8000. You can check docker ps or
lsof i:8000
and if it is running just copy the contained ID and rundocker kill <paste_copied_id>
Now, our aim is to integrate GitHub webhook to Jenkins to make a ci/cd pipeline
prerequisite: GitHub integration plugin to be installed in Jenkins, you can check it, Goto manage Jenkins -> plugins -> available plugins->(search)GitHub integration
Now add a public SSH key to the Github
settings ->SSH and GPG keys -> new SSH key
You can find the public key in
.ssh
directory in EC2 instance just copy it and paste it into GitHub.Add Webhook
payload URL:
http://<public_ip_addr>:8080/github-webhook/
type: application/JSON
active: true
NOTE: just check goto manage Jenkins->System->Jenkins URL, this must set to be http://<public_ip_addr>:8080
( AWS public IP got changed whenever it stopped and started again) and check under the Github server section valid credential is used or not.
Now, set GITScm polling checked.
Now, commit some changes it Github and Jenkins triggers build automatically.
Done!!🎉💕
credit: https://medium.com/@chetxn/project-1-implementation-3e4785af90f6
#90daysofdevops
Thank you for your patient reading.