In Jenkins, how do builds know who requested them?

后端 未结 6 1101
南旧
南旧 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:13

    In your Job add "Execute system Groovy script":

    def yourUserName = build.causes[0].userId
    
    0 讨论(0)
  • 2021-01-04 01:18

    The username isn't put in an easy-to-fetch environment variable, but you can get it using the xml (or json or python) api - as soon as you start a build, http://[jenkins-server]/job/[job-name]/[build-number]/api/xml is populated with details:

    <freeStyleBuild>
        <action>
            <cause>
                <shortDescription>Started by user foobar</shortDescription>
                <userName>foobar</userName>
            </cause>
        </action>
        <building>true</building>
        [...]
    
    0 讨论(0)
  • 2021-01-04 01:20

    user30997

    Please check out Jenkins Build User Vars plugin, it does what you need:

    It is used to set following user build variables:

    • BUILD_USER – full name of user started build,
    • BUILD_USER_FIRST_NAME – first name of user started build,
    • BUILD_USER_LAST_NAME – last name of user started build,
    • BUILD_USER_ID – id of user started build.
    0 讨论(0)
  • 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)
      ]))
    }
    
    0 讨论(0)
  • 2021-01-04 01:25
    import os
    import jenkinsapi.build
    import jenkinsapi.jenkins
    
    #Required Environment variables example:
    #JENKINS_URL=http://jenkinsserver/
    #JOB_NAME=TEST_GT
    #BUILD_NUMBER=8
    
    jenkins_inst = None
    
    def get_jenkins_inst():
        if jenkins_inst == None:
            jenkins_url = os.environ['JENKINS_URL']
            print("Connect to jenkins " + jenkins_url)
            jenkins_inst = jenkinsapi.jenkins.Jenkins(jenkins_url)
        return jenkins_inst
    
    def get_jenkins_job():
        jenkins_inst = get_jenkins_inst()
    
        jenkins_job_name = os.environ['JOB_NAME']
        print("Get jenkins job " + jenkins_job_name)
        return jenkins_inst.get_job(jenkins_job_name)
    
    def get_jenkins_user():
        jenkins_job = get_jenkins_job()
    
        jenkins_buildno = int(os.environ['BUILD_NUMBER'])
        print("Get jenkins job build " + str(jenkins_buildno))
        cur_build = jenkins_job.get_build(jenkins_buildno)
    
        return cur_build.get_actions()['causes'][0]['userId']
    
    0 讨论(0)
  • 2021-01-04 01:36

    I managed to get it (on Jenkins 2.58):

    currentBuild.getRawBuild().getCauses()[0].getUserId()
    

    Of course you need to set permissions in Jenkins to be able to call these methods. It's not always the 0th Cause object you are looking for, e.g. it may be another one if you replay another user's build (did not test this).

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