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

后端 未结 14 1539
爱一瞬间的悲伤
爱一瞬间的悲伤 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:26

    Build user vars plugin wasn't working for me so I did a quick-and-dirty hack:

    BUILD_CAUSE_JSON=$(curl --silent ${BUILD_URL}/api/json | tr "{}" "\n" | grep "Started by")
    BUILD_USER_ID=$(echo $BUILD_CAUSE_JSON | tr "," "\n" | grep "userId" | awk -F\" '{print $4}')
    BUILD_USER_NAME=$(echo $BUILD_CAUSE_JSON | tr "," "\n" | grep "userName" | awk -F\" '{print $4}')
    
    0 讨论(0)
  • 2020-12-09 03:26

    For declarative pipeline syntax, here is a quick hack, base on @Kevin answer. For declarative pipeline you need to enclose them in a node, else you will get an error/ build failure

    node {
        def BUILD_FULL = sh (
            script: 'curl --silent '+buildURL+' | tr "{}" "\\n" | grep -Po \'"shortDescription":.*?[^\\\\]"\' | cut -d ":" -f2',
            returnStdout: true
            )
    
        slackSend channel: '#ci-cd',
              color: '#000000',
              message: "The pipeline was ${BUILD_FULL}  ${GIT_COMMIT_MSG} "
    
    }
    

    The output will be slack notification sent to your slack channel with the git short description

    0 讨论(0)
  • Install 'Build User Vars Plugin' and use like below:- [ See https://plugins.jenkins.io/build-user-vars-plugin ]

    Be sure to check mark the Set jenkins user build variables checkbox under Build Environment for your Jenkins job's configuration.

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

    Without Plugin ->

    def cause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
    echo "userName: ${cause.userName}"
    
    0 讨论(0)
  • 2020-12-09 03:29

    The token $BUILD_CAUSE from the email-ext plugin is what you are looking for.

    You can see the full content token reference when you click the ? just after the Attach build log combobox at the email content configuration.

    Some tokens get added by plugins, but this one should be aviable by default.

    Edit: As pointed out by bishop in the comments, when using the EnvInject plugin, the $BUILD_CAUSE token gets changed to behave differently.

    0 讨论(0)
  • This can be done using the Jenkins Build User Vars Plugin which exposes a set of environment variables, including the user who started the build. It gives environment variables like BUILD_USER_ID, EMAIL, etc.

    When the build is triggered manually by a logged-in user, that user's userid is available in the BUILD_USER_ID environment variable.

    However, this environment variable won't be replaced / initialized when the build is automatically triggered by a Jenkins timer / scheduler.

    Attached a screenshot for details

    This can be resolved by injecting a condition to the Job by using Conditional Build Step Plugin / Run Condition Plugin,where in to each job we can add a condition to initialize the variable BUILD_USER_ID only when the build is caused or triggered by the Timer or scheduler, by setting a condition using the regular expression..

    0 讨论(0)
提交回复
热议问题