# 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:

1. **Open Source and Extensible**
    
2. **Automation of Repetitive Tasks**
    
3. **Wide Range of PluginsDistributed Builds**
    

### **Objective:**

1. **Setting Up Jenkins on EC2**
    
2. **Configuring Jenkins**
    
3. **Creating Your First Jenkins Pipeline**
    

### **Create an EC2 instance**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703666735367/79e340b6-f8a5-480d-9ef9-6e9e5bbc9cca.png align="center")

**Connect to the Jenkins server using SSH protocol**

```plaintext
ssh -i ~/Downloads/nod.pem ubuntu@52.91.204.14
```

**To install Jenkins on our machine, we need to have java**

```plaintext
sudo apt update
sudo apt install openjdk-11-jre
```

**Whether Java is installed or not let's verify**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703667046930/a952ab02-ada2-43eb-b054-dabfe8f8064e.png align="center")

**Now, you can proceed with installing Jenkins**

```plaintext
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**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703667437894/d6801910-d70e-4deb-b28f-5dfe8d6ee002.jpeg align="center")

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 <mark>8080</mark> in the ***inbound rule*** of <mark>security groups</mark>.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703667705648/641ce4aa-5e66-46d0-8f39-21a48d471a60.png align="center")

Now access the Jenkins in the browser using http://ec2-public-ip:8080

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703667842126/c91e7762-8d63-4542-a2a8-afb141294642.png align="center")

To log in the Jenkins we need to get the password from <mark>/var/lib/jenkins/secrets/initialAdminPassword.</mark>

```plaintext
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703668131704/6759fdcd-1312-46c8-a542-6b9bef7169d0.png align="center")

Once the plugins are installed, set up the Admin user details.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703668376516/fb8d097e-bfb0-43b7-9a94-f78150e3390a.png align="center")

Save and continue.

Set up the Instance configuration with your ec2 public IP address.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703668675632/ee7aee95-f3c4-466f-b195-c84ab0ffd74b.png align="center")

Save and finish.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703668708337/ac08cb5e-6f72-47b9-b9cb-6d1664c72d3b.png align="center")

Now Install the Docker Pipeline plugin in Jenkins:

* Go to Manage Jenkins &gt; 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.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703671916310/cdbeacdd-207c-40b8-96fd-93a37a70593f.png align="center")
    

Now install the docker agent in the instance

```plaintext
sudo apt install docker.io
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703672097873/4bb29530-c534-4bf8-a022-14f16fdaab0a.png align="center")

Let's Grant Jenkins user and Ubuntu user permission to docker daemon.

```plaintext
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** &gt; then add the *Name* &gt; choose the *pipeline* option.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703681272307/86b84637-61ec-4ba3-b7c4-b08344be6b2c.png align="center")

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
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703681568158/b63ee541-c562-4731-896c-3652cdb13c63.png align="center")

For this pipeline I have used the following Jenkinsfile

```plaintext
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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703681833047/d2071e60-6f42-4f26-852e-06091cb159b6.jpeg align="center")

Once the build is complete we can see the following output.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703681919051/8ac3388e-3454-4f6a-a184-9eb8d684196e.png align="center")

In the console output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703682045555/700d9b5d-d639-4507-ba60-0051e6eaf944.png align="center")

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!!
