简体   繁体   English

在 Jenkins 管道中,加载共享库时如何设置环境变量的值?

[英]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变量设置为""但无济于事......

  • The multi-branch pipeline's Folder properties多分支管道的文件夹属性
  • The multi-branch pipeline's parent folder properties多分支管道的父文件夹属性
  • In the Jenkinsfile itself在 Jenkinsfile 本身
  • In the System configuration global properties在系统配置全局属性中

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_CHOICEVERSION_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM