Jenkins Agents self-hosted setup
Configure Jenkins agent nodes for distributed builds with DevOps Hub integration
Jenkins Agents enable distributed build execution across multiple machines, allowing you to scale your CI/CD infrastructure horizontally. This guide covers setting up self-hosted Jenkins agents with DevOps Hub integration for efficient pipeline management.
Platform overview#
Jenkins follows a controller-agent architecture where the Jenkins controller (master) coordinates build execution across multiple agent nodes. This distributed approach provides several benefits:
- Scalability - Distribute workload across multiple machines
- Isolation - Separate build environments prevent interference
- Specialization - Dedicated agents for specific platforms or tools
- Performance - Parallel execution reduces overall build times
1┌─────────────────────┐2│ Jenkins Controller │3│ (Master) │4│ │5│ ┌─────────────────┐ │6│ │ Job Queue │ │7│ │ Build History │ │8│ │ Agent Manager │ │9│ └─────────────────┘ │10└──────────┬──────────┘11 │ Distributes jobs12 │13 ┌──────▼──────┬─────────────┬──────────────┐14 │ │ │ │15┌───▼────┐ ┌───▼────┐ ┌───▼────┐ ┌───▼────┐16│Agent 1 │ │Agent 2 │ │Agent 3 │ │Agent N │17│Linux │ │Windows │ │macOS │ │Docker │18│Build │ │Build │ │Build │ │Cloud │19└────────┘ └────────┘ └────────┘ └────────┘Prerequisites#
Before setting up Jenkins agents, ensure you have:
Jenkins Controller Setup#
- Jenkins LTS version 2.401+ running and accessible
- Administrative access to Jenkins controller
- Network connectivity between controller and agent machines
- Java 11 or 17 installed on controller
Agent Infrastructure Requirements#
- Compute resources appropriate for build workloads
- Operating system compatible with your build requirements
- Network access to Jenkins controller (typically port 8080)
- Java Runtime Environment 11 or 17
- Build tools required for your projects (Maven, Gradle, Node.js, etc.)
DevOps Hub Integration#
- DevOps Hub API token with project access permissions
- Project ID for DevOps Hub integration
- Branch management permissions for environment creation
Infrastructure setup#
1. Prepare agent machines#
For optimal performance, provision dedicated machines for Jenkins agents:
Minimum specifications:
- CPU: 2+ cores per concurrent build
- RAM: 4GB+ per concurrent build
- Storage: 50GB+ with fast I/O
- Network: Reliable connection to Jenkins controller
Recommended setup for production:
1# Ubuntu/Debian agent setup2sudo apt update && sudo apt upgrade -y34# Install Java 17 (recommended)5sudo apt install openjdk-17-jre-headless -y67# Verify Java installation8java -version910# Install build tools11sudo apt install -y git curl wget unzip12sudo apt install -y build-essential # For C/C++ compilation2. Network configuration#
Ensure proper network connectivity between controller and agents:
Required ports:
- SSH agents: Port 22 (SSH)
- JNLP agents: Random port assigned by controller
- Inbound agents: Port defined in agent configuration
Firewall configuration:
1# Allow SSH access (for SSH agents)2sudo ufw allow ssh34# Allow Jenkins controller access5sudo ufw allow from JENKINS_CONTROLLER_IP67# Enable firewall8sudo ufw --force enableAgent installation#
SSH agents (Linux/macOS)#
SSH agents use SSH protocol for secure communication with the Jenkins controller.
1. Create Jenkins user#
1# Create dedicated user for Jenkins2sudo adduser jenkins34# Add to docker group (if Docker builds needed)5sudo usermod -a -G docker jenkins67# Switch to jenkins user8sudo su - jenkins2. SSH key setup#
1# Generate SSH key pair on Jenkins controller2ssh-keygen -t rsa -b 4096 -f ~/.ssh/jenkins_agent_key34# Copy public key to agent5ssh-copy-id -i ~/.ssh/jenkins_agent_key.pub jenkins@AGENT_IP3. Configure SSH agent in Jenkins#
- Navigate to Manage Jenkins > Manage Nodes and Clouds
- Click New Node
- Enter node name and select Permanent Agent
- Configure agent settings:
1Name: linux-agent-012Description: Linux build agent for DevOps Hub projects3Number of executors: 24Remote root directory: /home/jenkins/jenkins-agent5Labels: linux ubuntu docker devops-hub6Usage: Use this node as much as possible7Launch method: Launch agents via SSH8Host: AGENT_IP9Credentials: [Select SSH private key credential]10Host Key Verification Strategy: Known hosts file Verification Strategy11Availability: Keep this agent online as much as possibleJNLP agents (Java Web Start)#
JNLP agents connect outbound from agent to controller, useful when SSH isn't available.
1. Create JNLP agent#
- In Jenkins, create a new Permanent Agent
- Set Launch method to Launch agent by connecting it to the controller
- Save the configuration to generate the agent secret
2. Download and start agent#
1# Download agent.jar from Jenkins controller2wget http://JENKINS_CONTROLLER:8080/jnlpJars/agent.jar34# Start agent with secret5java -jar agent.jar -jnlpUrl http://JENKINS_CONTROLLER:8080/computer/AGENT_NAME/slave-agent.jnlp -secret AGENT_SECRET -workDir "/home/jenkins/jenkins-agent"3. Run as system service#
Create systemd service for automatic startup:
1# Create service file2sudo tee /etc/systemd/system/jenkins-agent.service > /dev/null <<EOF3[Unit]4Description=Jenkins Agent5After=network.target67[Service]8Type=simple9User=jenkins10WorkingDirectory=/home/jenkins11ExecStart=/usr/bin/java -jar /home/jenkins/agent.jar -jnlpUrl http://JENKINS_CONTROLLER:8080/computer/AGENT_NAME/slave-agent.jnlp -secret AGENT_SECRET -workDir "/home/jenkins/jenkins-agent"12Restart=always13RestartSec=101415[Install]16WantedBy=multi-user.target17EOF1819# Enable and start service20sudo systemctl enable jenkins-agent21sudo systemctl start jenkins-agent22sudo systemctl status jenkins-agentConfiguration#
Node management and labels#
Labels help route specific jobs to appropriate agents:
Common label patterns:
- Platform labels:
linux,windows,macos - Tool labels:
docker,nodejs,python,maven - Environment labels:
dev,staging,prod - Capability labels:
gpu,high-memory,ssd
Example agent configuration:
1Labels: linux ubuntu-20.04 docker nodejs python maven devops-hubExecutors and concurrency#
Configure appropriate number of executors based on agent resources:
1Number of executors = CPU cores / 2 (for I/O bound builds)2Number of executors = CPU cores (for CPU bound builds)Workspace management#
Set up clean workspace policies:
1pipeline {2 agent { label 'devops-hub' }3 options {4 // Clean workspace before build5 skipDefaultCheckout()6 // Keep only last 10 builds7 buildDiscarder(logRotator(numToKeepStr: '10'))8 }9 stages {10 stage('Checkout') {11 steps {12 cleanWs()13 checkout scm14 }15 }16 }17}DevOps Hub integration#
Credential management#
Store DevOps Hub credentials securely in Jenkins:
- Navigate to Manage Jenkins > Manage Credentials
- Add Secret text credential for DevOps Hub API token
- Add Username with password for DevOps Hub project access
1// Access credentials in pipeline2environment {3 DEVOPS_HUB_TOKEN = credentials('devops-hub-api-token')4 PROJECT_ID = credentials('devops-hub-project-id')5 DEVOPS_HUB_URL = 'https://console.assistance.bg/api/v2'6}Pipeline integration#
Complete Jenkins pipeline with DevOps Hub integration:
1pipeline {2 agent { label 'devops-hub' }34 environment {5 DEVOPS_HUB_TOKEN = credentials('devops-hub-api-token')6 PROJECT_ID = credentials('devops-hub-project-id')7 DEVOPS_HUB_URL = 'https://console.assistance.bg/api/v2'8 BRANCH_NAME = "jenkins-build-${BUILD_NUMBER}"9 DATABASE_URL = ""10 }1112 options {13 buildDiscarder(logRotator(numToKeepStr: '10'))14 timeout(time: 30, unit: 'MINUTES')15 }1617 stages {18 stage('Setup Environment') {19 steps {20 script {21 // Create DevOps Hub branch for this build22 def response = sh(23 script: """24 curl -s -X POST "${DEVOPS_HUB_URL}/projects/${PROJECT_ID}/branches" \25 -H "Authorization: Bearer ${DEVOPS_HUB_TOKEN}" \26 -H "Content-Type: application/json" \27 -d '{"branch": {"name": "${BRANCH_NAME}", "parent_id": "main"}}'28 """,29 returnStdout: true30 ).trim()3132 def json = readJSON text: response33 env.DATABASE_URL = json.branch.database_url3435 echo "Created DevOps Hub branch: ${BRANCH_NAME}"36 echo "Database URL: ${DATABASE_URL}"37 }38 }39 }4041 stage('Checkout') {42 steps {43 checkout scm44 }45 }4647 stage('Install Dependencies') {48 when {49 anyOf {50 changeset "package*.json"51 not { fileExists 'node_modules' }52 }53 }54 steps {55 sh '''56 npm ci --prefer-offline --no-audit57 '''58 }59 }6061 stage('Database Setup') {62 steps {63 script {64 // Run database migrations65 sh """66 export DATABASE_URL="${DATABASE_URL}"67 npm run migrate:deploy68 """69 }70 }71 }7273 stage('Run Tests') {74 parallel {75 stage('Unit Tests') {76 steps {77 sh '''78 export DATABASE_URL="${DATABASE_URL}"79 npm run test:unit -- --coverage --ci80 '''81 }82 post {83 always {84 publishTestResults testResultsPattern: 'test-results.xml'85 publishCoverageResults([86 [87 path: 'coverage/clover.xml',88 type: 'clover'89 ]90 ])91 }92 }93 }9495 stage('Integration Tests') {96 steps {97 sh '''98 export DATABASE_URL="${DATABASE_URL}"99 npm run test:integration -- --ci100 '''101 }102 }103104 stage('Linting') {105 steps {106 sh '''107 npm run lint -- --format=checkstyle --output-file=lint-results.xml108 '''109 }110 post {111 always {112 recordIssues(113 enabledForFailure: true,114 aggregatingResults: false,115 tools: [checkStyle(pattern: 'lint-results.xml')]116 )117 }118 }119 }120 }121 }122123 stage('Build Application') {124 steps {125 sh '''126 npm run build127 '''128129 // Archive build artifacts130 archiveArtifacts artifacts: 'dist/**', fingerprint: true131 }132 }133134 stage('Deploy to Staging') {135 when {136 anyOf {137 branch 'main'138 branch 'develop'139 }140 }141 steps {142 script {143 // Deploy using DevOps Hub branch144 sh """145 export DATABASE_URL="${DATABASE_URL}"146 npm run deploy:staging147 """148149 // Store deployment info150 def deploymentUrl = sh(151 script: "echo 'https://${BRANCH_NAME}.staging.assistance.bg'",152 returnStdout: true153 ).trim()154155 // Add deployment status to DevOps Hub156 sh """157 curl -X PATCH "${DEVOPS_HUB_URL}/projects/${PROJECT_ID}/branches/${BRANCH_NAME}" \158 -H "Authorization: Bearer ${DEVOPS_HUB_TOKEN}" \159 -H "Content-Type: application/json" \160 -d '{"branch": {"metadata": {"deployment_url": "${deploymentUrl}", "jenkins_build": "${BUILD_NUMBER}"}}}'161 """162163 echo "Deployed to: ${deploymentUrl}"164 }165 }166 }167 }168169 post {170 always {171 // Clean up DevOps Hub branch for short-lived builds172 script {173 if (env.BRANCH_NAME && env.BRANCH_NAME.startsWith('jenkins-build-')) {174 sh """175 curl -X DELETE "${DEVOPS_HUB_URL}/projects/${PROJECT_ID}/branches/${BRANCH_NAME}" \176 -H "Authorization: Bearer ${DEVOPS_HUB_TOKEN}"177 """178 echo "Cleaned up DevOps Hub branch: ${BRANCH_NAME}"179 }180 }181 }182183 success {184 // Notify success185 script {186 if (env.CHANGE_ID) {187 // This is a PR build188 sh """189 curl -X POST "${DEVOPS_HUB_URL}/projects/${PROJECT_ID}/notifications" \190 -H "Authorization: Bearer ${DEVOPS_HUB_TOKEN}" \191 -H "Content-Type: application/json" \192 -d '{193 "type": "build_success",194 "message": "✅ Jenkins build #${BUILD_NUMBER} succeeded for PR #${CHANGE_ID}",195 "metadata": {196 "build_number": "${BUILD_NUMBER}",197 "pr_number": "${CHANGE_ID}",198 "branch": "${CHANGE_BRANCH}"199 }200 }'201 """202 }203 }204 }205206 failure {207 // Notify failure208 sh """209 curl -X POST "${DEVOPS_HUB_URL}/projects/${PROJECT_ID}/notifications" \210 -H "Authorization: Bearer ${DEVOPS_HUB_TOKEN}" \211 -H "Content-Type: application/json" \212 -d '{213 "type": "build_failure",214 "message": "❌ Jenkins build #${BUILD_NUMBER} failed on ${BRANCH_NAME}",215 "metadata": {216 "build_number": "${BUILD_NUMBER}",217 "branch": "${BRANCH_NAME}",218 "failure_stage": "${env.FAILED_STAGE}"219 }220 }'221 """222 }223 }224}Cloud provisioning#
Docker agents#
Use Docker for ephemeral, clean build environments:
1. Install Docker plugin#
- Navigate to Manage Jenkins > Manage Plugins
- Install Docker Plugin
- Restart Jenkins
2. Configure Docker cloud#
1// Docker agent template2pipeline {3 agent {4 docker {5 image 'node:18-alpine'6 args '-v /var/run/docker.sock:/var/run/docker.sock'7 }8 }910 stages {11 stage('Build') {12 steps {13 sh 'npm install && npm run build'14 }15 }16 }17}3. Dynamic Docker agents#
Configure dynamic Docker agent provisioning:
- Manage Jenkins > Manage Nodes and Clouds > Configure Clouds
- Add Docker Cloud
- Configure Docker host URL:
unix:///var/run/docker.sock - Add Docker templates for different environments
Kubernetes agents#
For scalable cloud-native builds:
1. Install Kubernetes plugin#
1# Install Kubernetes plugin2# Manage Jenkins > Manage Plugins > Available > Kubernetes2. Configure Kubernetes cloud#
1# kubernetes-agent-template.yaml2apiVersion: v13kind: Pod4metadata:5 labels:6 jenkins: agent7spec:8 containers:9 - name: jnlp10 image: jenkins/inbound-agent:latest11 env:12 - name: JENKINS_URL13 value: "http://jenkins-controller:8080"14 - name: build15 image: node:18-alpine16 command:17 - cat18 tty: true19 volumeMounts:20 - name: docker-sock21 mountPath: /var/run/docker.sock22 volumes:23 - name: docker-sock24 hostPath:25 path: /var/run/docker.sock3. Use Kubernetes agents in pipeline#
1pipeline {2 agent {3 kubernetes {4 yaml """5 apiVersion: v16 kind: Pod7 spec:8 containers:9 - name: build10 image: node:18-alpine11 command:12 - cat13 tty: true14 - name: docker15 image: docker:dind16 securityContext:17 privileged: true18 """19 }20 }2122 stages {23 stage('Build') {24 steps {25 container('build') {26 sh 'npm install && npm run build'27 }28 }29 }3031 stage('Docker Build') {32 steps {33 container('docker') {34 sh 'docker build -t myapp:${BUILD_NUMBER} .'35 }36 }37 }38 }39}AWS EC2 agents#
Dynamic EC2 instance provisioning:
1. Install EC2 plugin#
- Install Amazon EC2 Plugin
- Configure AWS credentials
- Set up EC2 cloud configuration
2. EC2 agent template#
1// EC2 agent configuration2AMI ID: ami-0c55b159cbfafe1d0 // Ubuntu 20.04 LTS3Instance Type: t3.medium4Security Group: jenkins-agents (ports 22, 8080)5Key Pair: jenkins-ec2-key6User Data: |7 #!/bin/bash8 apt-get update9 apt-get install -y openjdk-17-jre-headless10 # Additional setup scriptsMulti-OS support#
Windows agents#
1. Install Java on Windows#
1# Install Java using Chocolatey2choco install openjdk17 -y34# Verify installation5java -version2. Configure Windows agent#
1# Create jenkins user2net user jenkins PASSWORD /add3net localgroup administrators jenkins /add45# Create agent directory6mkdir C:\jenkins78# Download and run agent9Invoke-WebRequest -Uri "http://JENKINS_CONTROLLER:8080/jnlpJars/agent.jar" -OutFile "C:\jenkins\agent.jar"1011# Run as Windows service using NSSM12nssm install JenkinsAgent13nssm set JenkinsAgent Application "java"14nssm set JenkinsAgent AppParameters "-jar C:\jenkins\agent.jar -jnlpUrl http://JENKINS_CONTROLLER:8080/computer/windows-agent/slave-agent.jnlp -secret SECRET -workDir C:\jenkins"15nssm start JenkinsAgentmacOS agents#
1. Install dependencies#
1# Install Homebrew (if not installed)2/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"34# Install Java5brew install openjdk@1767# Create symlink8sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk2. Configure macOS agent#
1# Create jenkins user2sudo dscl . -create /Users/jenkins3sudo dscl . -create /Users/jenkins UserShell /bin/bash4sudo dscl . -create /Users/jenkins RealName "Jenkins Agent"5sudo dscl . -create /Users/jenkins UniqueID 10016sudo dscl . -create /Users/jenkins PrimaryGroupID 207sudo dscl . -create /Users/jenkins NFSHomeDirectory /Users/jenkins89# Create home directory10sudo createhomedir -c -u jenkins1112# Set up SSH keys or JNLP agent as described earlierCross-platform pipeline#
1pipeline {2 stages {3 stage('Build Matrix') {4 matrix {5 axes {6 axis {7 name 'PLATFORM'8 values 'linux', 'windows', 'macos'9 }10 }11 stages {12 stage('Platform Build') {13 agent { label "${PLATFORM}" }14 steps {15 script {16 if (PLATFORM == 'windows') {17 bat 'npm install && npm run build'18 } else {19 sh 'npm install && npm run build'20 }21 }22 }23 }24 }25 }26 }27 }28}Test pipeline#
Complete test pipeline#
Create a comprehensive test pipeline to validate your Jenkins agent setup:
1pipeline {2 agent none34 environment {5 DEVOPS_HUB_TOKEN = credentials('devops-hub-api-token')6 PROJECT_ID = credentials('devops-hub-project-id')7 DEVOPS_HUB_URL = 'https://console.assistance.bg/api/v2'8 }910 stages {11 stage('Agent Connectivity Test') {12 parallel {13 stage('Linux Agent Test') {14 agent { label 'linux' }15 steps {16 sh '''17 echo "Testing Linux agent..."18 uname -a19 java -version20 docker --version21 git --version22 node --version23 npm --version24 '''25 }26 }2728 stage('Windows Agent Test') {29 agent { label 'windows' }30 when {31 expression {32 return Jenkins.instance.nodes.find { it.labelString.contains('windows') } != null33 }34 }35 steps {36 bat '''37 echo "Testing Windows agent..."38 systeminfo | findstr /B "OS Name OS Version"39 java -version40 git --version41 node --version42 npm --version43 '''44 }45 }46 }47 }4849 stage('DevOps Hub Integration Test') {50 agent { label 'devops-hub' }51 steps {52 script {53 def testBranch = "jenkins-test-${BUILD_NUMBER}"5455 // Test branch creation56 def createResponse = sh(57 script: """58 curl -s -w "\\n%{http_code}" -X POST "${DEVOPS_HUB_URL}/projects/${PROJECT_ID}/branches" \59 -H "Authorization: Bearer ${DEVOPS_HUB_TOKEN}" \60 -H "Content-Type: application/json" \61 -d '{"branch": {"name": "${testBranch}"}}'62 """,63 returnStdout: true64 ).trim().split('\n')6566 def statusCode = createResponse[-1]67 def responseBody = createResponse[0..-2].join('\n')6869 if (statusCode == '201' || statusCode == '200') {70 echo "✅ DevOps Hub branch creation: SUCCESS"7172 def json = readJSON text: responseBody73 def databaseUrl = json.branch.database_url74 echo "Database URL: ${databaseUrl}"7576 // Test database connection77 sh """78 export DATABASE_URL="${databaseUrl}"79 timeout 10 bash -c 'until pg_isready -d "\${DATABASE_URL}"; do sleep 1; done'80 echo "✅ Database connection: SUCCESS"81 """8283 // Cleanup test branch84 sh """85 curl -s -X DELETE "${DEVOPS_HUB_URL}/projects/${PROJECT_ID}/branches/${testBranch}" \86 -H "Authorization: Bearer ${DEVOPS_HUB_TOKEN}"87 """88 echo "✅ DevOps Hub branch cleanup: SUCCESS"89 } else {90 error("❌ DevOps Hub integration failed with status ${statusCode}")91 }92 }93 }94 }9596 stage('Build Tools Test') {97 agent { label 'devops-hub' }98 steps {99 sh '''100 echo "Testing build tools..."101102 # Test Docker103 docker run --rm hello-world104 echo "✅ Docker: SUCCESS"105106 # Test npm/node107 npm --version108 node --version109 echo "✅ Node.js/npm: SUCCESS"110111 # Test git112 git --version113 echo "✅ Git: SUCCESS"114115 # Test curl116 curl --version117 echo "✅ Curl: SUCCESS"118 '''119 }120 }121122 stage('Performance Test') {123 agent { label 'devops-hub' }124 steps {125 script {126 def startTime = System.currentTimeMillis()127128 sh '''129 # CPU test130 echo "Testing CPU performance..."131 time dd if=/dev/zero of=/tmp/test bs=1M count=100132133 # Memory test134 echo "Testing memory..."135 free -h136137 # Disk I/O test138 echo "Testing disk I/O..."139 time sync140141 # Network test142 echo "Testing network..."143 ping -c 3 google.com144145 # Clean up146 rm -f /tmp/test147 '''148149 def endTime = System.currentTimeMillis()150 def duration = (endTime - startTime) / 1000151 echo "✅ Performance test completed in ${duration} seconds"152 }153 }154 }155 }156157 post {158 always {159 script {160 // Send test results to DevOps Hub161 def testStatus = currentBuild.result == null ? 'SUCCESS' : currentBuild.result162 def testReport = [163 type: 'jenkins_agent_test',164 status: testStatus,165 build_number: BUILD_NUMBER,166 duration: currentBuild.duration,167 agents_tested: Jenkins.instance.nodes.findAll {168 it.labelString.contains('devops-hub')169 }.size()170 ]171172 sh """173 curl -X POST "${DEVOPS_HUB_URL}/projects/${PROJECT_ID}/test-reports" \174 -H "Authorization: Bearer ${DEVOPS_HUB_TOKEN}" \175 -H "Content-Type: application/json" \176 -d '${groovy.json.JsonOutput.toJson(testReport)}'177 """178 }179 }180 }181}Production deployment#
Agent pool management#
Implement agent pools for different workload types:
1. Create agent pools#
1// Agent pool configurations2def agentPools = [3 'build-pool': [4 'labels': 'linux docker npm maven',5 'capacity': 4,6 'instance_type': 't3.large'7 ],8 'test-pool': [9 'labels': 'linux docker selenium cypress',10 'capacity': 2,11 'instance_type': 't3.xlarge'12 ],13 'deploy-pool': [14 'labels': 'linux aws kubectl terraform',15 'capacity': 1,16 'instance_type': 't3.medium'17 ]18]2. Pool-aware pipeline#
1pipeline {2 stages {3 stage('Build') {4 agent { label 'build-pool && linux' }5 steps {6 sh 'npm run build'7 }8 }910 stage('Test') {11 agent { label 'test-pool && docker' }12 steps {13 sh 'npm run test:e2e'14 }15 }1617 stage('Deploy') {18 agent { label 'deploy-pool && aws' }19 steps {20 sh 'terraform apply -auto-approve'21 }22 }23 }24}Monitoring and metrics#
1. Jenkins monitoring#
1// Monitor agent health2pipeline {3 agent { label 'monitoring' }4 triggers {5 cron('*/5 * * * *') // Every 5 minutes6 }78 stages {9 stage('Agent Health Check') {10 steps {11 script {12 def agents = Jenkins.instance.nodes13 def healthReport = [:]1415 agents.each { agent ->16 def status = agent.toComputer().isOnline() ? 'online' : 'offline'17 def lastSeen = agent.toComputer().getLastConnectTime()1819 healthReport[agent.name] = [20 status: status,21 lastSeen: lastSeen,22 labels: agent.labelString,23 executors: agent.numExecutors24 ]25 }2627 // Send metrics to DevOps Hub28 sh """29 curl -X POST "${DEVOPS_HUB_URL}/projects/${PROJECT_ID}/metrics" \30 -H "Authorization: Bearer ${DEVOPS_HUB_TOKEN}" \31 -H "Content-Type: application/json" \32 -d '{"agent_health": ${groovy.json.JsonOutput.toJson(healthReport)}}'33 """34 }35 }36 }37 }38}2. Resource utilization monitoring#
1# Agent monitoring script (deploy to each agent)2#!/bin/bash3# /usr/local/bin/monitor-agent.sh45AGENT_NAME=$(hostname)6JENKINS_CONTROLLER="http://jenkins:8080"78while true; do9 # Collect metrics10 CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)11 MEMORY_USAGE=$(free | grep Mem | awk '{printf "%.2f", $3/$2 * 100.0}')12 DISK_USAGE=$(df -h / | awk 'NR==2{print $5}' | cut -d'%' -f1)1314 # Send to Jenkins or monitoring system15 curl -X POST "$JENKINS_CONTROLLER/agent-metrics" \16 -H "Content-Type: application/json" \17 -d "{18 \"agent\": \"$AGENT_NAME\",19 \"cpu_usage\": $CPU_USAGE,20 \"memory_usage\": $MEMORY_USAGE,21 \"disk_usage\": $DISK_USAGE,22 \"timestamp\": $(date +%s)23 }"2425 sleep 6026doneBlue Ocean UI integration#
Enhance pipeline visualization with Blue Ocean:
1. Install Blue Ocean#
- Manage Jenkins > Manage Plugins
- Install Blue Ocean plugin suite
- Restart Jenkins
2. Configure Blue Ocean pipeline#
1// Jenkinsfile optimized for Blue Ocean2pipeline {3 agent none45 stages {6 stage('Prepare') {7 agent { label 'devops-hub' }8 steps {9 echo "🚀 Starting DevOps Hub build pipeline"10 script {11 currentBuild.displayName = "#${BUILD_NUMBER} - ${env.BRANCH_NAME}"12 }13 }14 }1516 stage('Parallel Build & Test') {17 parallel {18 stage('🔨 Build Application') {19 agent { label 'build-pool' }20 steps {21 checkout scm22 sh 'npm ci'23 sh 'npm run build'24 archiveArtifacts artifacts: 'dist/**'25 }26 }2728 stage('🧪 Unit Tests') {29 agent { label 'test-pool' }30 steps {31 checkout scm32 sh 'npm ci'33 sh 'npm run test:unit'34 publishTestResults testResultsPattern: 'test-results.xml'35 }36 }3738 stage('🐳 Docker Build') {39 agent { label 'docker' }40 steps {41 checkout scm42 sh 'docker build -t app:${BUILD_NUMBER} .'43 }44 }45 }46 }4748 stage('🚀 Deploy to Staging') {49 agent { label 'deploy-pool' }50 when {51 anyOf {52 branch 'main'53 branch 'develop'54 }55 }56 steps {57 echo "Deploying to staging environment"58 sh 'kubectl apply -f k8s/staging/'59 }60 }61 }6263 post {64 always {65 script {66 if (env.BRANCH_NAME == 'main') {67 // Send deployment notification68 sh """69 curl -X POST "${DEVOPS_HUB_URL}/projects/${PROJECT_ID}/deployments" \70 -H "Authorization: Bearer ${DEVOPS_HUB_TOKEN}" \71 -H "Content-Type: application/json" \72 -d '{73 "environment": "staging",74 "status": "${currentBuild.result ?: 'SUCCESS'}",75 "build_number": "${BUILD_NUMBER}",76 "commit_sha": "${env.GIT_COMMIT}"77 }'78 """79 }80 }81 }82 }83}High availability setup#
For production environments, implement agent redundancy:
1. Agent failover configuration#
1// Multi-agent deployment strategy2def deployToAgents(agents, task) {3 def deployments = [:]45 agents.each { agent ->6 deployments["Deploy to ${agent}"] = {7 node(agent) {8 try {9 task()10 } catch (Exception e) {11 echo "Deployment failed on ${agent}: ${e.message}"12 // Continue with other agents13 }14 }15 }16 }1718 parallel deployments19}2021// Usage in pipeline22stage('Resilient Deployment') {23 steps {24 script {25 def productionAgents = ['prod-agent-1', 'prod-agent-2', 'prod-agent-3']2627 deployToAgents(productionAgents) {28 sh 'kubectl apply -f k8s/production/'29 sh 'kubectl rollout status deployment/app'30 }31 }32 }33}2. Load balancer configuration#
1# nginx.conf for Jenkins controller load balancing2upstream jenkins_controllers {3 server jenkins-master-1:8080 max_fails=3 fail_timeout=30s;4 server jenkins-master-2:8080 max_fails=3 fail_timeout=30s backup;5}67server {8 listen 80;9 server_name jenkins.assistance.bg;1011 location / {12 proxy_pass http://jenkins_controllers;13 proxy_set_header Host $host;14 proxy_set_header X-Real-IP $remote_addr;15 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;16 proxy_set_header X-Forwarded-Proto $scheme;17 }18}This comprehensive Jenkins Agents setup provides a robust foundation for distributed CI/CD with DevOps Hub integration. The configuration supports multiple agent types, dynamic provisioning, cross-platform builds, and production-grade monitoring and high availability.