插件源码地址 https://github.com/jenkinsci/build-user-vars-plugin

问题背景

1
2
3
之前有写过 一篇 https://blog.csdn.net/mmh19891113/article/details/105747729 
一直在使用 BuildUserVars 和 BuildNameDescriptionSetter 这2个插件,当然是在自由风格的job中使用的,
但是当我们转到 流水线 风格的job时候 发现 build-user-vars-plugin 不太好用了。

解决问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
通过翻阅资料,谷歌查询。
我们从这里 https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/ 查到一个关于 wrap的用法。


wrap: General Build Wrapper
This is a special step that allows to call build wrappers (also called "Environment Configuration" in freestyle or similar projects). Just select the wrapper to use from the dropdown list and configure it as needed. Everything inside the wrapper block is under its effect.

Note that only Pipeline-compatible wrappers will be shown in the list.

To use this step you need to specify a delegate class, e.g wrap([$class: 'AnsiColorBuildWrapper']).

通过介绍 简单的可以理解为 在自由风格 "Environment Configuration" 处 定义的,勾选的特性可以使用 wrap 代码
在流水线中达到同样的功能。

其实 "Environment Configuration" 处 定义的 插件 都是继承 java 类 BuildWrapper的。
例如设置彩色输出的类是 AnsiColorBuildWrapper,
例如我们今天介绍的这个 设置 BUILD_USER 变量的类是 BuildUser。 (代码参考 https://github.com/jenkinsci/build-user-vars-plugin/blob/master/src/main/java/org/jenkinsci/plugins/builduser/BuildUser.java)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
参考 插件wiki
https://plugins.jenkins.io/build-user-vars-plugin/

目前版本的插件 提供了以下的几个变量:
Variable Description
BUILD_USER Full name (first name + last name)
BUILD_USER_FIRST_NAME First name
BUILD_USER_LAST_NAME Last name
BUILD_USER_ID Jenkins user ID
BUILD_USER_GROUPS Jenkins user groups
BUILD_USER_EMAIL Email address

在自由风格job中直接使用就行。


在流水线中 这样使用:
node {
wrap([$class: 'BuildUser']) {
def user = env.BUILD_USER_ID
}
}

自由风格中的用法

在这里插入图片描述我这个截图目前用的插件版本不是最新的,少了几个变量的。BUILD_USER_GROUPS 好像没有。

通常我们一般会和 “ Build Name and Description Setter” 插件一起使用。来设置 buildName。让构建历史看到更清晰。
在这里插入图片描述
然后执行构建,然后看到构建历史就会是下面这样的了。如果是其他人触发的就会显示其他人的账号信息。
在这里插入图片描述
我们设置个定时器触发的。效果如下。定时器触发的有个几个变量是没有值的。不过一般的 BUILD_USER 和 BUILD_USER_ID 会有值的。不过定时器触发 默认这个插件没提供支持设置build user
的,我是经过修改的。不过最新的这个插件版本我还没有研究它的代码是否加上了更多的build user支持。https://github.com/mamh-java/build-user-vars-plugin/commit/df831cd63ed9ef61ba0008f70c58f86b21636baa
在这里插入图片描述

流水线中用法

利用 wrap 来使用, wrap 要放到 steps 里面, 和 sh 平级别的。这里 只是 包裹了几个 sh 语句,然后
sh 语句里面就可以使用这几个 BUILD_USER 相关的变量了。出了这个wrap代码块就没这个变量了。
如果想 出了 这个 wrap 代码块使用这个 BUILD_USER 相关的变量 需要设置赋值给全局变量了。
全局变量可以参考 https://blog.csdn.net/mmh19891113/article/details/109862961

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
pipeline {
agent any
stages {
stage("Stage 1") {
steps {
wrap([$class: 'BuildUser']) {
sh 'echo "${BUILD_USER}"'
sh 'echo "${BUILD_USER_ID}"'
sh 'echo "${BUILD_USER_EMAIL}"'
script{
user = "${BUILD_USER}" // 这里赋值给全局变量 后续的stage中也可以使用到。
}

buildName "[${BUILD_NUMBER}#${BUILD_USER}][${BUILD_USER_ID}][${BUILD_USER_EMAIL}]"
}
}
}

stage("Stage 2") {
steps {
echo "user is '${user}'" // 这里会打印 触发人的姓名

}
}


}
}

在这里插入图片描述
这几个的解决方案的目标 都是为了设置buildName的值,其中包含触发人,方便在构建历史中查找。
下面这个 在没有找到 wrap 解决方案 之前的流水线的用法, 这里其实是 找 causes ,jenkins中有个 currentBuild.getBuildCauses() 就是获取这次构建的原因的。里面有包含个人手动触发的,定时器触发的,或者其他事件触发的,等等, 通过 currentBuild.getBuildCauses() 获取的会比较全的。像前面的
使用 Build user vars 插件的 会有某些触发的 事件 显示不了 的, 这个是 build user vars 插件的局限性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    stage("0.get causes") {
steps {
echo "will run script to get causes"
script {
def causes = currentBuild.getBuildCauses()
def causesShort = ""
if (causes != null) {
causes.each {
def desc = it.shortDescription
desc = desc.replace("Started by", "")
if (desc.contains(":")) {
desc = desc.split(":")[0]
}
causesShort += "${desc}, "
}
causesShort = causesShort.trim()
G_SHORT_CAUSES = causesShort[0..-2]
}
} // end script

echo "cause by: [${G_SHORT_CAUSES}]"
buildName "cause by: [${G_SHORT_CAUSES}]"
}
}

上面的流水线脚本效果如下,或设置 buildName 会 当前 构建的 cause。可以看出来 这个构建是由
个人手动触发,然后 回放的构建52 触发的。。
在这里插入图片描述