Welcome, tech enthusiasts! If you're here, you're about to discover something new and insightful about Jenkins pipelines.
Pipeline with when conditions:
pipeline {
agent any
stages {
stage("Test") {
when {
equals(actual: currentBuild.number, expected: 5)
}
steps {
echo "Hello World!"
}
}
}
}
In the above pipeline, the stage will gets executed when the build number is 5.
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
when {
branch 'main'
}
steps {
echo 'Building on the main branch...'
}
}
stage('Test') {
when {
anyOf {
branch 'develop'
environment name: 'RUN_TESTS', value: 'true'
}
}
steps {
echo 'Running tests on develop branch or if RUN_TESTS is true...'
}
}
stage('Deploy to Staging') {
when {
allOf {
branch 'main'
environment name: 'DEPLOY_ENV', value: 'production'
expression { env.BUILD_NUMBER.toInteger() % 2 == 0 }
}
}
steps {
echo 'Deploying to the staging environment...'
}
}
stage('Deploy to Production') {
when {
branch 'release/*'
}
steps {
echo 'Deploying to production from a release branch...'
sh './deploy.sh production'
}
}
}
}
In the above pipeline, i have used multiple conditions. Lets decode it
Build Stage:
when: This will execute when the condition matches.
Executes only if the branch is
main
.Commonly used when you want to limit build steps to production-ready branches.
Test Stage:
anyOf: This will execute when any of the value will match with condition.
Runs tests on the
develop
branch or when theRUN_TESTS
environment variable is explicitly set totrue
.Useful for scenarios where you need testing only in development or in specific pipelines.
Deploy Stage:
allOf: This will execute when all of the values will match with condition.
Executes if the branch is
main
and theDEPLOY_ENV
environment variable isstaging
.Ensures that deployment to staging occurs only under controlled conditions.
Deploy to Production:
Runs only for branches that follow the
release/*
naming convention.Helps enforce production deployments from specific release branches.
Notify Stage:
Triggers if
README
was modified in the latest changeset.Practical for notifying stakeholders about updates to key documentation.
Pipeline with post-build actions:
Post-build actions in Jenkins are tasks or steps that are executed after the pipeline has finished running. These actions can handle success, failure, or always-run scenarios and are crucial for cleanup, notifications, or additional workflows triggered by the pipeline's outcome.
Common Post-Build Actions
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
post {
always {
echo "This block always runs."
}
changed {
echo "This block runs when the current status is different than the previous one."
}
fixed {
echo "This block runs when the current status is success and the previous one was failed or unstable."
}
regression {
echo "This block runs when the current status is anything except success but the previous one was successful."
}
unstable {
echo "This block runs if the current status is marked unstable."
}
aborted {
echo "This block runs when the build process is aborted."
}
failure {
echo "This block runs when the build is failed."
}
success {
echo "This block runs when the build is succeeded."
}
unsuccessful {
echo "This block runs when the current status is anything except success."
}
}
}
Pipeline with cron syntax:
pipeline {
agent any
triggers {
cron "* * * * *"
}
stages {
stage("Test") {
steps {
echo "Hello World!"
}
}
}
}
The above pipeline triggers for every minute .
Pipeline with input functions:
pipeline {
agent any
stages {
stage('Code') {
steps {
echo 'This is code stage'
}
}
stage('Build') {
steps {
echo 'This is build stage'
}
}
stage('Test') {
steps {
echo 'This is test stage'
}
}
stage('deploy') {
input {
message "can i deploy"
ok "yes you can"
}
steps {
echo 'our code is deployed'
}
}
}
}
In the above pipeline, the stages like code, build & test will execute automatically, but when it comes to deploy it will ask the input to continue or abort.
Pipeline with parallel stages:
pipeline {
agent any
stages {
stage('Parallel Stages') {
parallel {
stage('Stage A') {
steps {
echo 'Running Stage A...'
sh 'sleep 5'
}
}
stage('Stage B') {
steps {
echo 'Running Stage B...'
sh 'sleep 5'
}
}
}
}
}
}
Parallel stages are used to build multiple stages at a time.
In the above pipeline, Stage-A & Stage-B will gets executed at a time.
Pipeline with parameters:
Parameters are used to pass the values while we build the jenkins pipelines. We have different parameters like string, boolean, choices, credentials, file, run
Example for string parameter:
pipeline {
agent any
parameters {
string (name: "aws", defaultValue: "EC2", description: "i am unsing aws cloud")
}
stages {
stage ("stage-1") {
steps {
echo "hai i am using parameters"
}
}
}
}
Example for boolean parameter:
pipeline {
agent any
parameters {
booleanParam (name: "jenkins", defaultValue: true, description: "")
}
stages {
stage('Hello') {
steps {
echo ' i am using boolean parameter'
}
}
}
}
Example for choice parameter:
pipeline {
agent any
parameters {
choice (name: "branch", choices : ["one", "two", "three", "four"], description: "this is cp")
}
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
Hope you guys like my articles and this will help you for your DevOps learning journey.
If you love stories that inspire learning, growth, and productivity, consider subscribing for more! If this article added value to your journey, your support would mean the world to me — only if it’s within your means. Let’s stay connected on LinkedIn too. Thank you for reading 💕