how to get $CAUSE in workflow

后端 未结 9 1719
暗喜
暗喜 2020-12-10 01:24

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 out

相关标签:
9条回答
  • 2020-12-10 01:57

    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.

    0 讨论(0)
  • 2020-12-10 02:05

    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

    0 讨论(0)
  • 2020-12-10 02:10

    As of early 2018, it looks like that information is now available with JENKINS-31576 being closed:

    def manualTrigger = true
    currentBuild.upstreamBuilds?.each { b ->
      echo "Upstream build: ${b.getFullDisplayName()}"
      manualTrigger = false
    }
    
    0 讨论(0)
  • 2020-12-10 02:10

    Looks like as of Jenkins 2.22 (JENKINS-41272), you can access the currentBuild.getBuildCauses() method to get an array of build causes. For example:

    environment {
      CAUSE = "${currentBuild.getBuildCauses()[0].shortDescription}"
    }
    
    steps {
      echo "Build caused by ${env.CAUSE}"
    }
    
    0 讨论(0)
  • 2020-12-10 02:11

    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.

    0 讨论(0)
  • 2020-12-10 02:12

    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
    }
    
    0 讨论(0)
提交回复
热议问题