为仅为一个管道运行的作业动态保存 GitLab 变量

时间:2021-05-26 14:48:14

标签: variables gitlab gitlab-ci

我正在尝试为 Maven 项目自动化一个 DefectDojo 模板。 我需要在 DefectDojo 实例中创建一个 Product 并存储创建的产品的 ID。

我为一个只运行一次的作业编写了一个脚本(创建项目时有一个管道,然后它因为没用而被删除)。

因此,我无法使用 .env 工件将获得的 ID 传递到其他阶段,因为作业不再运行。

实际上,我想使用运行一次的作业动态分配 PRODUCT_ID 作为 GitLab 变量。这可能吗?

1 个答案:

答案 0 :(得分:0)

您仍然可以将其传递给其他作业,也可以将其用作工件并将其传递给以后的作业或管道。如果需要,您还可以考虑将其保存为项目变量。 您可以在稍后阶段将环境变量从一个作业传递到另一个作业。这些变量不能用作 CI/CD 变量来配置管道,但可以在作业脚本中使用。

在作业脚本中,将变量保存为 <nav class="hidden md:flex space-x-10"> <div class="relative"> <!-- Item active: "text-gray-900", Item inactive: "text-gray-500" --> <button type="button" class=" text-gray-500 group bg-white rounded-md inline-flex items-center text-base font-medium hover:text-gray-900 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 " aria-expanded="false" hx-get="#response" hx-target="#response" hx-indicator=".htmx-indicator" > <span>Articles</span> <!-- Heroicon name: solid/chevron-down Item active: "text-gray-600", Item inactive: "text-gray-400" --> <svg class="text-gray-400 ml-2 h-5 w-5 group-hover:text-gray-500" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true" > <path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" /> </svg> </button> <!-- 'Solutions' flyout menu, show/hide based on flyout menu state. Entering: "transition ease-out duration-200" From: "opacity-0 translate-y-1" To: "opacity-100 translate-y-0" Leaving: "transition ease-in duration-150" From: "opacity-100 translate-y-0" To: "opacity-0 translate-y-1" --> <div id="response" name="response" class=" absolute z-10 -ml-4 mt-3 transform px-2 w-screen max-w-md sm:px-0 lg:ml-0 lg:left-1/2 lg:-translate-x-1/2 " > <div class=" rounded-lg shadow-lg ring-1 ring-black ring-opacity-5 overflow-hidden " > <div class=" relative grid gap-6 bg-white px-5 py-6 sm:gap-8 sm:p-8 " > <a href="#" class=" -m-3 p-3 flex items-start rounded-lg hover:bg-gray-50 " > <!-- Heroicon name: outline/chart-bar --> <svg class="flex-shrink-0 h-6 w-6 text-indigo-600" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true" > <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" /> </svg> <div class="ml-4"> <p class="text-base font-medium text-gray-900"> Analytics </p> <p class="mt-1 text-sm text-gray-500"> Get a better understanding of where your traffic is coming from. </p> </div> </a> </div> </div> </nav> 。 将 .env 文件另存为 artifacts:reports:dotenv 工件。 在稍后阶段设置作业以使用依赖项或需求关键字接收工件。 随后的作业可以在脚本中使用该变量。 例如,使用 dependencies 关键字:

.env file

例如,使用需要关键字:

build:
  stage: build
  script:
    - echo "BUILD_VERSION=hello" >> build.env
  artifacts:
    reports:
      dotenv: build.env

deploy:
  stage: deploy
  script:
    - echo "$BUILD_VERSION"  # Output is: 'hello'
  dependencies:
    - build