All technological notes.
Jenkins Pipeline
Jenkinsfile
Domain-Specific Language (DSL) based on the Groovy programming language.pipeline → top-level definitionagent → where it runs (node, container, k8s pod)stages → logical phasessteps → actual commands
Declarative Pipeline when possibleJenkinsfile in SCMshared libraries for common codeNode:
script{ node{}}node('linux') { ... }Agent:
agent { label 'linux' }node {
checkout scm
sh 'mvn test'
}
// choose a specific agent by label
node('linux && docker') {
sh 'docker build -t myapp .'
}
// using in declarative pipeline
pipeline {
agent none
stages {
stage('Build') {
steps {
script {
node('build-node') {
echo 'Building on build-node...'
}
}
}
}
}
}
pipeline {
agent any
}
pipeline {
agent any
tools {
// Define the tools and their configurations here
maven 'MavenTool' // Name of the tool and the tool installation name
jdk 'JDKTool' // Name of the tool and the tool installation name
}
stages {
// Define your pipeline stages here
stage('Build') {
steps {
// Use the configured tools in your pipeline stages
script {
sh '''#!/bin/bash
echo "Building with Maven"
mvn clean package
'''
}
}
}
}
}
buildDiscarder(logRotator(numToKeepStr: '30', artifactNumToKeepStr: '10')):
disableConcurrentBuilds:
retry:
timeout:
timestamps:
skipStagesAfterUnstable:
Upstream projects trigger downstream projects

Throttle Concurrent Builds
withCredentials// scope secrets as narrowly as possible
stage('Call API') {
steps {
withCredentials([string(credentialsId: 'my-api-token', variable: 'API_TOKEN')]) {
sh '''
set +x
curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com
'''
}
}
}
Milestones
pipeline {
agent any
stages {
stage('Deploy') {
steps {
// Ensure no older build deploys after this point
milestone 1
echo "1111"
milestone 2
echo "2222"
}
}
}
}
script block
pipeline {
agent any
stages {
stage('demo') {
steps {
// Standard declarative step
echo 'Starting...'
script {
// Scripted Groovy logic begins here
def browser = 'chrome'
if (browser == 'chrome') {
echo 'Running tests in Chrome'
} else {
echo 'Running tests in default browser'
}
}
}
}
}
}
timeout(time: 20, unit: 'MINUTES') {}:
retry(3) {}:
try {} catch (err) {throw err} finally {}:
when {branch 'main'} steps {}:
input { message 'Deploy to production?' ok 'Deploy'} steps {}
post {success {} failure {} always {} }:
pipeline {
agent any
options {
parallelsAlwaysFailFast() // fail-fast behavior
}
stages {
stage('Test') {
parallel {
stage('A') {
steps { sh './test-a.sh' }
}
stage('B') {
steps { sh './test-b.sh' }
}
}
}
}
}
Shared Library
Groovy code that can be shared across multiple Jenkins Pipelines.
@Library('my-shared-library') // Reference the Shared Library
// Import custom steps
import com.example.CustomPipelineSteps
pipeline {
agent any
stages {
stage("Build") {
steps {
script {
CustomPipelineSteps.build() // Use a custom step from the Shared Library
}
}
}
stage('Test') {
steps {
script {
CustomPipelineSteps.test() // Another custom step
}
}
}
}
}
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
docker.build("my-app:${env.BUILD_ID}")
}
}
}
stage('Push') {
steps {
script {
docker.withRegistry('https://www.docker.com/', 'docker-hub-credentials') {
docker.image("my-app:${env.BUILD_ID}").push()
}
}
}
}
}
}
pipeline {
agent any
options {
disableConcurrentBuilds()
timeout(time: 30, unit: 'MINUTES')
}
stages {
stage('Plan') {
steps {
retry(2) {
sh 'terraform plan'
}
}
}
stage('Approval') {
input {
message 'Apply infrastructure changes?'
ok 'Apply'
}
steps {
echo 'Approved'
}
}
stage('Apply') {
steps {
sh 'terraform apply -auto-approve'
}
}
}
post {
success {
echo 'Success!'
}
failure {
echo 'Failed!'
}
}
}
pipeline {
agent any
options {
parallelsAlwaysFailFast()
}
stages {
stage('Checks') {
parallel {
stage('Lint') {
steps { sh 'npm run lint' }
}
stage('Unit Test') {
steps { sh 'npm test' }
}
stage('Security Scan') {
steps { sh './scan.sh' }
}
}
}
}
}
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Build ...'
sh """
echo 'multiple steps:'
pwd
date
hostname
"""
}
}
stage('Test') {
steps {
echo 'Testing ...'
}
}
stage('Deploy') {
steps {
echo 'Deploy ...'
}
}
}
post {
success {
echo 'Success!'
}
failure {
echo 'Failed!'
}
}
}
pipeline {
agent any
stages {
stage('Retry') {
steps {
retry(3){
sh 'deliberate error'
}
}
}
}
}

pipeline {
agent any
stages {
stage('Deploy') {
steps {
timeout(time: 3, unit: "SECONDS"){
sh 'sleep 5'
}
}
}
}
}

pipeline {
agent any
stages {
stage('Parallel Tests') {
parallel {
stage('Unit Tests') {
steps { echo "Running unit tests..." }
}
stage('Integration Tests') {
steps { echo "Running integration tests..." }
}
}
}
}
}

