Jenkins Pipelines samples

1. Agent Configuration Examples

1.1 Agent: Any (Free Executor/Slave)

Concept: Runs on any available agent/executor in Jenkins

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Running on any available agent'
                sh 'mkdir -p abc'
                sh 'ls -lart'
                sh 'pwd'
            }
        }  
    }
}

1.2 Agent: None (Schedule Stages on Different Agents)

Concept: No global agent. Each stage specifies its own agent for flexibility


1.3 Agent: Label (Schedule on Specific Agent)

Concept: Run on a specific agent labeled 'slave01'


1.4 Agent: Docker (Container-Based Execution)

Concept: Creates a Docker container and runs pipeline steps inside it


2. Environment Variables & Variables in Pipelines

2.1 Basic Environment Variables

Concept: Define pipeline-level environment variables


2.2 Environment Variables with Shell Variable Expansion

Concept: Use double quotes for variable substitution in shell commands


3. Parameters in Pipelines

3.1 Pipeline Parameters with String Input

Concept: Accept user input as parameters during build trigger


4. Pipeline Options

4.1 Options: Retry, Build Discarder, Timeout, Timestamps

Concept: Control pipeline behavior with global options


4.2 Stage-Level Retry Option

Concept: Retry specific stage steps on failure


5. Parallel Stages

5.1 Running Multiple Stages in Parallel

Concept: Execute stages concurrently to reduce build time


6. Try-Catch Error Handling

6.1 Exception Handling with Try-Catch

Concept: Handle errors gracefully using try-catch blocks


7. Directory Navigation with dir()

7.1 Running Steps in Specific Directory

Concept: Execute commands from a specific folder context


8. Triggers

8.1 Cron Trigger (Scheduled)

Concept: Trigger pipeline at specified time intervals


8.2 Poll SCM Trigger

Concept: Periodically check SCM for changes and trigger if found


9. Post Section - Pipeline Notifications

9.1 Post Actions: Always, Success, Failure

Concept: Execute actions based on pipeline execution status


9.2 Stage-Level Post Actions

Concept: Add post actions to individual stages


10. Conditional Execution with When

10.1 When: Single Expression

Concept: Execute stage only if condition is met


10.2 When: anyOf (OR condition)

Concept: Execute if ANY condition is true


10.3 When: allOf (AND condition)

Concept: Execute if ALL conditions are true


10.4 When with Parameters

Concept: Use parameters in when conditions


11. Git & Build Integration

11.1 Git Checkout with Credentials

Concept: Clone repository with authentication (PAT/SSH)


11.2 Complete Maven Build Pipeline

Concept: Full CI pipeline with Git, Build, Test, and Package stages


Last updated