Advanced Jenkins Guide


Introduction
Jenkins is a widely used open-source automation server that facilitates continuous integration (CI) and continuous delivery (CD). This guide covers advanced Jenkins features, including pipeline creation, shared libraries, security practices, and performance optimization.
1. Jenkins Pipelines
Jenkins pipelines define the entire build process from code commit to production deployment using a Jenkinsfile
. Pipelines can be scripted or declarative.
1.1 Declarative Pipeline
pipeline {
agent any
environment {
APP_ENV = 'production'
}
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/user/repo.git'
}
}
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Deploy') {
steps {
sh './deploy.sh'
}
}
}
post {
always {
archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
}
failure {
mail to: 'admin@example.com', subject: 'Build Failed', body: 'Check Jenkins.'
}
}
}
1.2 Scripted Pipeline
node {
stage('Checkout') {
git branch: 'main', url: 'https://github.com/user/repo.git'
}
stage('Build') {
sh 'mvn clean package'
}
stage('Test') {
sh 'mvn test'
}
stage('Deploy') {
sh './deploy.sh'
}
}
2. Shared Libraries
Shared libraries allow code reuse across multiple Jenkins pipelines.
2.1 Directory Structure
(root)
├── vars/
│ └── common.groovy
└── resources/
2.2 Creating a Shared Library
vars/common.groovy
:
def hello(String name) {
echo "Hello, ${name}!"
}
2.3 Using Shared Library in Pipeline
@Library('my-shared-library') _
pipeline {
agent any
stages {
stage('Greet') {
steps {
common.hello('Jenkins User')
}
}
}
}
3. Jenkins Security Best Practices
Enable Role-Based Access Control (RBAC)
Restrict Anonymous Access
Use Credentials Binding Plugin
Encrypt Communication (HTTPS)
3.1 Setting Up RBAC
Install the Role-based Authorization Strategy plugin.
Manage Jenkins > Configure Global Security > Authorization > Role-based.
# Example to add a Jenkins Admin User
docker exec -it jenkins bash
jenkins-add-user --username admin --password secret
4. Jenkins Performance Optimization
Allocate Adequate Resources: Ensure sufficient CPU and memory.
Disable Unused Plugins: Regularly audit and remove unused plugins.
Configure Build Executors: Increase executors based on machine capacity.
4.1 Increasing JVM Heap Size
# Modify Jenkins service config
JAVA_ARGS="-Xms1024m -Xmx4096m"
systemctl restart jenkins
4.2 Using External Storage for Artifacts
Configure external storage in Jenkins > Manage Jenkins > System Configuration
.
archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
5. Monitoring Jenkins
Use plugins like Monitoring and Prometheus to track resource utilization.
# Install Monitoring Plugin
Manage Jenkins > Plugin Manager > Install "Monitoring"
# Access Monitoring Dashboard
http://your-jenkins-url/monitoring
6. Jenkins Agents and Nodes
Jenkins uses agents and nodes to distribute work across multiple machines for better scalability and performance.
6.1 Agents vs. Nodes
Node: A machine where Jenkins runs jobs (includes the controller and any agents).
Agent: A node that receives tasks from the Jenkins controller.
6.2 Configuring a Jenkins Agent
Navigate to
Manage Jenkins > Manage Nodes and Clouds
.Click
New Node
and configure the agent properties.
6.3 Launching a Jenkins Agent via SSH
# Run on agent machine
java -jar agent.jar -jnlpUrl http://jenkins-controller:8080/computer/agent-name/slave-agent.jnlp -secret YOUR_SECRET_KEY -workDir "agent-directory"
6.4 Using Specific Agents in Pipelines
pipeline {
agent {
label 'linux-agent'
}
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
}
}
Conclusion
By mastering advanced Jenkins techniques such as pipelines, shared libraries, and security best practices, you can optimize your CI/CD workflows, improve security, and scale effectively.
Subscribe to my newsletter
Read articles from Ashutosh Verma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
