Jenkins pipeline
5 min read

In this document, I am sharing some of my learnings with Jenkins Pipeline.
What is jenkins pipeline : -
Form my understanding jenkins pipeline is way of making all thing automatic .
Formal defination is : - Jenkins Pipeline (or simply "Pipeline" with a capital "P") is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins.
to understand pipeline i watch some youtube video , blogs and used chatGPT as teacher. so through out learning i made Jenkina server on EC2 and my VM , i created diffrend job , i shere that in below.
Key Components of jenkins Pipeline :-
Pipeline
The root block.
Defines the entire CI/CD workflow.
pipeline {
...
}
Agent
Defines where the pipeline (or a stage) should run.
Examples:
agent any→ Run on any available Jenkins node.agent none→ Disable default agent (stages define their own agents).agent { label 'linux' }→ Run on a specific node with labellinux.
Stages
Logical sections of the pipeline (e.g., Build, Test, Deploy).
Used for organizing and visualizing in Jenkins UI.
stages {
stage('Build') { ... }
stage('Test') { ... }
}
Steps
The actual commands/tasks inside a stage.
Examples:
sh 'mvn clean install'(Linux shell command)bat 'gradlew.bat build'(Windows batch command)echo "Hello World"
Post
Defines actions to run at the end of the pipeline or stage.
Useful for cleanup, notifications, archiving logs, etc.
post {
success {
echo 'Pipeline succeeded!'
}
failure {
echo 'Pipeline failed!'
}
}
Environment
- Used to set global or stage-specific environment variables.
environment {
JAVA_HOME = "/usr/lib/jvm/java-11"
APP_ENV = "staging"
}
Parameters
- Allows pipelines to take user inputs (useful for different environments or versions).
parameters {
string(name: 'BRANCH', defaultValue: 'main', description: 'Git branch to build')
choice(name: 'ENV', choices: ['DEV', 'STAGING', 'PROD'], description: 'Environment')
}
Triggers
- Defines what automatically starts the pipeline.
triggers {
cron('H/15 * * * *') // Runs every 15 minutes
pollSCM('* * * * *') // Polls SCM every minute
}
Options
- Configures pipeline-level settings (like timeouts, retries, etc.).
options {
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '10'))
}
Input
- Pause the pipeline and wait for manual approval or input.
input {
message "Deploy to production?"
ok "Yes, Deploy"
}
Above all are components of a pipeline, and I used some of them in my Jenkins pipeline job.
All job i created on my VM

In the Job - Parameter and pipeline
Agent any
- The pipeline can run on any available Jenkins agent (no restriction to a specific node).
Parameters Section
This lets you pass inputs when starting the job.
Parameters defined:
ENVIRONMENT (choice parameter) → User selects DEVELOPMENT / STAGING / PRODUCTION.
APIKEY (password parameter) → Securely input API key (masked in logs).
CHANGELOG (string parameter) → Free text input (default:
"This is the change log.").
✅ This makes your pipeline dynamic & reusable depending on user input.
Stages
Stage: Test
echo "This step tests the project"→ Just prints a test message (in real life, you’d run unit tests, integration tests, etc.).
Stage: Deploy
when { expression { params.ENVIRONMENT == "PRODUCTION" } }→ This stage only runs if ENVIRONMENT = PRODUCTION.
→ If you selected DEVELOPMENT or STAGING, this stage will be skipped.✅ This adds conditional execution → safer deployment control.
Stage: Report
sh "echo '${params.CHANGELOG}' > ${params.ENVIRONMENT}_report.txt" archiveArtifacts artifacts: '*.txt', fingerprint: true→ Creates a text report file named like:
DEVELOPMENT_report.txtSTAGING_report.txtPRODUCTION_report.txt
→ The file contains the CHANGELOG message entered as a parameter.
→archiveArtifactsstores that file in Jenkins so it can be downloaded later.
This is how you’re generating & archiving artifacts.
Here is the output -

Next i careted job - SSH Agent
Agent Selection
agent { label 'dev' }The pipeline runs on a Jenkins agent/node that has the label
dev.Ensures that builds only run on your chosen machine/environment.
Tools Section
tools { maven 'maven-3.9.11' }Tells Jenkins to use the configured Maven installation (v3.9.11).
This means all
mvncommands in your pipeline will use that Maven.
Stage: Source
sh 'mvn --version' sh 'git --version' git branch: 'main', url: 'https://github.com/LinkedInLearning/essential-jenkins-2468076.git'Verifies Maven and Git are installed on the agent.
Pulls code from the GitHub repository (
mainbranch).
✅ This is your source checkout stage.
Stage: Clean
dir("${env.WORKSPACE}/Ch04/04_02-ssh-agent") { sh 'mvn clean' }Moves into the project’s subdirectory.
Runs
mvn clean→ deletestarget/folder, removing old compiled classes and artifacts.
✅ Ensures a fresh build.
Stage: Test
dir("${env.WORKSPACE}/Ch04/04_02-ssh-agent") { sh 'mvn test -Dmaven.compiler.source=17 -Dmaven.compiler.target=17' }Runs unit tests for the project.
Explicitly sets Java compiler level to 17.
✅ Validates your code with automated tests.
Stage: Package
dir("${env.WORKSPACE}/Ch04/04_02-ssh-agent") { sh 'mvn package -DskipTests -Dmaven.compiler.source=17 -Dmaven.compiler.target=17' }Runs
mvn packageto create a JAR/WAR file.Skips tests in this stage (since you already ran them in the "Test" stage).
Output is stored in
target/folder.
✅ Produces the build artifact.
Here is the Output -


Here i am Using Github socrce repo - https://github.com/DheerenGaud/Jenkins-learning
in the next Docs i will cover my EC2 intsace job also