[英]In Jenkins pipeline, how to set a value for an environment variable when loading a shared library?
I have a multi-branch pipeline that uses a Jenkinsfile
to load a shared library defined in my system configuration.我有一个多分支管道,它使用
Jenkinsfile
加载在我的系统配置中定义的共享库。
@Library("my-shared-library") _
import com.company.exa.builders.BaseBuilder
import com.company.exa.builders.EdiBuilder
import hudson.model.*
buildNumbers = getBuildNumbers() // Function not shown, but it works
properties ([
disableConcurrentBuilds(),
[$class: 'jenkins.model.BuildDiscarderProperty',
strategy: [$class: 'LogRotator',
numToKeepStr: '50',
artifactNumToKeepStr: '20']],
parameters ([
choiceParam(name: "VERSION_CHOICE",
choices: buildNumbers,
description: "Version from Builds"),
stringParam(name: "VERSION_PASSEDIN",
defaultValue: env.BRANCH_NAME,
description: "Passed-in version. Note this will override VERSION_CHOICE."),
booleanParam(name: "UPLOAD_ARTIFACTS",
defaultValue: false,
description: "Upload artifacts to file servers?"),
choiceParam(name: "DEBUG_LEVEL",
choices: ["0", "1", "2", "3"],
description: "Debug level; 0=less verbose, 3=most verbose")
])
])
When I run it clicking Scan Multibranch Pipeline Now
, I get当我单击
Scan Multibranch Pipeline Now
运行它时,我得到
00:00:01.018 Loading library my-shared-library
00:00:01.019 Attempting to resolve maser from remote references...
00:00:01.019 > git --version # timeout=10
00:00:01.023 > git --version # 'git version 2.17.1'
00:00:01.023 using GIT_SSH to set credentials Jenkins Master SSH
00:00:01.028 > git ls-remote -h -- git@bitbucket.org:cfouts-kmha/kmha-infrastructure.git # timeout=10
00:00:01.546 Found match: refs/heads//master revision a1bc1e273b41c4e892d7c25814d0f2a1c261f7e5
00:00:01.546 ERROR: Checkout failed
00:00:01.546 java.lang.IllegalArgumentException: Null value not allowed as an environment variable: VERSION_PASSEDIN
00:00:01.546 at hudson.EnvVars.put(EnvVars.java:379)
00:00:01.546 at hudson.model.StringParameterValue.buildEnvironment(StringParameterValue.java:59)
...complaining that variable VERSION_PASSEDIN
is null. ...抱怨变量
VERSION_PASSEDIN
是 null。 I've tried setting the VERSION_PASSEDIN
variable to just ""
in the following locations to no avail...我尝试在以下位置将
VERSION_PASSEDIN
变量设置为""
但无济于事......
Any clues on how to fix this?关于如何解决这个问题的任何线索? I have a feeling it's something obvious that I'm not seeing.
我有一种感觉,这是我没有看到的明显的东西。
Note that if I run the job with a branch's "Build with parameters" link, the job runs fine.请注意,如果我使用分支的“使用参数构建”链接运行作业,则作业运行良好。
You have a chicken-and-egg problem when trying to set default value for parameters, eg here:尝试为参数设置默认值时,您遇到了先有鸡还是先有蛋的问题,例如:
stringParam(name: "VERSION_PASSEDIN",
defaultValue: env.BRANCH_NAME,
To run your pipeline, Jenkins needs to figure out the value of the parameters.要运行您的管道,Jenkins 需要计算出参数的值。 However, to figure out the value of the parameters, Jenkins needs to run your pipeline.
但是,要弄清楚参数的值,Jenkins 需要运行您的管道。 See the problem?
看到问题了吗?
To overcome this problem, here's what happens: Jenkins assumes the value of nothing for both VERSION_CHOICE
and VERSION_PASSEDIN
.为了克服这个问题,会发生以下情况: Jenkins 假定
VERSION_CHOICE
和VERSION_PASSEDIN
的值为 none 。 In the first case, it's an empty choice;在第一种情况下,这是一个空洞的选择; in the second, it's
null
.第二个是
null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.