Jenkins

From G1Joshi/Agent-Skills · View interactive version

Jenkins is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Jenkins automation server assistance covering declarative Jenkinsfiles, shared libraries, multi-branch pipelines, and plugins. Use when managing enterprise CI/CD build automation.

How to use this skill

Skill file

Jenkins

Jenkins is an extensible, open-source automation server supporting pipeline-as-code (Jenkinsfile), distributed agent execution on Kubernetes, and enterprise CI/CD workflows.

When to Use

Quick Start

// Jenkinsfile
pipeline {
    agent { docker { image 'node:20' } }
    stages {
        stage('Build') {
            steps {
                sh 'npm ci'
                sh 'npm run build'
            }
        }
    }
}

Core Concepts

Declarative Jenkinsfile with Docker Agents & Parallel Stages

Modern pipeline structure running in parallel stages:

// Jenkinsfile
pipeline {
    agent {
        docker {
            image 'node:22-alpine'
            args '-u root:root'
        }
    }
    options {
        timeout(time: 1, unit: 'HOURS')
        disableConcurrentBuilds()
        ansiColor('xterm')
    }
    environment {
        CI = 'true'
        NPM_CONFIG_CACHE = "${WORKSPACE}/.npm"
    }
    stages {
        stage('Install') {
            steps {
                sh 'npm ci'
            }
        }
        stage('Quality Gates') {
            parallel {
                stage('Lint & Typecheck') {
                    steps {
                        sh 'npm run lint'
                        sh 'npm run typecheck'
                    }
                }
                stage('Unit Tests') {
                    steps {
                        sh 'npm test -- --coverage'
                    }
                    post {
                        always {
                            junit 'junit.xml'
                        }
                    }
                }
            }
        }
        stage('Deploy to Staging') {
            when {
                branch 'main'
            }
            steps {
                withCredentials([string(credentialsId: 'STAGING_API_KEY', variable: 'API_KEY')]) {
                    sh 'npm run deploy:staging'
                }
            }
        }
    }
    post {
        failure {
            slackSend(channel: '#ci-alerts', color: 'danger', message: "Pipeline Failed: ${env.JOB_NAME} #${env.BUILD_NUMBER}")
        }
    }
}

Kubernetes Dynamic Cloud Agents

Spawning ephemeral build pods dynamically in Kubernetes:

pipeline {
    agent {
        kubernetes {
            yaml '''
apiVersion: v1
kind: Pod
metadata:
  labels:
    some-label: build-agent
spec:
  containers:
  - name: maven
    image: maven:3.9-eclipse-temurin-21
    command: ['cat']
    tty: true
'''
        }
    }
    stages {
        stage('Build') {
            steps {
                container('maven') {
                    sh 'mvn clean package -DskipTests'
                }
            }
        }
    }
}

Jenkins Configuration as Code (JCasC)

Managing Jenkins master controller configuration declaratively:

# jenkins.yaml
jenkins:
  systemMessage: "Enterprise Production CI/CD Controller - Managed via JCasC"
  numExecutors: 0 # Master runs zero builds; all work on agents
  mode: EXCLUSIVE
security:
  queueItemAuthenticator:
    authenticators:
      - global:
          strategy: triggeringUsersAuthorizationStrategy

Common Patterns

Declarative Pipeline with Docker Agent and Post-Build Notifications

Problem: Build environment drift across physical Jenkins worker nodes.

Solution:

Use isolated container execution in declarative Jenkinsfile:

pipeline {
    agent {
        docker {
            image 'node:20-alpine'
            args '-u root'
        }
    }
    stages {
        stage('Install & Test') {
            steps {
                sh 'npm ci'
                sh 'npm test'
            }
        }
    }
    post {
        failure {
            slackSend channel: '#ci-alerts', message: "Job ${env.JOB_NAME} failed!"
        }
    }
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :-------------------------------------------- | :------------------------------------------------------------- | :-------------------------------------------------------------------------- |

| java.lang.OutOfMemoryError: Java heap space | Master/controller JVM heap limit exhausted by build history. | Increase -Xmx parameter in Jenkins JVM options and discard old builds. |

| Scripts not permitted to use method | Groovy script attempting restricted method call under sandbox. | Navigate to Manage Jenkins > In-process Script Approval and approve method. |

| Cannot connect to Docker daemon in agent | Docker socket not mounted into the executor container. | Mount socket: args '-v /var/run/docker.sock:/var/run/docker.sock'. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.