如何在Jenkins的groovy动态参数上获取作业名称

Dav*_*uez 10 groovy jenkins

我需要在此示例中获取名称作业,我有一个代码,用于获取具有该代码的构建参数的先前值

jenkins.model.Jenkins.instance.getItem("nameOfJob").lastBuild.getBuildVariables().get("NameOfParameter");
Run Code Online (Sandbox Code Playgroud)

现在的工作名称是硬编码的,我需要得到这个名字将是当前工作的名称,我该怎么办?

谢谢

小智 12

Groovy动态参数无法访问jenkins工作的其余部分所知的常用环境变量.

这是一个工作黑客获取工作名称:

def build = Thread.currentThread().toString()
def regexp= ".+?/job/([^/]+)/.*"
def match = build  =~ regexp
def jobName = match[0][1]
Run Code Online (Sandbox Code Playgroud)


小智 7

以下来自 uno-choice 插件参数 Groovy 脚本对我有用(意味着在构建绑定之前):

def job = this.binding.jenkinsProject
Run Code Online (Sandbox Code Playgroud)

如果您对职位名称感兴趣,那么:

def jobName = this.binding.jenkinsProject.name
Run Code Online (Sandbox Code Playgroud)


Kee*_*yOn 5

如果您正在项目中运行某个步骤,则存在一个环境变量JOB_NAME

可以在groovy中访问

String jobName = System.getenv('JOB_NAME')
Run Code Online (Sandbox Code Playgroud)

如果您希望它在构建后发布,请检查此答案如何通过Groovy在Jenkins中获取有关当前构建项目的特定信息?

根据其他问题,在设置阶段要访问Jenkins职位名称,您需要使用支持groovy 的EnvInject插件 -它需要返回地图

println "currentJob -->${currentJob}"
println "currentBuild --> ${currentBuild}"

[currentJob: currentJob, currentBuild: currentBuild]
Run Code Online (Sandbox Code Playgroud)

这是我使用shell作业回显新环境时的输出

Started by user anonymous
[EnvInject] - Loading node environment variables.
[EnvInject] - Preparing an environment for the build.
[EnvInject] - Keeping Jenkins system variables.
[EnvInject] - Keeping Jenkins build variables.
[EnvInject] - Evaluation the following Groovy script content: 

println "currentJob -->${currentJob}"
println "currentBuild --> ${currentBuild}"

[currentJob: currentJob, currentBuild: currentBuild]

currentJob -->hudson.model.FreeStyleProject@5ffe31a7[_xxx]
currentBuild --> _xxx #6
[EnvInject] - Injecting contributions.
Building on master in workspace /var/lib/jenkins/workspace/_xxx
[_xxx] $ /bin/sh -xe /tmp/hudson3812821436584477615.sh
+ echo _xxx #6
_xxx #6 
+ echo hudson.model.FreeStyleProject@5ffe31a7[_xxx]
hudson.model.FreeStyleProject@5ffe31a7[_xxx]
Finished: SUCCESS
Run Code Online (Sandbox Code Playgroud)

您需要进行一些字符串操作以分离作业和构建