In Jenkins, how do builds know who requested them?

为君一笑 提交于 2019-12-30 07:57:10

问题


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 line is always "Started by user foo," so Jenkins is clearly keeping track of who triggered the build. So it should be possible to pass that information down to the job..... The question is, how?


回答1:


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>
    [...]



回答2:


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.



回答3:


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:

]
// 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)
  ]))
}



回答4:


In your Job add "Execute system Groovy script":

def yourUserName = build.causes[0].userId



回答5:


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']



回答6:


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).



来源:https://stackoverflow.com/questions/5902426/in-jenkins-how-do-builds-know-who-requested-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!