From EC2 Setup to Jenkins Pipeline: A Step-by-Step Guide for Seamless CI/CD Integration
In the ever-evolving landscape of software development, Continuous Integration (CI) and Continuous Deployment (CD) have become integral practices for ensuring the efficiency, reliability, and rapid delivery of software projects. Jenkins, an open-source automation server, plays a pivotal role in making CI/CD pipelines a reality. In this tutorial, we will explore the process of setting up an EC2 instance, installing Jenkins, and creating a basic Jenkins pipeline.
Understanding Jenkins:
Jenkins is a leading automation server that facilitates the automation of building, testing, and deploying code changes. Its popularity stems from its robust feature set and flexibility. Here are some key reasons why Jenkins is widely embraced in the software development community:
Open Source and Extensible
Automation of Repetitive Tasks
Wide Range of PluginsDistributed Builds
Objective:
Setting Up Jenkins on EC2
Configuring Jenkins
Creating Your First Jenkins Pipeline
Create an EC2 instance
Connect to the Jenkins server using SSH protocol
ssh -i ~/Downloads/nod.pem ubuntu@52.91.204.14
To install Jenkins on our machine, we need to have java
sudo apt update
sudo apt install openjdk-11-jre
Whether Java is installed or not let's verify
Now, you can proceed with installing Jenkins
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
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
sudo apt-get update
sudo apt-get install jenkins
Verify Jenkins
Our Jenkins is running actively at port 8080 but we have not configured any rule regarding port 8080 for our ec2 instance. Therefore, we will add port 8080 in the inbound rule of security groups.
Now access the Jenkins in the browser using ec2-public-ip:8080
To log in the Jenkins we need to get the password from /var/lib/jenkins/secrets/initialAdminPassword.
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
After hitting the above command, copy the password and enter it to log into Jenkins. After that click on Install the suggested plugins in Jenkins.
Once the plugins are installed, set up the Admin user details.
Save and continue.
Set up the Instance configuration with your ec2 public IP address.
Save and finish.
Now Install the Docker Pipeline plugin in Jenkins:
Go to Manage Jenkins > Manage Plugins.
In the Available tab, search for "Docker Pipeline".
Select the plugin and click the Install button.
Restart Jenkins after the plugin is installed.
Now install the docker agent in the instance
sudo apt install docker.io
Let's Grant Jenkins user and Ubuntu user permission to docker daemon.
sudo su -
usermod -aG docker jenkins
usermod -aG docker ubuntu
systemctl restart docker
Now let's create a first Jenkins pipeline.
Click on Create Job > then add the Name > choose the pipeline option.
In the next step scroll down and move to Pipeline
Pipeline: Pipeline script from SCM
SCM: Git
Repository: Enter the repo link
Branch: check main/master in GitHub and add according to it
script path: add the path to the Jenkins file in GitHub
For this pipeline I have used the following Jenkinsfile
pipeline {
// Define the agent where the pipeline runs, using a Docker container with Python 3.9
agent {
docker {
image 'python:3.9-alpine'
}
}
// Define the stages of the pipeline
stages {
// Stage for testing
stage('Test') {
// Steps to be executed within the 'Test' stage
steps {
// Print the Python version
sh 'python --version'
}
}
}
}
Now after saving the changes in the next screen click on Build now
Once the build is complete we can see the following output.
In the console output:
Congratulations on doing this perfectly.
As we execute our first Jenkins pipeline successfully, we recognize the transformative impact Jenkins has on the CI/CD landscape. Its versatility, extensibility, and user-friendly interface make it a go-to choice for teams aiming to streamline their development processes. In the upcoming blog, we will have new updates on the Jenkins blog.
Happy Learning!!