如何在Jenkins声明性流水线中使用shell脚本的if-else条件块
下面是我想要在Jenkins流水线阶段运行的shell脚本命令,以及我们使用shell脚本/命令行运行它们的方法
#!/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"
这些步骤在Linux和Jenkins shell/命令提示符中正常工作。如何在Jenkins管道内复制相同的步骤?
我尝试过以下方法,但失败了。
# 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'
}
}
}
}
希望您能尽快回复。