In Jenkins, how do builds know who requested them?

2019-02-16 06:51发布

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?

6条回答
乱世女痞
2楼-- · 2019-02-16 07:24

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楼-- · 2019-02-16 07:31

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)
  ]))
}
查看更多
三岁会撩人
4楼-- · 2019-02-16 07:35
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']
查看更多
Evening l夕情丶
5楼-- · 2019-02-16 07:40

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

查看更多
Melony?
6楼-- · 2019-02-16 07:44

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>
    [...]
查看更多
【Aperson】
7楼-- · 2019-02-16 07:48

In your Job add "Execute system Groovy script":

def yourUserName = build.causes[0].userId
查看更多
登录 后发表回答