如何将值从提交传递到 GitLab CI 管道作为变量? [英] how to pass value from commit to GitLab CI pipeline as variable?

查看:33
本文介绍了如何将值从提交传递到 GitLab CI 管道作为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态地将值传递给 GitLab CI 管道,以进一步将值传递给作业.问题是:值不能存储在代码中,不需要重新配置管道(例如,我可以在 .gitlab-ci.yml 的变量"部分传递值,但这意味着存储值在代码中,或CI/CD 设置"的环境变量"部分的更改意味着手动重新配置).此外,分支名称也不能用于此目的.

I need to dynamically pass value to GitLab CI pipeline to pass the value further to jobs. The problem is: the value cannot be stored in the code and no pipeline reconfiguration should be needed (e.g. I can pass the value in "variables" section of .gitlab-ci.yml but it means store value in the code, or changes in "Environment variables" section of "CI / CD Settings" means manual reconfiguration). Also, branch name cannot be used for that purpose too.

它不是一个秘密字符串,而是一个修改管道执行的关键字.那么,我该怎么做呢?

It is not a secret string but a keyword which modifies pipeline execution. So, how can I do it?

推荐答案

你没有指定这个值的来源.

You didn't specify the source of this value.

您说将值从提交传递到..."
如果是关于提交本身的一些元信息,请查看 预定义环境变量的列表
有很多名为 CI_COMMIT_* 的变量可能对你有用.

You say "pass value from commit to ..."
If it's some meta information about the commit itself, look at the list of Predefined environment variables
There's quite a lot of vars named CI_COMMIT_* which might work for you.

然而,如果这是您在一个工作的管道中产生的一些价值,并且想要传递给另一个工作 - 这是另一种情况.在作业之间传递变量的长期请求,它还没有实现.

However, if it's some value that you generate in the pipeline in one job and want to pass to another job - it's a different case. There is a long-living request to Pass variables between jobs, which is still not implemented.

目前的解决方法是使用 artifacts - 文件在不同阶段的作业之间传递信息.
我们的用例是从 pom.xml 中提取 Java 应用程序版本,然后将其传递给一些打包作业.
以下是我们在 .gitlab-ci.yml 中的做法:

The workaround for this moment is to use artifacts - files to pass information between jobs in stages.
Our use case is to extract Java app version from pom.xml and pass it to some packaging job later.
Here is how we do it in our .gitlab-ci.yml:

...
variables:
  VARIABLES_FILE: ./variables.txt  # "." is required for image that have sh not bash

...

get-version:
  stage: prepare
  image: ...
  script:
    - APP_VERSION=...
    - echo "export APP_VERSION=$APP_VERSION" > $VARIABLES_FILE
  artifacts:
    paths:
      - $VARIABLES_FILE
...
package:
  stage: package
  image: ...
  script:
    - source $VARIABLES_FILE
    - echo "Use env var APP_VERSION here as you like ..."

这篇关于如何将值从提交传递到 GitLab CI 管道作为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆