如果 Jenkins 声明性管道中的其他条件块,如何使用 shell 脚本

时间:2021-05-11 11:08:30

标签: 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 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'
            }
        }
    }
}

快速响应将不胜感激。

0 个答案:

没有答案
相关问题