How to get the BUILD_USER in Jenkins when job triggered by timer?

后端 未结 14 1538
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 03:01

I wanted to show the user who triggered a Jenkins job in the post job email. This is possible by using the plugin Build User Vars Plugin and the env variable

相关标签:
14条回答
  • 2020-12-09 03:06

    SIMPLE SOLUTIONS (NO PLUGINS) !!

    METHOD 1: Via Shell

    BUILD_TRIGGER_BY=$(curl -k --silent ${BUILD_URL}/api/xml | tr '<' '\n' | egrep '^userId>|^userName>' | sed 's/.*>//g' | sed -e '1s/$/ \//g' | tr '\n' ' ')
    echo "BUILD_TRIGGER_BY: ${BUILD_TRIGGER_BY}"
    

    METHOD 2: Via Groovy

    node('master') {
    BUILD_TRIGGER_BY = sh ( script: "BUILD_BY=\$(curl -k --silent ${BUILD_URL}/api/xml | tr '<' '\n' | egrep '^userId>|^userName>' | sed 's/.*>//g' | sed -e '1s/\$/ \\/ /g'); if [[ -z \${BUILD_BY} ]]; then BUILD_BY=\$(curl -k --silent ${BUILD_URL}/api/xml | tr '<' '\n' | grep '^shortDescription>' | sed 's/.*user //g;s/.*by //g'); fi; echo \${BUILD_BY}", returnStdout: true ).trim()
    echo "BUILD_TRIGGER_BY: ${BUILD_TRIGGER_BY}"
    }
    

    METHOD 3: Via Groovy

    BUILD_TRIGGER_BY = "${currentBuild.getBuildCauses()[0].shortDescription} / ${currentBuild.getBuildCauses()[0].userId}"
    echo "BUILD_TRIGGER_BY: ${BUILD_TRIGGER_BY}"
    

    OUTPUT:

    Started by user Admin / user@example.com
    

    Note: Output will be both User ID and User Name

    0 讨论(0)
  • 2020-12-09 03:06

    Just to elaborate on Musaffir Lp's answer. The Conditional Build Step plugin now supports the Build Cause directly - it requires the Run Condition Plugin also.

    If you wanted to detect when the build was started by a timer you can select a Run? value of Build Cause, with Build Cause of: TimerTrigger

    This is a little simpler and more robust than using a regex. There are also other triggers you can detect, for example when the build was a result of Source Control Management commit, you can select: SCMTrigger.

    0 讨论(0)
  • 2020-12-09 03:12

    This below is working for me.

    Install "user build vars plugin"

    Build Name  =   ${BUILD_NUMBER}_${TICKET}_${ENV,var="BUILD_USER_ID"}
    

    0 讨论(0)
  • 2020-12-09 03:15

    I found similar but really working on Jenkins 2.1.x and easy for my understanding way. And it works without any plugins.

    if (currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')['userId']){
        // Will be run only if someone user triggers build
        // Because in other cases this contructions returns null
    }
    

    You can use in this construction any classes described here. They will be returns maps with usable values.

    0 讨论(0)
  • 2020-12-09 03:16

    I wanted to trigger build initiator info to one of my slack/flock group so I used following way to get build initiator email and name by writing in Declarative fashion .

    I am just printing here, you can use to store in some environment variable or write in one file giving file path according to your own convenience..

    pipeline {
    environment {
        BRANCH_NAME = "${env.BRANCH_NAME}"
        }
    agent any
    
    stages{
            stage('Build-Initiator-Info'){
                    sh 'echo $(git show -s --pretty=%ae)'
                    sh 'echo $(git show -s --pretty=%an)'
            }
               }
    

    }

    0 讨论(0)
  • 2020-12-09 03:23

    This gets the username who clicked "Build Now" in a Jenkins pipeline job.

    @NonCPS
    def getBuildUser() {
        return currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId()
    }
    
    0 讨论(0)
提交回复
热议问题