如何将共享库的分支名称指定为环境变量

问题描述

我想引用/指定(语法)分支,该分支设置为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设置为环境变量一样

<?xml version='1.1' encoding='UTF-8'?>
<org.jenkinsci.plugins.workflow.libs.GlobalLibraries plugin="workflow-cps[email protected]">
  <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="[email protected]">
          <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>

谢谢, 草间

解决方法

我认为无论如何都不可能使环境变量可用于 org.jenkinsci.plugins.workflow.libs.GlobalLibraries.xml。但是如果你使用 Jenkins Code As Configuration 插件,你可以从 docker-compose 传递变量并将其提供给配置文件,当 Jenkins 加载配置文件为 Jenkins 准备配置时就可以了。 可以找到一个例子here