Search code examples
jenkinsmultibranch-pipeline

Jenkins when with Build Trigger


I have a multibranch job in my jenkins, what I have a webhook setup from my github to my jenkins that send every pull request changes and issue comments.

What I'm trying to do is let github send the pull request changes for indexing purposes, but don't run the job, unless the developer add comment 'test' in the comment on the github pull request.

This is my 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'
      }
    }
  }
}

So I want if the trigger isn't GitHub pull request comment, don't run anything. I've tried this but it doesn't work. I've tried print currentBuild.buildCauses.shortDescription variable and it prints [GitHub pull request comment], but the job still won't run with my when expression

How can I do this? Thanks


Solution

  • So actually the problem is because currentBuild.buildCauses.shortDescription return ArrayList instead of plain String.

    I didn't really think this was meant an array [GitHub pull request comment], so I manage to fix the issue with just array index.

    currentBuild.buildCauses.shortDescription[0]
    

    This return the correct build trigger GitHub pull request comment. So for anyone who also stumbles to this issue, this is how I fixed it

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