how to use shell script if else condition block in Jenkins declarative pipeline
below are the shell script commands I wanted to run in a Jenkins pipeline stage, the way we run them using shell script/command line
#!/bin/sh
var=$(git status -s)
echo ${#var}
if [ ${#var} -eq 0 ]
then
echo "tree is clean"
else
echo "tree is dirty, please commit changes before running this"
git add -A
git commit -m "updated terraform files"
git push
fi
echo "End of script"
These steps are working fine inside Linux and Jenkins shell/command prompt. How do I replicate same inside jenkins pipeline?
This is what I have tried but it's failing
# jenkins pipeline
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'node --version'
sh 'pwd'
sh 'sleep 15'
script {
sh 'pwd'
sh 'var=$(git status -s)'
sh 'echo ${#var}' # till here it's working, but if block it's failing. I guess perhaps because if block is actually written in groovy syntax not linux/shell script 'if else' block
if ( ${#var} -eq 0 ) {
sh 'echo tree is clean'
}else {
sh 'echo tree is dirty please commit your code changes'
}
}
sh 'sleep 5'
}
}
}
}
Quick response will be appreciated.