通过 bash 命令设置 gitlab-ci.yml 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49026724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:50:51  来源:igfitidea点击:

Set gitlab-ci.yml variable via bash command

bashgitlab-ci

提问by Karl Morrison

variables:
  CUSTOM_NODE_VERSION: '$${cat .nvmrc}'

I'd like for the variable CUSTOM_NODE_VERSIONto be populated via the contents of the .nvmrcfile (which is located in the projects root directory). How does one do this in the gitlab-ci.ymlfile?

我希望CUSTOM_NODE_VERSION通过.nvmrc文件的内容(位于项目根目录中)填充变量。如何在gitlab-ci.yml文件中做到这一点?

The example above isn't working. I've also tried the following:

上面的例子不起作用。我还尝试了以下方法:

  • CUSTOM_NODE_VERSION: $(cat .nvmrc)-> (cat .nvmrc)
  • CUSTOM_NODE_VERSION: "$(cat .nvmrc)"-> (cat .nvmrc)
  • CUSTOM_NODE_VERSION: '$(cat .nvmrc)'-> (cat .nvmrc)
  • CUSTOM_NODE_VERSION: ${cat .nvmrc}-> (empty string)
  • CUSTOM_NODE_VERSION: '${cat .nvmrc}'-> (empty string)
  • CUSTOM_NODE_VERSION: "${cat .nvmrc}"-> (empty string)
  • CUSTOM_NODE_VERSION: $(cat .nvmrc)-> (cat .nvmrc)
  • CUSTOM_NODE_VERSION: "$(cat .nvmrc)"-> (cat .nvmrc)
  • CUSTOM_NODE_VERSION: '$(cat .nvmrc)'-> (cat .nvmrc)
  • CUSTOM_NODE_VERSION: ${cat .nvmrc}->(空字符串)
  • CUSTOM_NODE_VERSION: '${cat .nvmrc}'->(空字符串)
  • CUSTOM_NODE_VERSION: "${cat .nvmrc}"->(空字符串)

It works if I put it in the before_scriptlike the following:

如果我将其放入如下before_script所示,它会起作用:

before_script:
  - CUSTOM_NODE_VERSION=$(cat .nvmrc)

But it isn't accessible to the following part of the gitlab-ci.ymlfile:

但是gitlab-ci.yml文件的以下部分无法访问它:

lint:
  stage: Test
  image: node:$CUSTOM_NODE_VERSION

回答by Stefan van Gastel

There are some parts of .gitlab-ci.ymlwhere variables are usable and some parts where they are not.

还有一些地方.gitlab-ci.yml,其中的变量是可用的和一些地方,他们不是。

The .ymlfile is parsed in Gitlab itself and then the commands are executed by the runner. So setting a variable that is used in the job config is not possible at this point. You could use a pre-defined Secret Variable although that doesnot seem to fix your need.

.yml文件在 Gitlab 本身中解析,然后命令由运行程序执行。因此,此时无法设置作业配置中使用的变量。您可以使用预定义的秘密变量,尽管这似乎不能满足您的需求。

There are issues tracking the documentation of what you can and cannot do:

跟踪您可以做什么和不能做什么的文档存在问题: