[英]Passing environment variable as a pipeline parameter to Jenkins shared library
I have a shared Jenkins library that has my pipeline for Jenkinsfile.我有一个共享的 Jenkins 库,里面有我的 Jenkinsfile 管道。 The library is structured as follows:
该库的结构如下:
myPipeline.groovy file myPipeline.groovy文件
def call(body) {
def params= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = params
body()
pipeline {
// My entire pipeline is here
// Demo stage
stage("Something"){
steps{
script{
projectName = params.name
}
}
}
}
}
And my Jenkinsfile is as follows:我的 Jenkinsfile 如下:
Jenkinsfile詹金斯档案
@Library("some-shared-lib") _
myPipeline{
name = "Some name"
}
Now, I would like to replace "Some name"
string with "env.JOB_NAME"
command.现在,我想用
"env.JOB_NAME"
命令替换"Some name"
字符串。 Normally in Jenkinsfile, I would use name = "${env.JOB_NAME}"
to get the info, but because I am using my shared library instead, it failed to work.通常在 Jenkinsfile 中,我会使用
name = "${env.JOB_NAME}"
来获取信息,但是因为我使用的是我的共享库,所以它无法工作。 Error message is as follows:错误信息如下:
java.lang.NullPointerException: Cannot get property 'JOB_NAME' on null object
I tried to play around with brackets and other notation but never got it to work.我尝试使用括号和其他符号,但从未让它起作用。 I think that I incorrectly pass a parameter.
我认为我错误地传递了一个参数。 I would like Jenkinsfile to assign
"${env.JOB_NAME}"
to projectName
variable, once library runs the pipeline that I am calling (via myPipeline{}
command)我希望 Jenkinsfile 将
"${env.JOB_NAME}"
分配给projectName
变量,一旦库运行我正在调用的管道(通过myPipeline{}
命令)
You can do like this in myPipeline.groovy
:你可以在
myPipeline.groovy
这样做:
def call(body) {
def params= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = params
body()
pipeline {
// My entire pipeline is here
// Demo stage
stage("Something"){
steps{
script{
projectName = "${env.JOB_NAME}"
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.