In this tutorial, our main goal is to configure Jenkins for Continuous Integration/Continuous Delivery (CI/CD). Jenkins is an open-source automation server that allows you to automate different stages of your delivery pipeline. You will learn how to set up Jenkins, create a simple pipeline, and integrate it with a version control system.
You will learn how to:
- Install Jenkins
- Configure Jenkins
- Create a Jenkins pipeline
- Integrate Jenkins with GitHub
Prerequisites:
- Basic knowledge of version control systems, preferably Git
- Java installed on your system
- A GitHub account
First, you need to install Jenkins. Here are the steps to install Jenkins on Ubuntu:
sudo apt update
sudo apt install openjdk-11-jdk
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt update && sudo apt install jenkins
After installation, Jenkins service will start automatically. You can check its status using systemctl status jenkins
Open your web browser and navigate to http://localhost:8080
. You'll see a Unlock Jenkins page. To find the password, use the command sudo cat /var/lib/jenkins/secrets/initialAdminPassword
. Copy the password, paste it into the form, and click Continue.
Next, you'll be prompted to customize Jenkins. For this tutorial, select Install suggested plugins. Jenkins will then install the suggested plugins.
Once Jenkins is configured, you need to create a new job. On the Jenkins dashboard, click on New Item. In the next screen, enter a name for the job, select Pipeline, and click OK.
In the pipeline configuration page, scroll down to the Pipeline section. Here, you can define your pipeline script. For simplicity, we'll create a pipeline that prints a simple message.
pipeline {
agent any
stages {
stage('Stage 1') {
steps {
echo 'Hello, Jenkins!'
}
}
}
}
Click Save to finish creating the pipeline.
To integrate Jenkins with GitHub, you need to install the GitHub plugin. Go to Manage Jenkins > Manage Plugins > Available, then find and select GitHub plugin. Click on Install without restart.
After installing the plugin, go to your job configuration page and in the Pipeline section, select Pipeline script from SCM. Then, select Git and enter your repository URL.
Here's an example of a Jenkins pipeline that clones a Git repository and builds a Java project with Maven. The example assumes you have a Maven project in your repository.
pipeline {
agent any
stages {
stage('Clone repository') {
steps {
git 'https://github.com/your-repository.git'
}
}
stage('Build') {
steps {
sh 'mvn clean install'
}
}
}
}
In this tutorial, you've learned how to install and configure Jenkins, create a simple pipeline, and integrate Jenkins with GitHub. To expand your knowledge, you can explore how to use Jenkins with other version control systems, and how to configure complex pipelines.
Exercise 1: Install Jenkins on your local machine and create a simple pipeline that prints a message.
Exercise 2: Create a Jenkins pipeline that clones a Git repository and lists the files in the repository.
Exercise 3: Create a Jenkins pipeline that clones a Git repository, builds a Java project with Maven, and archives the build results.
Remember, practice is the key to mastering any skill. Keep experimenting with different pipeline configurations and Jenkins features.