詹金斯(Jenkins)使用构建触发器时

时间:2019-08-03 09:11:49

标签: jenkins multibranch-pipeline

我的詹金斯有一个多分支工作,我有一个从github到我的詹金斯的webhook设置,可以发送每个请求请求更改并发表评论。

我要做的是让github发送拉取请求更改以用于建立索引,但不要运行该作业,除非开发人员在github拉取请求的注释中添加注释“ test”。

这是我的Jenkinsfile

pipeline {
  agent { label 'mac' }
  stages {
    stage ('Check Build Cause') {
      steps {
        script {
           def cause = currentBuild.buildCauses.shortDescription
           echo "${cause}"
        }
      }
    }
    stage ('Test') {
      when {
        expression {
          currentBuild.buildCauses.shortDescription == "[GitHub pull request comment]"
        }
      }
      steps {
        sh 'bundle exec fastlane test'
      }
    }
  }
}

因此,我希望如果触发器不是GitHub pull request comment,则不要运行任何命令。我已经尝试过了,但是没有用。我尝试打印currentBuild.buildCauses.shortDescription变量,但它打印[GitHub pull request comment],但是该作业仍无法与我的when expression

一起运行

我该怎么做?谢谢

1 个答案:

答案 0 :(得分:0)

实际上,问题是因为currentBuild.buildCauses.shortDescription返回ArrayList而不是纯字符串。

我并不是真的以为这是一个数组[GitHub pull request comment],所以我只用数组索引来解决这个问题。

currentBuild.buildCauses.shortDescription[0]

这将返回正确的构建触发器GitHub pull request comment。因此,对于也偶然发现此问题的任何人,这就是我解决的方法

pipeline {
  agent { label 'mac' }
  stages {
    stage ('Test') {
      when {
        expression {
          currentBuild.buildCauses.shortDescription[0] == "GitHub pull request comment"
        }
      }
      steps {
        sh 'bundle exec fastlane test'
      }
    }
  }
}