In this guide, we'll walk you through the process of setting up a Jenkins Server for Continuous Integration (CI) on Ubuntu.
To begin, follow these steps to install Jenkins on your Ubuntu system:
-
Update your package list:
sudo apt update
-
Install OpenJDK 11:
sudo apt install openjdk-11-jre -y
-
Add the Jenkins repository key:
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
-
Add the Jenkins repository to your sources:
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 list again:
sudo apt-get update
-
Install Jenkins:
sudo apt-get install jenkins -y
To access Jenkins for the first time, you'll need the default admin password. Run this command to retrieve it:
cat /var/lib/jenkins/secrets/initialAdminPassword
In a production environment, you would create a user, but for this guide, we'll skip that step. Click "skip and continue as admin" to access Jenkins using the admin ID and the password you obtained in Step 1.
Next, install the recommended plugins.
Navigate to the Plugin section in Jenkins at http://localhost:8080/manage/pluginManager/available
and install the Docker Pipeline Plugin.
This plugin is essential for pulling and pushing Docker images to DockerHub. Note that the Git plugin is already installed in Step 2.
Execute the following commands one by one to install Docker. Make sure to replace 'ubuntu' with the appropriate username according to your instance:
sudo apt update
sudo apt install docker.io -y # installs Docker
sudo su -
usermod -aG docker jenkins # Add the 'jenkins' user to the 'docker' group.
usermod -aG docker ubuntu # Add the 'ubuntu' user to the 'docker' group.
systemctl restart docker
By adding the "jenkins" and "ubuntu" users to the "docker" group, we grant them the necessary permissions to work with Docker containers without requiring root privileges.
Once everything is set up, restart the Jenkins server by appending /restart
to the Jenkins URL, like this: http://localhost:8080/restart
.
We will set up credentials for DockerHub and GitHub to enable Jenkins to interact with these services securely.
-
Go to
https://hub.docker.com/settings/security
and create a new access token. -
In Jenkins, navigate to
http://localhost:8080/manage/credentials/store/system/domain/_/
. -
Add a new credential with the following details:
- Username: Use your DockerHub account username.
- Password: Enter the access token you generated in step 1.
- ID: Set this as
docker-cred
. Make sure to keep a note of this ID as it will be referenced in our Jenkins pipeline.
-
Create a personal access token on GitHub by visiting
https://github.com/settings/tokens
. -
In Jenkins, go to
http://localhost:8080/manage/credentials/store/system/domain/_/
. -
Create a new GitHub credential as a secret text with the following details:
- Secret: Paste the GitHub access token you created in step 1.
- ID: Set this as
github
. Make sure to keep a note of this ID as it will be referenced in our Jenkins pipeline.
We will now configure the Jenkinsfile to define our CI process. You can read this file to get a comprehensive understanding of the entire CI process that we are going to configure using Jenkins.
Complete Jenkinsfile at - Jenkinsfile
// Configure the agent
agent {
docker {
// Use this image to set up the Jenkins agent
image 'dipanshu13/python-docker:latest'
// Mount the agent's Docker socket to access the host's Docker daemon
args '--user root -v /var/run/docker.sock:/var/run/docker.sock'
}
}
The specified Docker image includes both Docker and Python, which are essential for building and creating images of our application.
stage('Checkout') {
steps {
sh 'echo GitHub and repository and credential are already configured during pipeline configuration'
// Uncomment the line below if we're using a direct script in the pipeline rather than a Dockerfile from a repository
git branch: 'main', url: 'https://github.com/i-dipanshu/python-django-todo-app.git'
}
}
We don't need to create any authorization to pull the repository because our repository is public. However, we need to create a way to authorize Jenkins to push to our GitHub repository.
stage("Build Docker Image and Push to DockerHub") {
environment {
DOCKER_IMAGE = "dipanshu13/django-todo:v${BUILD_NUMBER}"
REGISTRY_CREDENTIALS = credentials('docker-cred')
}
steps {
script {
sh "docker build -t ${DOCKER_IMAGE} ."
// sh 'echo $REGISTRY_CREDENTIALS_PSW | docker login -u $REGISTRY_CREDENTIALS_USR --password-stdin'
// sh 'docker push ${DOCKER_IMAGE}'
}
}
}
Note: Since this is a Python application, we don't need to build the source code. However, for compiled languages like Java, we would first compile the source code before building the Docker image.
stage("Update the Deployment manifest") {
environment {
MANIFEST_GITHUB_REPO = "python-django-todo-app.git"
GITHUB_USER_NAME = "i-dipanshu"
EMAIL = "[email protected]"
NAME = "Dipanshu"
}
steps {
withCredentials([string(credentialsId:"github", variable: "GITHUB_TOKEN")]) {
sh '''
git config user.email '${EMAIL}'
git config user.name '${NAME}'
sed -i "s/todo:v[0-9]\\{1,\\}/todo:v${BUILD_NUMBER}/" ./k8s-argocd-manifests/deployment.yml
git add .
git commit -m "Updated the deployment image to version${BUILD_NUMBER}"
git push https://${GITHUB_TOKEN}@github.com/${GITHUB_USER_NAME}/${MANIFEST_GITHUB_REPO} HEAD:main
'''
}
}
}
// Setup github webhook to trigger pipeline
We can create a new job using this pipeline script in Jenkins server and github will trigger it whenever a change is made.