在 Jenkins 中,构建如何知道谁请求了它们?

发布于 2024-11-05 15:11:28 字数 113 浏览 0 评论 0原文

我需要将构建请求者的用户名传递给实际正在执行工作的脚本。查看特定构建的控制台输出,第一行始终是“由用户 foo 启动”,因此 Jenkins 清楚地跟踪谁触发了构建。因此应该可以将该信息传递给工作。问题是,如何?

I need to pass the username of the requester 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

澜川若宁 2024-11-12 15:11:29

我尝试使用 Jenkins Build User Vars 插件 和通知 HipChat 房间某个用户启动了构建,但 HipChat 插件无法使用 BUILD_USER 变量,可能是因为 HipChat 操作发生在 Build User Vars 插件之前注入变量。

所以我安装了 pre-scm-buildstep 插件 并添加:

在此处输入图像描述]

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

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)
  ]))
}
却一份温柔 2024-11-12 15:11:29

在您的作业中添加“执行系统 ​​Groovy 脚本”:

def yourUserName = build.causes[0].userId

In your Job add "Execute system Groovy script":

def yourUserName = build.causes[0].userId
爱的故事 2024-11-12 15:11:29

我设法得到它(在 Jenkins 2.58 上):

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

当然你需要在 Jenkins 中设置权限才能调用这些方法。
它并不总是您正在寻找的第 0 个 Cause 对象,例如,如果您重播另一用户的构建(没有测试这一点),它可能是另一个对象。

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

对你再特殊 2024-11-12 15:11:29
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']
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']
情栀口红 2024-11-12 15:11:28

user30997

请查看 Jenkins Build User Vars 插件,它执行您需要的操作:

它用于设置以下用户构建变量:

  • BUILD_USER – 用户启动构建的全名,
  • BUILD_USER_FIRST_NAME – 用户启动构建的名字,
  • BUILD_USER_LAST_NAME – 开始构建的用户的姓氏,
  • BUILD_USER_ID – 开始构建的用户的 ID。

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.
滴情不沾 2024-11-12 15:11:28

用户名没有放在易于获取的环境变量中,但您可以使用 xml(或 json 或 python)api - 一旦开始构建,http://[jenkins-server]/job/[job-name]/[build -number]/api/xml 填充了详细信息:

<freeStyleBuild>
    <action>
        <cause>
            <shortDescription>Started by user foobar</shortDescription>
            <userName>foobar</userName>
        </cause>
    </action>
    <building>true</building>
    [...]

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>
    [...]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文