🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Jenkins Tutorials: Scripted Pipeline jenkinsfile example


Pipeline: Basic Steps

  • catchError: Catch error and set build result to failure
  • deleteDir: Recursively delete the current directory from the workspace
  • dir: Change current directory
  • echo: Print Message
  • error: Error signal
  • fileExists: Verify if file exists in workspace
  • isUnix: Checks if running on a Unix-like node
  • mail: Mail
  • pwd: Determine current directory
  • readFile: Read file from workspace
  • retry: Retry the body up to N times
  • sleep: Sleep
  • stash: Stash some files to be used later in the build
  • step: General Build Step
  • timeout: Enforce time limit
  • tool: Use a tool from a predefined Tool Installation
  • unstable: Set stage result to unstable
  • unstash: Restore files previously stashed
  • waitUntil: Wait for condition
  • warnError: Catch error and set build and stage result to unstable
  • withEnv: Set environment variables
  • wrap: General Build Wrapper
  • writeFile: Write file to workspace
  • archive: Archive artifacts
  • getContext: Get contextual object from internal APIs
  • unarchive: Copy archived artifacts into the workspace
  • withContext: Use contextual object from internal APIs within a block

How to apply conditions aka Flow Control in jenkins scripted pipeline?
========================================================
node {
stage('Example') {
if (env.BRANCH_NAME == 'master') {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
}
}
}
stage('UT') {
// Run the maven build
if (isUnix()) {
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean test"
} else {
bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean test/)
}
}
view raw condition hosted with ❤ by GitHub
decalartive Pipeline
===================================
pipeline {
agent { docker { image 'maven:3.3.3' } }
stages {
stage('build') {
steps {
sh 'mvn --version'
}
}
}
}
Scripted Pipeline
node {
stage('Build')
{
sh 'echo rajesh'
}
}
node {
stage('Build')
{
sh "echo Build"
}
stage('Deploy')
{
sh "echo Deploy"
}
stage('Test')
{
sh "echo Test"
}
}
node {
stage('Build')
{
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven'
sh "echo Build"
}
stage('Deploy')
{
sh "echo Deploy"
}
stage('Test')
{
sh "echo Test"
}
}
# This code would run in a node called 'linux'
node('linux') {
stage('Build')
{
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven'
sh "echo Build"
}
stage('Deploy')
{
sh "echo Deploy"
}
stage('Test')
{
sh "echo Test"
}
}
node {
def mvnHome
stage('Build')
{
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven'
sh "echo Build"
mvnHome = tool 'rajesh-maven'
sh "'${mvnHome}/bin/mvn' clean compile"
}
stage('Deploy')
{
sh "echo Deploy"
}
stage('Test')
{
sh "echo Test"
sh "'${mvnHome}/bin/mvn' clean test"
}
}
node {
def mvnHome
stage('Build')
{
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven'
sh "echo Build"
mvnHome = tool 'rajesh-maven'
sh "'${mvnHome}/bin/mvn' clean compile"
}
stage('Deploy')
{
sh "echo Deploy"
}
stage('Test')
{
sh "echo Test"
sh "'${mvnHome}/bin/mvn' clean test"
junit '**/target/surefire-reports/TEST-*.xml'
}
}
node {
def mvnHome
stage('Build')
{
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven'
sh "echo Build"
mvnHome = tool 'rajesh-maven'
sh "'${mvnHome}/bin/mvn' clean compile"
}
stage('Test')
{
sh "echo Test"
sh "'${mvnHome}/bin/mvn' clean test"
junit '**/target/surefire-reports/TEST-*.xml'
}
stage('Deploy')
{
sh "echo Deploying..."
sh 'ansible-playbook -i inventory ansible-playbook.yaml'
}
}
node {
def mvnHome
stage('Build')
{
git 'https://github.com/devopsschool-demo-labs-projects/helloworld-java-maven'
sh "echo Build"
mvnHome = tool 'rajesh-maven'
sh "'${mvnHome}/bin/mvn' clean compile"
}
stage('Test')
{
sh "echo Test"
sh "'${mvnHome}/bin/mvn' clean test"
junit '**/target/surefire-reports/TEST-*.xml'
}
stage('Package')
{
sh "echo Package"
sh "'${mvnHome}/bin/mvn' clean package"
archive 'target/*.jar'
}
stage('Deploy')
{
sh "echo Deploying..."
sh 'ansible localhost -m setup'
}
}
abcs = ['a', 'b', 'c']
node('master') {
stage('Test 1: loop of echo statements') {
echo_all(abcs)
}
stage('Test 2: loop of sh commands') {
loop_of_sh(abcs)
}
stage('Test 3: loop with preceding SH') {
loop_with_preceding_sh(abcs)
}
stage('Test 4: traditional for loop') {
traditional_int_for_loop(abcs)
}
}
@NonCPS // has to be NonCPS or the build breaks on the call to .each
def echo_all(list) {
list.each { item ->
echo "Hello ${item}"
}
}
// outputs all items as expected
@NonCPS
def loop_of_sh(list) {
list.each { item ->
sh "echo Hello ${item}"
}
}
// outputs only the first item
@NonCPS
def loop_with_preceding_sh(list) {
sh "echo Going to echo a list"
list.each { item ->
sh "echo Hello ${item}"
}
}
// outputs only the "Going to echo a list" bit
//No NonCPS required
def traditional_int_for_loop(list) {
sh "echo Going to echo a list"
for (int i = 0; i < list.size(); i++) {
sh "echo Hello ${list[i]}"
}
}
// echoes everything as expected
How to apply try catch in jenkins scripted pipeline?
-------------------------------
node {
stage('Example') {
try {
sh 'exit 1'
}
catch (exc) {
echo 'Something failed, I should sound the klaxons!'
throw
}
}
}
view raw try.catch hosted with ❤ by GitHub
How to define var in jenkins scripted pipeline?
# init a variable
def mvnHome
# set a value of it
mvnHome = tool 'maven'
# How to use
- ${mvnHome}
- sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean compile"
view raw variable hosted with ❤ by GitHub
Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x