Home > Blockchain >  Jenkins: triggeredBy 'UpstreamCause' always evaluates to false in When condition
Jenkins: triggeredBy 'UpstreamCause' always evaluates to false in When condition

Time:11-02

In Jenkins I have an Upstream Project A that triggeres a Downstream Project B.

Project A:

pipeline {
  agent any
  stages {
    stage('Hello') {
      steps {
        echo "Some message"
        build(job: 'B', wait: false)
      }
    }
  }
}

Project B

pipeline {
  agent none
  tools {
    maven "maven"
  }
 stages {
  stage('Triggered By Upstream') {
    when {
      triggeredBy "UpstreamCause"
    }
    steps {
      echo "Triggered by Upstream project"
    }
  }
 }
}

Here project A successfully triggers the project B. But the stage in project B will be skipped due to the condition in when being False. It seems like a bug to me. Does anyone know what's wrong in this code?

CodePudding user response:

Apparently there is no cause as UpstreamCause, but rather BuildUpstreamCause, which evaluates to true when the pipeline is triggered from an upstream project. So the when clause in project B should be written as:

when {
  triggeredBy "BuildUpstreamCause"
}

in order for the stage to be run when triggered from an upstream project.

  • Related