Introduction
In the world of DevOps and continuous integration/continuous delivery (CI/CD), Jenkins is one of the most popular tools. It is an open-source automation server that helps automate various stages of software development, from building and testing to deployment. Jenkins supports multiple plugins, which extend its functionality, making it a powerful and flexible tool for any software development team. This tutorial will provide a comprehensive guide on how to use Jenkins, starting from installation and setup to creating and running pipelines with relevant code examples.
Table of Contents
- Introduction
- Installing Jenkins
- Setting Up Jenkins
- Jenkins Overview
- Configuring Jenkins
- Jenkins Plugins
- Creating Your First Jenkins Job
- Jenkins Pipelines
- Integrating Jenkins with Version Control Systems
- Automating Tests with Jenkins
- Deploying Applications with Jenkins
- Jenkins Best Practices
- Conclusion
1. Installing Jenkins
Prerequisites
Before installing Jenkins, ensure you have the following prerequisites:
- A machine with a supported operating system (Windows, Linux, macOS)
- Java Development Kit (JDK) installed (Jenkins requires Java 8 or Java 11)
- Internet connection to download Jenkins and necessary plugins
Installing Jenkins on Windows
Download Jenkins: Go to the official Jenkins website and download the Windows installer.
Run the Installer: Double-click the downloaded
.msi
file to start the installation process. Follow the on-screen instructions to complete the installation.Start Jenkins: After installation, Jenkins should start automatically. You can also start it manually by running the Jenkins service from the Services panel.
Access Jenkins: Open your web browser and navigate to
http://localhost:8080
. You should see the Jenkins setup wizard.
Installing Jenkins on Linux
- Add Jenkins Repository: Open a terminal and run the following commands to add the Jenkins repository:
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
- Install Jenkins: Update your package index and install Jenkins:
sudo apt-get update
sudo apt-get install jenkins
- Start Jenkins: Start the Jenkins service:
sudo systemctl start jenkins
- Enable Jenkins: Enable Jenkins to start at boot:
sudo systemctl enable jenkins
-
Access Jenkins: Open your web browser and navigate to
http://your_server_ip_or_domain:8080
.
Installing Jenkins on macOS
- Install Homebrew: If you don't have Homebrew installed, install it by running the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install Jenkins: Use Homebrew to install Jenkins:
brew install jenkins-lts
- Start Jenkins: Start Jenkins by running:
brew services start jenkins-lts
-
Access Jenkins: Open your web browser and navigate to
http://localhost:8080
.
2. Setting Up Jenkins
Initial Setup
When you first access Jenkins, you will be prompted to unlock Jenkins using an initial admin password. Follow these steps:
Locate the Initial Admin Password: The password is stored in a file on your system. The location of this file is displayed on the setup screen. On Linux, it is usually found at
/var/lib/jenkins/secrets/initialAdminPassword
.Enter the Password: Copy the password from the file and paste it into the setup wizard to unlock Jenkins.
Install Suggested Plugins: Jenkins will prompt you to install plugins. Choose the "Install suggested plugins" option. This will install a set of commonly used plugins.
Create First Admin User: After installing the plugins, you will be prompted to create the first admin user. Fill in the details and click "Save and Finish".
Jenkins is Ready: Click "Start using Jenkins" to complete the setup.
3. Jenkins Overview
Jenkins Dashboard
The Jenkins dashboard is the main interface where you can manage jobs, configure settings, and monitor builds. It consists of the following sections:
- New Item: Create a new job or pipeline.
- People: View and manage users.
- Build History: View the history of builds.
- Manage Jenkins: Access configuration settings and manage plugins.
- My Views: Create custom views to organize jobs.
Jenkins Nodes
Jenkins can distribute build jobs across multiple machines, called nodes. This helps in managing and balancing the load, especially in large projects. Nodes can be added and configured from the "Manage Jenkins" -> "Manage Nodes and Clouds" section.
Jenkins Jobs
Jenkins jobs are the fundamental units of work in Jenkins. Each job can be configured to perform specific tasks such as building code, running tests, and deploying applications. Jobs can be created from the dashboard by clicking "New Item".
4. Configuring Jenkins
Global Configuration
Global configuration settings can be accessed from "Manage Jenkins" -> "Configure System". Here you can configure various settings such as:
- Jenkins URL: Set the URL for accessing Jenkins.
- JDK: Configure Java Development Kit installations.
- Git: Set up Git installations.
- Email Notifications: Configure email notifications for build statuses.
Security Configuration
Securing Jenkins is crucial to protect your build environment. Jenkins provides several security options under "Manage Jenkins" -> "Configure Global Security":
- Enable Security: Enable Jenkins security.
- Realm: Choose a security realm (e.g., Jenkins' own user database, LDAP).
- Authorization: Configure authorization strategies (e.g., Matrix-based security, Role-based strategy).
Tool Configuration
Jenkins can be integrated with various tools and services. Tool configurations can be done under "Manage Jenkins" -> "Global Tool Configuration". Common tools include:
- JDK: Configure JDK installations.
- Maven: Configure Maven installations.
- Git: Configure Git installations.
- Gradle: Configure Gradle installations.
5. Jenkins Plugins
Managing Plugins
Jenkins has a vast ecosystem of plugins that extend its functionality. Plugins can be managed from "Manage Jenkins" -> "Manage Plugins". Here you can:
- Install Plugins: Browse and install new plugins from the available plugins list.
- Update Plugins: Check for updates and update installed plugins.
- Uninstall Plugins: Uninstall unnecessary plugins.
Essential Plugins
Here are some essential plugins you should consider installing:
- Git Plugin: Integrates Jenkins with Git repositories.
- Maven Integration Plugin: Integrates Jenkins with Maven projects.
- Pipeline Plugin: Enables the creation of Jenkins pipelines.
- Blue Ocean Plugin: Provides a modern user interface for Jenkins.
- Slack Notification Plugin: Sends build notifications to Slack.
6. Creating Your First Jenkins Job
Creating a Freestyle Job
Create a New Job: From the Jenkins dashboard, click "New Item". Enter a name for your job and select "Freestyle project". Click "OK".
Configure Source Code Management: Under the "Source Code Management" section, select "Git". Enter the repository URL and credentials if required.
Repository URL: https://github.com/your-repo/your-project.git
Build Triggers: Configure how the job should be triggered. For example, you can trigger the job to run periodically, or when changes are pushed to the repository.
Build Environment: Configure the build environment settings if needed.
Build Steps: Add build steps to define the tasks the job should perform. For example, to build a Maven project, add a "Invoke top-level Maven targets" build step.
Goals: clean install
Post-build Actions: Configure post-build actions such as sending notifications or archiving artifacts.
Save and Build: Click "Save" to save the job configuration. To run the job, click "Build Now".
7. Jenkins Pipelines
Jenkins pipelines are used to define the steps involved in building, testing, and deploying applications. Pipelines can be defined using a domain-specific language (DSL) based on Groovy.
Creating a Simple Pipeline
Create a New Pipeline Job: From the Jenkins dashboard, click "New Item". Enter a name for your job and select "Pipeline". Click "OK".
Pipeline Definition: In the "Pipeline" section, define your pipeline using the Pipeline DSL.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
- Save and Build: Click "Save" to save the pipeline configuration. To run the pipeline, click "
Build Now".
Declarative vs. Scripted Pipelines
Jenkins supports two types of pipelines: Declarative and Scripted.
- Declarative Pipeline: Uses a more structured and simpler syntax. Recommended for most use cases.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
}
- Scripted Pipeline: Uses a more flexible and powerful syntax based on Groovy.
node {
stage('Build') {
echo 'Building...'
}
}
8. Integrating Jenkins with Version Control Systems
Integrating with Git
Install Git Plugin: Ensure the Git plugin is installed.
Configure Source Code Management: When creating or configuring a job, select "Git" under the "Source Code Management" section. Enter the repository URL and credentials if required.
Repository URL: https://github.com/your-repo/your-project.git
- Specify Branches: Specify the branches to build. For example, to build the master branch:
Branches to build: */master
Integrating with GitHub
Install GitHub Plugin: Ensure the GitHub plugin is installed.
Configure GitHub Repository: When creating or configuring a job, select "GitHub project" and enter the repository URL.
Project URL: https://github.com/your-repo/your-project
- Configure GitHub Hook: Set up a GitHub webhook to trigger Jenkins jobs when changes are pushed to the repository. In your GitHub repository settings, add a webhook with the following URL:
Payload URL: http://your-jenkins-server/github-webhook/
9. Automating Tests with Jenkins
Running Unit Tests
- Add Build Step: In your job configuration, add a build step to run unit tests. For example, for a Maven project, add a "Invoke top-level Maven targets" build step.
Goals: test
- Publish Test Results: Add a post-build action to publish test results. For example, for JUnit test results, add a "Publish JUnit test result report" post-build action.
Test report XMLs: **/target/surefire-reports/*.xml
Running Integration Tests
- Add Build Step: In your job configuration, add a build step to run integration tests. For example, for a Maven project, add a "Invoke top-level Maven targets" build step.
Goals: verify
- Publish Test Results: Add a post-build action to publish test results. For example, for JUnit test results, add a "Publish JUnit test result report" post-build action.
Test report XMLs: **/target/failsafe-reports/*.xml
10. Deploying Applications with Jenkins
Deploying to a Local Server
- Add Build Step: In your job configuration, add a build step to deploy the application to a local server. For example, for a Tomcat server, you can use the "Deploy war/ear to a container" build step.
WAR/EAR files: **/target/*.war
Containers: Tomcat 8.x
- Configure Container: Configure the container details such as URL, credentials, and context path.
Tomcat URL: http://localhost:8080
Deploying to a Remote Server
- Add Build Step: In your job configuration, add a build step to deploy the application to a remote server. You can use SSH or SCP for this purpose. For example, use the "Send build artifacts over SSH" build step.
Source files: **/target/*.war
Remove prefix: target
Remote directory: /path/to/deploy
- Configure SSH Server: Configure the SSH server details such as hostname, port, and credentials.
Hostname: remote.server.com
Port: 22
11. Jenkins Best Practices
Use Declarative Pipelines: Prefer declarative pipelines for their simplicity and structure.
Use Version Control for Pipeline Scripts: Store pipeline scripts in version control to track changes and collaborate with team members.
Use Parameterized Builds: Use parameterized builds to pass variables to your jobs and pipelines.
Monitor Jenkins Performance: Regularly monitor Jenkins performance and optimize build times.
Backup Jenkins Configuration: Regularly backup Jenkins configuration and job data to prevent data loss.
Use Security Best Practices: Follow security best practices such as enabling security, using strong credentials, and limiting user permissions.
12. Conclusion
Jenkins is a powerful and flexible automation server that can significantly improve your software development workflow. By following this comprehensive tutorial, you should now have a solid understanding of how to install, configure, and use Jenkins to automate various stages of your software development lifecycle. Whether you are building simple projects or complex applications, Jenkins provides the tools and features to help you achieve continuous integration and continuous delivery with ease. Happy building!
Top comments (0)