
- DevOps - Home
- DevOps - Traditional SDLC
- DevOps - History
- DevOps - Architecture
- DevOps - Lifecycle
- DevOps - Tools
- DevOps - Automation
- DevOps - Workflow
- DevOps - Pipeline
- DevOps - Benefits
- DevOps - Use Cases
- DevOps - Stakeholders
- DevOps - Certifications
- DevOps - Essential Skills
- DevOps - Job Opportunities
- DevOps - Agile
- DevOps - Lean Principles
- DevOps - AWS Solutions
- DevOps - Azure Solutions
- DevOps Lifecycle
- DevOps - Continuous Development
- DevOps - Continuous Integration
- DevOps - Continuous Testing
- DevOps - Continue Delivery
- DevOps - Continuous Deployment
- DevOps - Continuous Monitoring
- DevOps - Continuous Improvement
- DevOps Infrastructure
- DevOps - Infrastructure
- DevOps - Git
- DevOps - Docker
- DevOps - Selenium
- DevOps - Jenkins
- DevOps - Puppet
- DevOps - Ansible
- DevOps - Kubernetes
- DevOps - Jira
- DevOps - ELK
- DevOps - Terraform
DevOps - Jenkins
In this chapter, we will look at Jenkins, which is an open-source automation server. It plays an important role in making continuous integration and delivery easier.
We will talk about Jenkins architecture. We will also discuss how to set it up, configure jobs, and connect it with version control systems. Finally, we will explain how to use pipelines as code. This will give us a complete guide to using Jenkins in our DevOps practices.
Understanding Jenkins Architecture
Jenkins is a strong automation server that helps us with continuous integration and continuous delivery, we call it CI/CD. Its design is based on a master-agent model. This model helps us to be more scalable and reliable.
- Jenkins Master − This is the main control unit that runs the build process. It schedules jobs, sends builds to agents, and checks their progress. It gives us the web interface for setting things up and monitoring.
- Jenkins Agents (Slaves) − These are separate machines that carry out jobs from the master. They can work on different systems like Windows or Linux. They allow us to run builds in a distributed way to make things faster.
- Job Configuration − We define jobs as a series of steps that Jenkins will run. We can set them up using the user interface or with configuration files.
- Plugins − Jenkins has a flexible design. This means we can add many plugins. These plugins help us with notifications, version control, build tools, and more.
This design lets us use Jenkins to manage complicated CI/CD tasks across different environments.
Setting up Jenkins Environment
We can set up a Jenkins environment by following these easy steps.
Following are the prerequisites −
- Java − We need to make sure that Java Development Kit (JDK) is installed. It should be Java 8 or higher.
- Operating System − It can run on Windows, macOS, or Linux.
Installation Steps
Download Jenkins − We go to the Jenkins download page and pick the right package for our system.
Install Jenkins − For Windows, run the installer and just follow the setup wizard. For Linux, use package managers like this −
sudo apt update sudo apt install jenkins
For macOS − We can use Homebrew like this −
brew install jenkins-lts
Start Jenkins − On Linux, we run this command −
sudo systemctl start jenkins
On Windows, we look for Jenkins in the services and start it from there.
Access Jenkins
We open a browser and go to http://localhost:8080. We need to unlock Jenkins with the initial admin password. We can find it by running this command −
cat /var/lib/jenkins/secrets/initialAdminPassword
Configure Jenkins
Follow the setup wizard. It helps us to install suggested plugins and make an admin user.
Creating and Configuring Jenkins Jobs
Creating and configuring jobs in Jenkins is very important for automating builds and deployments. Jenkins can handle different types of jobs. These include Freestyle projects, Pipeline projects, and Multibranch Pipelines.
Steps to Create a Jenkins Job
Access Jenkins Dashboard − Open Jenkins in a web browser.
Create a New Job − Click on "New Item" on the dashboard. Enter a name for our job.
We select the job type like Freestyle project or Pipeline and then click "OK."
Configure Job Settings −
- General − We add a description. We can also choose to discard old builds.
- Source Code Management − We set up our version control system like Git.
Repository URL: https://github.com/user/repo.git Credentials: [Add credentials if needed]
- Build Triggers − We set triggers such as "Build periodically" or "Poll SCM".
- Build Environment − We configure any needed environment settings.
- Build Steps − We define the steps to run. For example, we can invoke a shell script.
# Example shell command echo "Building the project..."
Post-build Actions − We specify what to do after the build. This could be sending notifications or archiving artifacts.
Save and Build: We click "Save" to keep our settings. We can start a build by clicking "Build Now" from the job page.
With these settings, we can make Jenkins jobs that automate our processes very well.
Integrating Jenkins with Version Control Systems
We need to integrate Jenkins with version control systems (VCS) to automate the build process. Jenkins works with many VCS like Git, Subversion (SVN), and Mercurial.
Steps to Integrate Jenkins with Git
Install Git Plugin − Go to Manage Jenkins and then to Manage Plugins. Install the "Git Plugin".
Configure Jenkins Global Settings − Click on Manage Jenkins and then Global Tool Configuration. Set up the path for Git installation.
Create a New Jenkins Job − We click on New Item, choose Freestyle project, and give it a name.
Configure Source Code Management − In the job settings, we select Git. We enter the repository URL like https://github.com/user/repo.git. If the repository is private, we add the credentials.
Set Build Triggers − We enable Poll SCM to check for changes at certain times, for example H/5 * * * *.
Example Configuration
# Jenkins Job Configuration scm: git: branches: - master remote: url: https://github.com/user/repo.git
By following these steps, Jenkins will pull the latest code changes from the VCS we specified. This helps us with Continuous Integration.
Implementing Continuous Integration with Jenkins
Continuous Integration (CI) is a way we develop software. In this method, we automatically build, test, and deploy our code changes. This helps us keep quality and integration. Jenkins helps us with CI by automating these steps.
Steps to Implement CI with Jenkins
Create a Jenkins Job − We can choose Freestyle or Pipeline job types. We need to set up the source code repository. For example, we can use Git.
Configure Build Triggers − We set triggers to check the SCM or use webhooks. This helps to build automatically.
triggers { scm('H/5 * * * *') // This checks SCM every 5 minutes }
Define Build Steps − We specify the build tools. This could be Maven, Gradle, or scripts.
steps { sh 'mvn clean package' // This is for Maven projects }
Add Post-Build Actions − We can set up notifications, save artifacts, or deploy builds.
post { success { archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true } }
Test Automation − We can add testing frameworks like JUnit or Selenium. They run tests after builds automatically.
Monitor Builds − We can use Jenkins dashboards. This helps us to watch build statuses and logs.
By following these steps, we can easily implement Continuous Integration in our development work with Jenkins.
Example of a Declarative Pipeline
pipeline { agent any stages { stage('Build') { steps { echo 'Building...' sh 'make' } } stage('Test') { steps { echo 'Testing...' sh 'make test' } } stage('Deploy') { steps { echo 'Deploying...' sh 'make deploy' } } } }
By using Pipelines as Code, we can make our DevOps work better and improve how we deploy.
Conclusion
In this chapter, we looked at the basics of Jenkins. We talked about its design, how to set it up, how to configure jobs, and how to connect it with version control systems. We also discussed how to use continuous integration and Jenkins Pipeline as Code.