Jenkins Tutorial
Jenkins is an open-source automation server used to build, test, and deploy software. It supports Continuous Integration (CI) and Continuous Deployment (CD), making it a popular tool in DevOps.
1. Installing Jenkins
Step 1: Install Java
Jenkins requires Java (Java Development Kit, JDK) to run.
On Ubuntu
sudo apt update
sudo apt install openjdk-11-jdk
Step 2: Install Jenkins
Follow the instructions from the Jenkins website for your operating system.
On Ubuntu
wget -q -O - https://pkg.jenkins.io/debian-stable/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'
sudo apt update
sudo apt install jenkins
Start Jenkins with:
sudo systemctl start jenkins
Access Jenkins
Visit http://localhost:8080 in your browser to access Jenkins.
2. Setting Up Jenkins
Unlocking Jenkins
-
When you first access Jenkins, you’ll be asked for an admin password.
-
Locate the password in
/var/lib/jenkins/secrets/initialAdminPassword:sudo cat /var/lib/jenkins/secrets/initialAdminPassword -
Enter this password to unlock Jenkins.
Installing Suggested Plugins
After unlocking, Jenkins will prompt you to install suggested plugins. This will include commonly used plugins.
3. Creating Your First Job
- Click on New Item on the Jenkins dashboard.
- Enter a name for your project and select Freestyle project.
- Configure the job with various build steps, triggers, and post-build actions.
Example Build Step
In Build > Execute Shell, you can add a shell command:
echo "Hello, Jenkins!"
Save the job and click Build Now to run it.
4. Configuring Source Control with Git
Jenkins integrates with source control systems like Git.
- In your job configuration, scroll to Source Code Management.
- Select Git and enter the repository URL.
- You may need to provide credentials for private repositories.
5. Adding Build Triggers
Build triggers allow Jenkins to automatically start jobs based on events.
Common Triggers
- Poll SCM: Checks for code changes at specified intervals.
- Build Periodically: Runs jobs at scheduled times.
- GitHub Hook Trigger: Starts a build when there are commits to a GitHub repository.
6. Pipeline as Code
Jenkins Pipelines allow you to define CI/CD workflows using code, usually stored in a Jenkinsfile.
Creating a Pipeline
- Click New Item > Pipeline.
- Define the pipeline in the Pipeline section using Scripted or Declarative syntax.
Example Declarative Pipeline
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
7. Jenkinsfile Example for Git Integration
Place a Jenkinsfile in the root of your Git repository to use it with Jenkins.
pipeline {
agent any
stages {
stage('Clone Repository') {
steps {
git 'https://github.com/your-repo/example.git'
}
}
stage('Build') {
steps {
echo 'Building the project...'
}
}
}
}
Configure the job to pull the Jenkinsfile from the repository.
8. Installing Plugins
Jenkins has an extensive plugin ecosystem.
- Go to Manage Jenkins > Manage Plugins.
- Search for plugins like Git, GitHub Integration, Slack Notifications, etc.
- Click Install and restart Jenkins if prompted.
9. Setting Up Notifications
Email Notifications
- Go to Manage Jenkins > Configure System.
- Set up an SMTP server and email configuration.
Slack Notifications
- Install the Slack Notification plugin.
- Go to your job configuration, scroll to Post-build Actions and add Slack Notifications.
10. Backing Up Jenkins
Jenkins stores its configuration and job data in /var/lib/jenkins (or similar).
- Backing up: Regularly back up the
JENKINS_HOMEdirectory. - Restoring: Replace
JENKINS_HOMEwith your backup and restart Jenkins.
Summary
This tutorial covered Jenkins basics:
- Installing Jenkins and setting up the server.
- Creating jobs and configuring source control with Git.
- Setting up pipelines and using the
Jenkinsfile. - Installing plugins and configuring notifications.
Jenkins is a powerful tool for automating CI/CD workflows, supporting many integrations to streamline software development.
Content Review
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewer
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.