如何在Jenkins声明性管道中使用shell脚本if-else条件块

如何在Jenkins声明性管道中使用shell脚本if-else条件块,shell,jenkins,jenkins-pipeline,Shell,Jenkins,Jenkins Pipeline,如何在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 be

如何在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'
            }
        }
    }
}

如能迅速回复,将不胜感激