Jenkins - pranavkumarpk01/MD-DevOps GitHub Wiki
π What is Jenkins?
Jenkins is an open-source automation server written in Java. It is used to automate parts of software development, including building, testing, and deploying code.
π§ Jenkins Installation
On Ubuntu/Debian:
sudo apt update
sudo apt install openjdk-11-jdk -y
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install jenkins -y
sudo systemctl start jenkins
sudo systemctl enable jenkins
π₯ Jenkins: Creating Users, Jobs, and Master-Slave Configuration
β Creating Users in Jenkins
Jenkins allows multiple users to access and manage jobs securely.
Steps:
- Go to Manage Jenkins > Manage Users.
- Click on Create User.
- Fill in the following details:
- Username
- Password
- Full name
- Click Create User.
π You must have Matrix-based security or Role-based authorization enabled to manage users.
βοΈ Creating Jobs in Jenkins
Jenkins Jobs are tasks like building a project, running tests, deploying apps, etc.
Steps to Create a Job:
- From Jenkins Dashboard, click New Item.
- Enter a job name.
- Choose a type:
- Freestyle Project (GUI-based)
- Pipeline (Scripted)
- Multi-configuration Project (for matrix builds)
- Click OK.
- Configure the job:
- Source Code Management: Git, SVN, etc.
- Build Triggers: SCM polling, schedule, webhook
- Build Steps: Shell commands, Maven, Gradle, etc.
- Post-build Actions: Email notifications, archiving, etc.
- Click Save.
π Once saved, you can click Build Now to run the job.
π Configuring Jenkins Master-Slave Architecture
Jenkins supports a distributed build architecture with Master and Slave (Agent) nodes.
π‘ Why Use Master-Slave?
- Distribute builds across multiple machines.
- Reduce load on master.
- Run jobs in parallel.
- Run jobs in specific environments (labels/tags).
π§± Master:
- Central Jenkins server.
- Manages UI, job scheduling, result tracking.
- Can also execute jobs.
βοΈ Slave (Agent):
- Remote machine connected to Master.
- Executes jobs as per Master's instruction.
π§ Steps to Configure Master-Slave:
- Go to Manage Jenkins > Manage Nodes and Clouds.
- Click on New Node.
- Give a name, select Permanent Agent, and click OK.
- Fill in:
- Remote root directory (e.g.,
/home/jenkins
) - Labels (e.g.,
linux
,build
,test
) - Launch method:
- SSH
- Java Web Start (deprecated)
- Custom launcher
- Remote root directory (e.g.,
- Save the node.
- Jenkins will attempt to connect to the slave machine and establish communication.
π Requirements on Slave:
- Java should be installed.
- Jenkins master must be able to SSH into slave.
π Verify:
- Node status will show as Connected and Idle if configured correctly.
- Use labels to bind jobs to specific slaves.
β οΈ Ensure port 50000 (JNLP) or SSH port is open between master and slave for communication.
π Jenkins Pipelines, Jenkinsfiles, and Git Integration
π Creating Pipelines in Jenkins
Jenkins Pipeline is a suite of plugins that supports implementing continuous delivery pipelines as code.
πΉ Types of Pipelines:
- Declarative: Simpler, structured syntax
- Scripted: More flexible, Groovy-based
β Steps to Create a Pipeline:
- From Jenkins Dashboard β Click New Item.
- Enter a name and select Pipeline.
- Click OK.
- In the configuration:
- Add description (optional).
- Under Pipeline, choose:
- Pipeline script (inline)
- Or Pipeline script from SCM (Git repo).
- Write or paste your pipeline code (Jenkinsfile).
- Save and click Build Now to execute.
π§Ύ Example Declarative Pipeline:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building the application...'
}
}
stage('Test') {
steps {
echo 'Running tests...'
}
}
stage('Deploy') {
steps {
echo 'Deploying the app...'
}
}
}
}
π Git Integration with Jenkins
Integrating Git with Jenkins enables version-controlled CI/CD pipelines and seamless automation with your source code.
π¦ Step-by-Step Git Integration
1οΈβ£ Install Git Plugin in Jenkins
The Git Plugin is required to enable Git SCM features in Jenkins.
π§ How to Install:
- Navigate to: Manage Jenkins β Manage Plugins
- Go to the Available tab
- Search for:
Git plugin
- Click Install without restart
π You can also check the Installed tab to verify if Git plugin is already installed.
2οΈβ£ Configure Global Git Settings
Jenkins needs to know the location of the Git binary on the system.
- Go to: Manage Jenkins β Global Tool Configuration
- Under Git, click Add Git
- Name it (e.g.,
Default Git
) - Let Jenkins auto-detect the path OR manually set the Git executable path (like
/usr/bin/git
)
3οΈβ£ Create Jenkins Job with Git Integration
For Freestyle Projects:
- Go to New Item β Enter job name β Select Freestyle Project
- Under Source Code Management, choose Git
- Enter:
- Repository URL (e.g.,
https://github.com/user/repo.git
) - Branch to build (default is
*/main
or*/master
) - Credentials (if itβs a private repo)
- Repository URL (e.g.,
For Pipeline Jobs:
Use Git inside the Jenkinsfile
like this:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/user/repo.git'
}
}
}
}
π Managing Git Credentials in Jenkins
π₯ Where to Add Credentials
- Navigate to: Manage Jenkins β Credentials
- Choose the appropriate domain (e.g., Global)
- Click Add Credentials
π§Ύ Types of Credentials
Type | Description |
---|---|
Username/Password | For HTTPS Git URLs requiring a username and password |
SSH Username with Private Key | For Git over SSH (e.g., GitHub using SSH key) |
Secret Text | For personal access tokens (PAT) |
Certificate | For advanced use-cases involving cert-based auth |
π― Use
Secret Text
if you're integrating GitHub using a Personal Access Token (PAT) for enhanced security.
π Using Credentials in Jenkins Job
- When configuring Git in Freestyle or Pipeline job, select the appropriate credentialsId.
- Example in
Jenkinsfile
:
git url: 'https://github.com/user/repo.git', credentialsId: 'github-token-id'