首页 > 解决方案 > 詹金斯`如果(sh'...')`

问题描述

如何让詹金斯sh在条件内运行命令?如果它拉取以“WIP”开头的 git 提交,我希望它中止执行。

我的设想是:

stage('Build') {
  steps {
    // some steps ...
    script {
      if (sh "git log -1 --pretty='format:%B' | grep -q ^WIP") {
        currentBuild.result = 'ABORTED'
        return
      }
    }
    // more steps ...
  }
}

...但我的管道抱怨“期待')',发现'git log -1 --pretty ='格式:%B'| grep -q ^WIP'”

标签: jenkinsgroovyjenkins-groovy

解决方案


似乎是 Groovy 解析器的问题。只需将参数括在括号中:

if (sh("git log -1 --pretty='format:%B' | grep -q ^WIP"))

推荐阅读