Project: Set Up a CI/CD Jenkins Pipeline to Deploy a Dockerized Node.js Application on an EC2 Instance Using a Declarative Groovy Pipeline
Steps to Set Up an EC2 Instance for Jenkins
Navigate to EC2
Log in to your AWS Management Console.
Go to the EC2 service.
Launch an Instance
- Click on the Launch Instance button.
Configure the Instance
Name: Set the instance name to
jenkins-server
.Application and OS Images (Amazon Machine Image): Select Ubuntu Server 24.04 LTS.
Instance Type: Choose
t2.micro
(eligible for the AWS Free Tier).
Configure the Security Group
select a new security group or modify an existing one with the following rules:
- SSH (Port 22): Allow from anywhere (
0.0.0.0/0
) or restrict it to your IP address to give you better security. and allow HTTP and HTTPS accordingly
- SSH (Port 22): Allow from anywhere (
Launch the Instance
Review your configuration.
Click Launch Instance to initialize the server.
Access the EC2 Instance
- connect the instance to your terminal through SSH
Your EC2 instance is set up and ready to install and configure Jenkins.
update the packages
sudo apt-get update
Before the installation of Jenkins, you have to install java
sudo apt install fontconfig openjdk-17-jre
java -version
Select the long-term support of Jenkins to install
add Jenkins repository key
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \ https://pkg.jenkins.io/debian binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null
Update the package again and install the Jenkins
sudo apt-get update
sudo apt-get install jenkins
enable the Jenkins and start it.
sudo systemctl enable jenkins #enable the jenkins sudo systemctl start jenkins #start the jenkins sudo systemctl status jenkins #check the status of jenkins
Install docker and docker-compose
sudo apt update sudo apt install docker.io docker-compose-v2 -y sudo usermod -aG docker $USER && newgrp docker sudo usermod -aG docker jenkins #//add jenkins user to dockergroup sudo newgrp docker sudo systemctl restart jenkins
Go to the security group and add port 8080 Jenkins by default runs on port 8080 after that copy the public IP and paste it to the browser to access Jenkins public IP of ec2:8080
The
initialAdminPassword
is required to unlock Jenkins during the initial setup. To retrieve it, you can follow these steps in your terminal:. Open your terminal.
. Use the
cat
commandsudo
to access theinitialAdminPassword
file:sudo cat /var/lib/jenkins/secrets/initialAdminPassword
. Copy the displayed password.
. Paste it into the Jenkins setup screen under "Unlock Jenkins."
Now, click on Install suggested plugins
After that Jenkins asks to create a first user admin
After creating the admin user, you’re prompted to configure the Jenkins URL (automatically pre-filled with the server's address).
Once setup is complete, you'll land on the Jenkins dashboard, showing options to create jobs, manage plugins, and configure system settings.
Click on Manage Jenkins in the left-hand menu. Search for the Plugin In the search bar, type Pipeline Stage View. Install the Plugin:
Check the box next to the Pipeline Stage View plugin. Click on Install after that Restart Jenkins when installation is complete
click on the new item put the name and select pipeline then ok
put the descriptions and then choose In the Source Code Management section:
Select Git and paste the URL of your GitHub repository
Go to Advanced Project Options Scroll down to the Pipeline section and define your pipeline script:
click Build now
pipeline{ agent any; stages{ stage("Code Clone"){ steps{ git url:"https://github.com/fauzeya67/node-todo-cicd.git", branch:"master" echo "code clone stage done" } } stage("Image Build and test"){ steps{ sh "whoami" sh "docker build -t node-app ." echo "code build stage done" } } stage("scan the image"){ steps{ echo 'I will scan and add code further' } } stage("Deploy"){ steps{ sh "docker compose down && docker compose up -d --build" echo "code deploy stage done" } } } }
Click on Stage View and then see logs in the console output and access the deployed application
To achieve automatic deployment of changes from a GitHub repository to a web application whenever a developer commits code, you can configure Jenkins with GitHub Webhooks and a deployment pipeline
Configure Webhooks in GitHub
Go to your repository on GitHub.
Navigate to Settings > Webhooks.
Click Add webhook.
In the Payload URL field, enter the URL of your Jenkins server
Now make changes in the code, it will automatically trigger the pipeline and deploy the updates to the web application without the need to manually click on "Build Now.
I hope this was helpful! Stay tuned for more content like this. 🚀