首页 > 解决方案 > 如何将共享库的分支名称指定为环境变量

问题描述

我想引用/指定(语法)分支,该分支设置为 Jenkins 共享库的环境变量,它将在 docker 容器期间提供。例如:

@Library(['my-shared-library', BRANCH_NAME])

尝试使用 ${BRANCH_NAME} ${env.BRANCH_NAME}

我将在 docker-compose.yml 中提供 BRANCH_NAME 作为环境变量

我也想在 org.jenkinsci.plugins.workflow.libs.GlobalLibraries.xml 中获得 env 变量

就像我将 PIPELINE_VERSION 设置为 env 变量

<?xml version='1.1' encoding='UTF-8'?>
<org.jenkinsci.plugins.workflow.libs.GlobalLibraries plugin="workflow-cps-global-lib@2.15">
  <libraries>
    <org.jenkinsci.plugins.workflow.libs.LibraryConfiguration>
      <name>XXXXXXXXXXXX</name>
      <retriever class="org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever">
        <scm class="jenkins.plugins.git.GitSCMSource" plugin="git@3.12.0">
          <id>XXXXXXXXXXXXXXXXXXXXXXXX</id>
          <remote>XXXXXXXXXXXXXXXXXXX</remote>
          <credentialsId>jXXXXXXXXXXXXXXXXXXXX</credentialsId>
          <traits>
            <jenkins.plugins.git.traits.BranchDiscoveryTrait/>
          </traits>
        </scm>
      </retriever>
      <defaultVersion>${PIPELINE_RELEASE_VERSION}</defaultVersion>
      <implicit>true</implicit>
      <allowVersionOverride>true</allowVersionOverride>
      <includeInChangesets>false</includeInChangesets>
    </org.jenkinsci.plugins.workflow.libs.LibraryConfiguration>
  </libraries>
</org.jenkinsci.plugins.workflow.libs.GlobalLibraries>

谢谢, Kusuma

标签: dockerjenkinsdocker-composejenkins-pipelinejenkins-shared-libraries

解决方案


无论如何,我认为不可能使环境变量可用于 org.jenkinsci.plugins.workflow.libs.GlobalLibraries.xml。但是,如果您使用Jenkins 代码作为配置插件,您可以从 docker-compose 传递变量并将其提供给配置文件,并且当 Jenkins 加载配置文件为 Jenkins 准备配置时将起作用。一个例子可以在这里找到


推荐阅读