- Inbound rule- Allow port 22(SSH),80(HTTP) & 443(HTTPs)
- Outbount rule - All traffic
- Create and EC2 Instance->
Jenkins-Master
& use AMI - ubuntu 22.2.0 - Create a Key pair(Save it in you local machine to SSH into Jenkins-Master Server)
- Attached the previously created sceurity group(Jenkins-Master)
- Create an Elastic IP and attach to the EC2 Instance
→ switch to super user `sudo su -` &</br>
→ get updates `apt-get update` & upgrade the packages `apt-get upgrade -y` </br>
- Install java-17 →
apt install openjdk-17-jdk -y
- Add Jenkins Repository to ubuntu system and install Jenkins
- Importing GPG Key, The GPG key verifies package integrity
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
- Importing GPG Key, The GPG key verifies package integrity
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
- Add the Jenkins software repository to the source list and provide the authentication key
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
- installing jenkins (Update the system repository one more time. Updating refreshes the cache and makes the system aware of the new Jenkins repository)
apt update
apt install jenkins -y
- Check the running status of Jenkins
systemctl status jenkins | grep Active
-
Install nginx
apt install nginx -y
-
remove the default configuration file for Nginx, which is stored in the
/etc/nginx/sites-enabled
directory use command:unlink /etc/nginx/sites-enabled/default
-
open the Vim text editor and creates a new configuration file for Nginx, specifically for Jenkins.
vim /etc/nginx/conf.d/jenkins.conf
This file will be stored in the
/etc/nginx/conf.d
directory. -
using Vim, add the following lines to the configuration file:
upstream jenkins { server 127.0.0.1:8080; } server { listen 80 default_server; listen [::]:80 default_server; location / { proxy_pass http://jenkins; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
-
These lines configure Nginx to act as a reverse proxy for Jenkins. The
upstream
block defines a group of servers that Nginx can proxy requests to, in this case only one server running on127.0.0.1
on port8080
. Theserver
block defines a virtual server that listens on port80 (HTTP)
and forwards requests to the Jenkins server defined In theupstream
block. Thelocation
block defines how requests to the root URL (/) should be handled, by passing them on to the Jenkins server and setting some HTTP headers in the process. -
Run
nginx -t
to test the configuration file of the Nginx web server for syntax errors
When this command is executed, Nginx parses the configuration file and checks for any syntax errors or issues that might prevent Nginx from starting properly. If any errors are found, the command will print an error message and exit with a non-zero status code. -
systemctl reload nginx
→ reload the configuration of the Nginx web server without stopping it.
[4.1] Creating IAM role, Security group and Key pair for build-Server
- AWS Console → IAM console → "Roles" (in left navigation menu).
- Click "Create role" button.
- Choose "AWS service" as the trusted entity → "EC2" as the service that will use the role.
- Click on "Next: Permissions" button. Search for the "AWS Elastic Beanstalk Full" it will give full access to the build-server.
- (Optional) Add tags to the role for better organization and tracking → Click on "Next: Review" button.
- Give the role a name and description, and review the policies attached to the rol & Click on the "Create role" button to create the role.
- Go to the EC2 console and select the Jenkins-Slave Ec2 instance.
- Click on the "Actions" button and select "Instance Settings" and then "Attach/Replace IAM Role" & Select the role you just created and click on the "Apply" button.
- Create a secutiry group and Key Pair for Build server save the private key to you local system.
Security Group :
In bound rules: Allow SSH (80) from SG Jenkins-Master created for Jenkins-Master server & Allow SSH from 0.0.0.0/0 . - Create Jenkins-Slave EC2 instance with same configuration as Jenkins-Master add the IAM role choose Jenkins-Slave SG and Create a new key pair and save the .pem file.
[4.2]FConnecting Jenkins-Master & Jenkins-Slave
- Start the jenkins-build server and connect using SSH and run following commands:
#!/bin/bash
#install dependecies
yum -y group install "Development Tools"
yum -y install bzip2-devel.x86_64
yum -y install openjdk-17-jdk -y
yum -y install libffi-devel
yum -y install ncurses-devel
yum -y install openssl-devel
yum -y install python3
yum -y install readline-devel.x86_64
yum -y install sqlite-devel.x86_64
yum -y install zlib-devel
curl -0 https://bootstrap,pypa,io/get-pip.py
/usr/bin.python get-pip,py
/usr/local/bin/pip3 install awsebcli
→ The above commands can be used in user data so that when the build-server boots up everything is ready automatically.
2. Open Jenkins(Jenkins-Master) → (left menu) 'Build Executor Status'
3. Follow the below step to create a new node:
1. Click on "Build Executors status"
- Copy the private Ipv4 address of jenkins-master server
What is a webhook?
→ A webhook is a way for an application or service to provide real-time information to another application or service. It is a simple event-notification mechanism that sends data to a URL (also known as a webhook endpoint) when a particular event occurs.When an event occurs in the source application, the application will generate a message that contains information about the event and sends it to the webhook endpoint. The receiving application can then process the message and use the information to take further actions or trigger additional events.
Creating a webhook and testing it for first time:
- Go to jenkins server →
New Item
→ Name the job (webhook test) choosefreestyle
→ General: paste the github url in the GitHub project option
→SCM : choose GitHub hook trigger for GITScm polling
→Build Steps: Execute shell
→command cat README.md
→ Apply & Save
- Create a github repository (keep it public)
- go to : settings → webhooks → add webhook → paste the DNS link of Jenkins-Master add
github-webhook/
at the end of url → This url is endpoint in our jenkins server which will handle the webhook events(here,any update on github repo)
Now make some changed in your README.md file and commit to github, you webhook-test job sould automatically run
it should look like this→ http://ec2-43-205-2-42.ap-south-1.compute.amazonaws.com/github-webhook/
-
[6] Deployment to Elastic Beanstalk
→ Go to AWS Beanstalk console create a new Beanstalk application and name it
Jenkins-Beanstalk
→ Create a Jenkins freestyle job:
→ General : Add the github url in github project .
→ Source Code Management : check- Git → Repository URL : add the url of repo & change Branch Specifier : */main accordingly. In additional behaviours select Check out to specific local branch
→ Build Triggers : GitHub hook trigger for GITScm polling
→ Build Steps : add the below code in the command and save
#!/bin/bash -xe
APPLICATION=Jenkins-Beanstalk
REGION=ap-south-1
ENVIRONMENT=Jenkinsbeanstalk-env
PLATFORM=python-3.8
# get identity
aws sts get-caller-identity
# intialize the application
eb init "${APPLICATION}" --platform "${PLATFORM}" --region "${REGION}"
# select the environment
eb use "${ENVIRONMENT}"
# deploy the application
eb deploy
# get the deployment's health and status information
eb health
eb status
→ Check your aws beanstalk console for environement updation you make chenges to the webapp file to see noticeable changes or can check the job console where the commit message can also confirm that everything is fine!