Jenkins Zero to Hero: How to Install, Configure and Use Jenkins for CI/CD on AWS EC2 Instance
Table of contents
- ๐ Introduction:
- ๐ Install Java JDK:
- ๐น Step 1: Add Jenkins repository key to the system
- ๐น Step 2: Add the Jenkins repository to your system
- ๐น Step 3: Update your system and install Jenkins
- ๐น Step 4: Open port 8080 in the inbound traffic rules of your EC2 instance
- ๐น Step 5: Login to Jenkins using the below URL:
- ๐น Step 6: Retrieve the Jenkins Admin Password
- ๐น Step 7: Install suggested plugins
- ๐น Step 8: Create First Admin User (Optional)
- ๐ Install Docker Pipeline Plugin:
- ๐ Install Docker on EC2:
- ๐ Conclusion:
๐ Introduction:
Jenkins is an open-source automation server that enables developers to automate their software development process by building, testing, and deploying their code. In this tutorial, we will learn how to install, configure and use Jenkins for CI/CD on an AWS EC2 instance.
Prerequisites Before we proceed, make sure that you have an AWS account and an EC2 instance running with the Ubuntu operating system. Also, ensure that you have Java Development Kit (JDK) installed on your instance. You can use the following command to install JDK.
๐ Install Java JDK:
sudo apt update
sudo apt install openjdk-11-jre
Install Jenkins Once you have JDK installed on your EC2 instance, you can proceed with the installation of Jenkins. Follow the below steps to install Jenkins.
๐น Step 1: Add Jenkins repository key to the system
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
๐น Step 2: Add the Jenkins repository to your system
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
๐น Step 3: Update your system and install Jenkins
sudo apt-get update
sudo apt-get install jenkins
๐น Step 4: Open port 8080 in the inbound traffic rules of your EC2 instance
By default, Jenkins will not be accessible to the external world due to the inbound traffic restriction by AWS. Open port 8080 in the inbound traffic rules as shown below.
Go to your AWS EC2 console
Click on Instances and select your running instance
In the bottom tabs, click on Security groups
Add inbound traffic rules as shown in the image (you can just allow TCP 8080 as well, in my case, I allowed All traffic).
๐น Step 5: Login to Jenkins using the below URL:
http://<ec2-instance-public-ip>:8080
Note: If you are not interested in allowing All Traffic to your EC2 instance, you can delete the inbound traffic rule for your instance or edit the inbound traffic rule to only allow custom TCP port 8080.
๐น Step 6: Retrieve the Jenkins Admin Password
Run the command to copy the Jenkins Admin Password
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
๐น Step 7: Install suggested plugins
Click on Install suggested plugins and wait for Jenkins to Install suggested plugins.
๐น Step 8: Create First Admin User (Optional)
You can create a first admin user or skip this step if you want to use this Jenkins instance for future use cases as well.
๐ Install Docker Pipeline Plugin:
Install the Docker Pipeline plugin in Jenkins Now that we have installed and configured Jenkins, we can proceed with the installation of the Docker Pipeline plugin in Jenkins. Follow the below steps to install the Docker Pipeline plugin.
Step 1: Log in to Jenkins.
Step 2: Go to Manage Jenkins > Manage Plugins.
Step 3: In the Available tab, search for "Docker Pipeline".
Step 4: Select the plugin and click the Install button.
Step 5: Restart Jenkins after the plugin is installed.
Docker Slave Configuration After the installation of the Docker Pipeline plugin, we need to configure Docker as the agent. Follow the below steps to configure Docker as the agent.
๐ Install Docker on EC2:
๐น Step 1: Install Docker on the EC2 instance
Docker is a containerization platform that allows you to package an application with all of its dependencies into a container. This container can then be run on any system that supports Docker, making it an excellent tool for building and deploying applications. To install Docker on the EC2 instance, follow the steps below.
- SSH into the EC2 instance:
To install Docker, you need to SSH into the EC2 instance using an SSH client like PuTTY or the built-in terminal on a Unix-based system. If you are using PuTTY, enter the EC2 instance's public IP address or DNS name in the "Host Name" field, and then click "Open."
- Update the package index:
Before installing any new packages, it is a good practice to update the package index on the EC2 instance.
sudo apt-get update
- Install Docker:
Once the package index is updated, you can install Docker by running the following command:
sudo apt-get install docker.io
This command will download and install the Docker package along with its dependencies.
- Grant Jenkins and Ubuntu user permissions to Docker daemon:
By default, the Docker daemon can only be run by the root user. However, we want to allow the Jenkins and Ubuntu users to run Docker commands without having to use sudo every time. To do this, we need to add the Jenkins and Ubuntu users to the Docker group.
sudo usermod -aG docker jenkins
sudo usermod -aG docker ubuntu
- Restart the Docker daemon:
Finally, restart the Docker daemon to apply the new group membership.
sudo systemctl restart docker
Now that Docker is installed and the Jenkins user has been granted permissions to the Docker daemon, we can proceed to the next step of configuring Jenkins as a Docker agent.
๐น Step 2: Configure Docker as a Jenkins agent
In this step, we will configure Docker as an agent in Jenkins. This means that Jenkins will use Docker containers to run build and test jobs, rather than running them directly on the EC2 instance.
- Install the Docker Pipeline plugin in Jenkins:
Log in to Jenkins and navigate to "Manage Jenkins" > "Manage Plugins." In the "Available" tab, search for "Docker Pipeline" and select the plugin. Click the "Install without restart" button.
- Restart Jenkins:
Once the plugin is installed, Jenkins will prompt you to restart the server to apply the changes. Click the "Restart Jenkins when installation is complete and no jobs are running" checkbox and then click "Continue."
- Configure the Docker agent in Jenkins:
Navigate to "Manage Jenkins" > "Configure System" and scroll down to the "Cloud" section. Click the "Add a new cloud" dropdown and select "Docker." In the "Docker" section, enter a name for the Docker agent, select "Connect with Docker API" as the "Docker Host URI," and enter "tcp://localhost:2375" as the value.
- Configure the Docker image to use:
Scroll down to the "Docker Agent templates" section and click "Add Docker Template." Enter a name for the template, such as "Ubuntu Docker Image," and select the "Docker image" option. Enter the name of the Docker image to use, such as "ubuntu:latest," in the "Docker image" field.
- Configure the Docker Container Startup Options
To configure the Docker container startup options, we need to create a Jenkins agent container that will run on the EC2 instance.
We will use the Docker Pipeline plugin to create the agent container. This plugin allows us to create a Docker container as a build agent dynamically.
To create the agent container, follow these steps:
In Jenkins, click on "New Item" to create a new job.
Enter a name for the job and select "Pipeline" as the job type.
Click "OK" to create the job.
In the Pipeline script section, enter the following script:
pipeline {
agent {
docker { image 'node:16-alpine' }
}
stages {
stage('Test') {
steps {
sh 'node --version'
}
}
}
}
This script defines a pipeline with one stage that will run on an Ubuntu Docker container. The container will mount the Docker socket to allow the pipeline to run Docker commands on the host.
- Click "Save" to save the job.
Now that the job is created, we can run it to test the Docker container startup options.
Click on "Build Now" to run the job.
Wait for the job to complete.
The job will create a Docker container with the specified options and run a "Node --version" command on it. If everything is configured correctly, you should see the output "Node Version" in the job console output.
Congratulations! You have successfully configured the Docker container startup options in Jenkins.
๐ Conclusion:
Jenkins is a popular automation tool that can be used to automate building, testing, and deploying software applications. In this tutorial, we will walk you through the process of setting up Jenkins on an AWS EC2 instance, configuring Docker as an agent, and setting up a CI/CD pipeline for deploying applications to Kubernetes. We will cover everything from installing Java and Jenkins to configuring Docker, setting up a Jenkins slave node, and installing the Docker Pipeline plugin.
๐น Step-by-Step Guide:
Launch an EC2 instance on the AWS console and install Java and Jenkins
Open port 8080 in the inbound traffic rules to access Jenkins
Install the Docker Pipeline plugin in Jenkins
Install Docker on the EC2 instance
Configure the Docker container startup options
Grant Jenkins user and Ubuntu user permission to the Docker daemon
Restart Jenkins to apply changes
Configure Docker as a Jenkins slave node