Jenkins Tutorial For Beginners | Jenkins Full Course | Jenkins Tutorial
Table of Contents
Introduction
This tutorial is designed for beginners who want to get started with Jenkins, a popular automation server in the DevOps ecosystem. It covers the fundamentals of Jenkins, including installation, job creation, integration with GitHub, and advanced features like the CI/CD pipeline and distributed builds. By the end of this guide, you will have a solid understanding of Jenkins and how to utilize it for continuous integration and delivery.
Step 1: Understand DevOps and CI/CD
- Familiarize yourself with key concepts:
- DevOps: A set of practices that combines software development (Dev) and IT operations (Ops) to shorten the systems development life cycle.
- Agile: An iterative approach to software development that promotes flexibility and customer satisfaction.
- CI/CD: Continuous Integration and Continuous Delivery/Deployment practices that automate the testing and deployment of applications.
Step 2: Install Jenkins on Ubuntu
- There are multiple ways to install Jenkins. Choose one of the following methods:
- Using the package manager:
- Update your package index:
sudo apt update
- Install Java (Jenkins requires Java):
sudo apt install openjdk-11-jdk
- 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:
sudo apt update sudo apt install jenkins
- Start Jenkins:
sudo systemctl start jenkins
- Update your package index:
- Using Docker:
- Pull the Jenkins image:
docker pull jenkins/jenkins
- Run Jenkins:
docker run -d -p 8080:8080 jenkins/jenkins
- Pull the Jenkins image:
- Using the package manager:
Step 3: Create Your First Jenkins CI/CD Job
- Set up a basic CI/CD job using Maven and GitHub:
- Access Jenkins by opening
http://localhost:8080
in your web browser. - Unlock Jenkins using the initial admin password found in
/var/lib/jenkins/secrets/initialAdminPassword
. - Install suggested plugins.
- Create a new item:
- Select "Freestyle project."
- Name your project.
- Configure Source Code Management:
- Choose Git and enter your GitHub repository URL.
- Set up Build Triggers:
- Check "GitHub hook trigger for GITScm polling."
- Add a Build step:
- Select "Invoke top-level Maven targets" and specify the goals (e.g.,
clean install
).
- Select "Invoke top-level Maven targets" and specify the goals (e.g.,
- Save and build the job.
- Access Jenkins by opening
Step 4: Integrate Jenkins with GitHub
- Set up a webhook in GitHub:
- Go to your repository on GitHub.
- Navigate to "Settings" > "Webhooks."
- Click "Add webhook."
- Enter the payload URL as
http://<your-jenkins-url>/github-webhook/
. - Set content type to
application/json
. - Select "Just the push event" and click "Add webhook."
Step 5: Explore Jenkins Master-Slave Architecture
- Understand the architecture:
- Master: Manages the build process and schedules jobs.
- Slave: Executes the jobs assigned by the master.
- To set up a slave:
- Install Java on the slave machine.
- From the master, go to "Manage Jenkins" > "Manage Nodes and Clouds."
- Create a new node and configure it.
Step 6: Create a Jenkins CI/CD Pipeline
- Create a pipeline job:
- Select "New Item" and choose "Pipeline."
- Define the pipeline script:
- Use a Jenkinsfile stored in your GitHub repo, or write the script directly.
- Example Jenkinsfile:
pipeline { agent any stages { stage('Build') { steps { sh 'mvn clean install' } } stage('Test') { steps { sh 'mvn test' } } stage('Deploy') { steps { sh 'deploy.sh' } } } }
Step 7: Implement Multi-branch Pipeline
- Use multi-branch pipelines for better management of branches:
- Create a new item and select "Multibranch Pipeline."
- Add a branch source (e.g., Git).
- Configure it to scan the repository for branches and pull request builds.
Conclusion
By following this tutorial, you have learned the basics of Jenkins, from installation to creating CI/CD jobs and integrating with GitHub. You now have a foundation to explore more advanced features such as Jenkins pipelines and distributed builds. Next, consider experimenting with different plugins and configurations to optimize your CI/CD processes further.