首页 > 解决方案 > Jenkins 使用构建触发器时

问题描述

我在我的 jenkins 中有一个多分支的工作,我有一个从我的 github 到我的 jenkins 的 webhook 设置,用于发送每个拉取请求更改并发布评论。

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

这是我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,不要运行任何东西。我已经尝试过了,但它不起作用。我已经尝试了 printcurrentBuild.buildCauses.shortDescription变量并且它可以打印[GitHub pull request comment],但是我的作业仍然无法运行when expression

我怎样才能做到这一点?谢谢

标签: jenkinsmultibranch-pipeline

解决方案


所以实际上问题是因为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'
      }
    }
  }
}

推荐阅读