how to get $CAUSE in workflow

2019-01-19 10:40发布

Jenkins has a $CAUSE variable available to freestyle build jobs.

How can I access this or something similar in workflow?

My team makes use of it in email output of existing ad-hoc builds. We'd like to continue the same in new workflow based jobs.

7条回答
祖国的老花朵
2楼-- · 2019-01-19 10:57

To get cause if build was triggered by user , SCM or pull request you can use this:

def SCMTriggerCause
def UserIdCause
def GitHubPRCause
def PRCause = currentBuild.rawBuild.getCause(org.jenkinsci.plugins.github.pullrequest.GitHubPRCause)
def SCMCause = currentBuild.rawBuild.getCause(hudson.triggers.SCMTrigger$SCMTriggerCause)
def UserCause = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause)

if (PRCause) {
    println PRCause.getShortDescription()
} else if (SCMCause) {
    println SCMCause.getShortDescription()
} else if (UserCause) {
    println UserCause.getShortDescription()
}else {
   println "unknown cause"
}

Note: you have to run in a script section

credit to this github: https://github.com/benwtr/jenkins_experiment/blob/master/Jenkinsfile

查看更多
何必那么认真
3楼-- · 2019-01-19 10:58

It looks like Workflow builds don't have this variable injected. However you can retrieve the required info from currentBuild.rawBuild object using hudson.model.Run.getCause() or hudson.model.Run.getCauses() method.

Example:

Workflow script:

println "CAUSE ${currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause).properties}"

results with this output:

Running: Print Message
CAUSE [userName:John Smith, userId:jsmith, class:class hudson.model.Cause$UserIdCause, shortDescription:Started by user John Smith]

Other Cause subtypes can be found in the javadoc.

There is also a good get-build-cause example which is based on this answer in the jenkins Pipeline Examples repository.

查看更多
冷血范
4楼-- · 2019-01-19 11:03

I guess you are talking about a macro defined in the Email Ext plugin. There is ongoing work to make that plugin directly support Workflow. I am not sure about the status of this specific macro.

查看更多
小情绪 Triste *
5楼-- · 2019-01-19 11:08

I'm replying to Jazzschmidt's answer, as I just don't have enough rep... previousBuild does the wrong thing, as it gets the previously launched job of the same type, not the job that launched the current one. If that job was first launched by someone, that's who you'll get. Otherwise, the response will be NULL, which will then cause an exception trying to get its userId.

To get the "original" cause, you have to traverse the causes using UpstreamCause. This is what I ended up doing, though there may be other ways:

@NonCPS
def getCauser() {
  def build = currentBuild.rawBuild
  def upstreamCause
  while(upstreamCause = build.getCause(hudson.model.Cause$UpstreamCause)) {
    build = upstreamCause.upstreamRun
  }
  return build.getCause(hudson.model.Cause$UserIdCause).userId
}
查看更多
贪生不怕死
6楼-- · 2019-01-19 11:09

In case the build is triggered by an upstream build, you have to traverse the currentBuild hierarchy.

For example:

println getCauser(currentBuild).userId

@NonCPS
def getCauser(def build) {
    while(build.previousBuild) {
        build = build.previousBuild
    }

    return build.rawBuild.getCause(hudson.model.Cause$UserIdCause)
}

This will return the user id of the original user cause.

查看更多
一纸荒年 Trace。
7楼-- · 2019-01-19 11:14

$BUILD_CAUSE env is not available for pipelines, and in multibranch pipeline even currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause) would fail, if build was triggered by SCM change or timer. So, I implemented below workaround..

    def manualTrigger = false
    node('master'){
       def causes = currentBuild.rawBuild.getCauses()
       for(cause in causes) {
          if(cause.properties.shortDescription =~ 'Started by user') {
             manualTrigger = true
             break
          }
      }
  }

And rest of my workflow is in another node

   node('nodefarm') {
       if(manualTrigger) {
         // do build stuff here
       } else {
         //build not triggered by user.
       }
   } 
查看更多
登录 后发表回答