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

查看:1024
本文介绍了如何将值从提交传递到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?

推荐答案

您未指定此值的来源.

您说将值从提交传递给..."
如果是有关提交本身的一些元信息,请查看预定义环境变量的列表
有很多名为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.

此刻的解决方法是使用工件-文件在阶段之间的作业之间传递信息.
我们的用例是从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天全站免登陆