Day 80 : Project 1- Automated Web Application Development and Deployment with Jenkins and GitHub Integration
Main Highlights of this project:
Provisioning 2 EC2 instances, designated as the Master Node and Worker Node.
Configured Jenkins on the Master Node and installed Java on the Worker Node.
Task allocation from the Master Node to the Worker Node for parallel processing.
Implemented a declarative pipeline with the following stages:
a. Code Clone
b. Code Build
c. Code Test
d. Code Deploy
Steps:
Setting up Jenkins server:
- Create 2 AWS instances named them Master and Worker.
2. In the master node install Java and Jenkins:
# Install Java
sudo apt install -y default-jdk# Add Jenkins repository key
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -# Add Jenkins repository to the package sources
echo "deb http://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list# Update package list again to include Jenkins repository
sudo apt update# Install Jenkins
sudo apt install -y jenkins# Start Jenkins service
sudo systemctl start jenkins# Enable Jenkins to start on boot
sudo systemctl enable jenkins
3. Now, install the same Java version in the worker node.
#!/bin/bash
# Update package list
sudo apt update
# Install Java
sudo apt install -y default-jdk
# Display Java version to verify installation
java -version
# Additional steps specific to a Jenkins worker node can be added here if needed
# For example, you might need to configure the worker node to connect to the Jenkins master.
echo "Java is now installed on the Jenkins worker node."
4. In the security group of Master Node add port 8080, so that you can access the Jenkins running in that port.
5. Now open the browser and navigate to ip_address_of_ec2:8080.
6. To get the password, connect to your EC2 instance and run the below command:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
7. Select Install suggested plugins.
Connecting Master and Worker instances:
- To connect the Master and Worker servers, we will use the SSH protocol. In your master server generate the Public and Private key, by using the below command:
cd. ssh
ssh-keygen -t rsa -b 2048 -f key_name
2. Now copy your public key, day80.pub
,
and in your worker server navigate to .ssh
and paste your public key.
3. Now in your Master server run the command to connect to your Worker node:
ssh username@public_ip
This shows that the key pair generated is working great!.
4. To connect the Master and Worker Node, come to you Jenkins Dashboard and then click on Set up an agent.
5. Enter the desired node name.
6. Enter the Node name, description, number of executors and remote root directory.
Now label your agent, choose the desired usage, and in the Launch method choose, Launch via SSH
Click on Add Credentails.
Now choose the authentication method as SSH username with private key.
Add the ID, Description and username of the worker instance and paste your private key, then click on add private key.
In the Host Key Verification Strategy choose Non host verification strategy.
Click on save.
7. Your Agent is now connected.
Create a job
- Click on
Create a job.
2. Enter an item name and choose Pipeline
.
3. Add the project URL, Github URL.
In Build Triggers choose GitHub Hook trigger for GITScm polling
, which allows you to trigger the pipeline when any changes is made in the project.
4. Write the pipeline script, in Groovy syntax.
pipeline {
agent { label "Agent-1" }
stages{
stage("Clone Code"){
steps{
git url: "https://github.com/abhirajsingh2001/node-todo-cicd.git", branch: "master"
}
}
stage("Build and Test"){
steps{
sh "docker build . -t node-todo-app-cicd:v1"
}
}
stage("Push to Docker Hub"){
steps{
withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
sh "docker tag node-todo-app-cicd:v1 ${env.dockerHubUser}/node-todo-app-cicd:v1"
sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
sh "docker push ${env.dockerHubUser}/node-todo-app-cicd:v1"
}
}
}
stage("Deploy"){
steps{
sh "docker-compose down && docker-compose up -d"
echo "Code Deployed"
}
}
}
}
5. Click on save, to save the pipeline.
Adding Credentials for Docker:
To push our docker image to the docker hub we need the credentials, but we can’t pass them directly, for that Jenkins for us the credentials management system, which allows you to use them in the pipelines.
In your Jenkins app navigate to Dashboard > Manage Jenkins > Credentials > Global credentials.
Click on add Credentials.
Choose Kind as Username with password
. Choose the scope global.
Enter the Username of your Docker Hub, followed by its password. Then give the ID these credentials.
Make sure that your Master node has the Docker installed in it.
Also, make sure you add your system and Jenkins user in the Docker group:
sudo usermod -aG docker $USER
sudo usermod -aG docker jenkins
Configuring the webhook for your GitHub project:
1. Click on the webhooks.
Add a new webhook.
Enter the payload URL: http://ip_address:8080/github-hook
3. Select the event type to trigger the hook.
Click on Add webhook.
Conclusion:
In this project, we did the following things:
Created 2 EC2 instances Master Node and Worker Node. Install Java and Jenkins in the Master server and in the Worker node install Java only.
Also, install the Docker and Docker Compose in the worker node.
Add the Global credentials for Docker Hub login to push the docker images.
Setup the Webhooks so that the pipeline would get triggered as the changes made in the repository branch.