In Jenkins, how do builds know who requested them?

后端 未结 6 1144
南旧
南旧 2021-01-04 01:09

I need to pass the username of the requestor of a build down to the script that is actually doing the work. Looking at the console output for a particular build, the first

6条回答
  •  孤独总比滥情好
    2021-01-04 01:24

    I tried to use Jenkins Build User Vars plugin and notify a HipChat room that a build was started by a certain user, but BUILD_USER variable was not available to HipChat plugin, possibly because HipChat action happened before Build User Vars plugin injects the variable.

    So I installed pre-scm-buildstep plugin and added:

    enter image description here]

    // Inject environment variables using Groovy
    
    import hudson.model.*
    
    def build = Thread.currentThread().executable
    def userCause = build.getCause(hudson.model.Cause$UserIdCause)
    def userName = userCause?.userId ?: 'Jenkins'
    
    def envVars = ['BUILD_USER': userName]
    
    for (item in envVars) {
      build.addAction(new ParametersAction([
        new StringParameterValue(item.key, item.value)
      ]))
    }
    

提交回复
热议问题