可以在Jenkins共享库中使用构建用户变量插件吗? [英] Is it possible to use build user vars plugin in a Jenkins shared library?
本文介绍了可以在Jenkins共享库中使用构建用户变量插件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在实现将Jenkins管道的触发用户公开到我们的CD系统的功能,因此我获取了构建用户vars插件:https://plugins.jenkins.io/build-user-vars-plugin/
该插件的工作方式似乎是包装您需要向其公开变量的代码,如下所示:
wrap([$class: 'BuildUser']) {
userId = env.BUILD_USER_ID
}
我在一条常规管道上尝试了一下,然后重复了一下,一切正常。
然后我尝试在我们的共享库中实现它,以便在所有对cd的调用中都会发生这种情况,但我遇到了错误。
wrap([$class: 'BuildUser']) {
jobBuildUrl ="${jobBuildUrl}&USER_ID=${env.BUILD_USER_ID}"
}
[2021-08-19T10:20:22.852Z]hudson.emoting.Proxy异常:groovy.lang.Missing方法异常:无方法签名:com.company.jenkins.pipelines.BuildManager.wrap()适用于参数类型:(java.util.LinkedHashMap,BuildUser值:[[$org.jenkinsci.plugins.workflow.cps.CpsClosure2):BuildUser],org.jenkinsci.plugins.workflow.cps.CpsClosure2@1c9a210c]
有没有办法在共享库中使用此插件代码?如果是,如何?
我不这么认为,但我觉得这值得一问。作为参考,有一个悬而未决的问题:https://issues.jenkins.io/browse/JENKINS-44741
附注,我正试图在不触及每个人的管道本身的情况下这样做。如果这个插件不可能做到这一点,我可能只会在共享库中实现我自己的版本。推荐答案
该插件很难使用,并且有许多问题,其中之一是共享库。
相反,只需自己实现它,它相对容易,并允许您更多地控制所做的事情、错误处理以及返回哪些参数的逻辑。
您可以使用类似以下内容:
/**
* Get the last upstream build that triggered the current build
* @return Build object (org.jenkinsci.plugins.workflow.job.WorkflowRun) representing the upstream build
*/
@NonCPS
def getUpstreamBuild() {
def build = currentBuild.rawBuild
def upstreamCause
while (upstreamCause = build.getCause(hudson.model.Cause$UpstreamCause)) {
build = upstreamCause.upstreamRun
}
return build
}
/**
* Get the properties of the build Cause (the event that triggered the build)
* @param upstream If true (Default) return the cause properties of the last upstream job (If the build was triggered by another job or by a chain of jobs)
* @return Map representing the properties of the user that triggered the current build.
* Contains keys: USER_NAME, USER_ID
*/
@NonCPS
def getUserCauseProperties(Boolean upstream = true) {
def build = upstream ? getUpstreamBuild() : currentBuild.rawBuild
def cause = build.getCause(hudson.model.Cause$UserIdCause)
if (cause) {
return ['USER_NAME': cause.userName, 'USER_ID': cause.userId]
}
println "Job was not started by a user, it was ${build.getCauses()[0].shortDescription}"
return [:]
}
这篇关于可以在Jenkins共享库中使用构建用户变量插件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文