🤖

Jenkins Pipeline Cheat Sheet

Declarative pipeline snippets — build, test, Docker, and K8s deploy.

Related: jenkins · docker · kubernetes

Pipeline skeleton

CommandDescription
pipeline { agent any stages { stage('Build') { steps { sh 'make build' } } stage('Test') { steps { sh 'make test' } } } post { failure { echo 'Failed!' } } }Basic declarative pipeline
options { timeout(time: 30, unit: 'MINUTES') disableConcurrentBuilds() }Timeout and concurrency

Docker agent

CommandDescription
agent { docker { image 'node:20-alpine' args '-v /var/run/docker.sock:/var/run/docker.sock' } }Run steps in Docker
stage('Push') { steps { sh 'docker build -t myreg/app:$BUILD_NUMBER .' sh 'docker push myreg/app:$BUILD_NUMBER' } }Build and push image

Deploy to K8s

CommandDescription
withKubeConfig([credentialsId: 'kubeconfig-prod']) { sh 'kubectl apply -f k8s/' sh 'kubectl rollout status deploy/myapp -n apps' }kubectl with stored kubeconfig
kubernetes { yaml ''' apiVersion: v1 kind: Pod spec: containers: - name: kubectl image: bitnami/kubectl command: ['kubectl', 'get', 'pods'] ''' }Dynamic pod agent in cluster