[英]Variable inside variable gitlab ci
Is there a way to use predefined variable inside custom variable in gitlab ci like this:有没有办法在 gitlab ci 的自定义变量中使用预定义变量,如下所示:
before_script:
- cat "${$CI_COMMIT_REF_NAME}" >> .env
to extract the name of branch from $CI_COMMIT_REF_NAME
and use it as a name of custom variable从
$CI_COMMIT_REF_NAME
中提取分支名称并将其用作自定义变量的名称
Update:更新:
Check out GitLab 14.3 (September 2021)查看GitLab 14.3 (2021 年 9 月)
Use variables in other variables
在其他变量中使用变量
CI/CD pipeline execution scenarios can depend on expanding variables declared in a pipeline or using GitLab predefined variables within another variable declaration.
CI/CD 管道执行场景可以依赖于扩展管道中声明的变量或在另一个变量声明中使用 GitLab 预定义变量。
In 14.3, we are enabling the “ variables inside other variables ” feature on GitLab SaaS.
在 14.3 中,我们在 GitLab SaaS 上启用了“其他变量中的变量”功能。
Now you can define a variable and use it in another variable definition within the same pipeline .
现在您可以定义一个变量并在同一管道内的另一个变量定义中使用它。
You can also use GitLab predefined variables inside of another variable declaration.
您还可以在另一个变量声明中使用 GitLab 预定义变量。
This feature simplifies your pipeline definition and eliminates pipeline management issues caused by the duplicating of variable data.
此功能简化了您的管道定义并消除了由重复可变数据引起的管道管理问题。
Note - for GitLab self-managed users the feature is disabled by default.
注意 - 对于 GitLab 自管理用户,该功能默认禁用。
To use this feature, your GitLab administrator will need to enable the feature flag .要使用此功能,您的 GitLab 管理员将需要启用功能标志。
( demo -- video )
(演示-视频)
See Documentation and Issue .
请参阅文档和问题。
dba
asks in the comments : dba
在评论中询问:
Does this include or exclude using globally defined variables?
这是否包括或排除使用全局定义的变量?
dba's own answer: dba自己的答案:
Global variables can be reused, but they need the
local_var: ${global_var}
syntax with recursive expansion (independent of the shell).全局变量可以重复使用,但它们需要具有递归扩展的
local_var: ${global_var}
语法(独立于 shell)。
Lots of options.很多选择。
.env
.env
image: busybox:latest
variables:
MY_CUSTOM_VARIABLE: $CI_JOB_STAGE
ANIMAL_TESTING: "cats"
before_script:
- echo "Before script section"
- echo $CI_JOB_STAGE
- echo $MY_CUSTOM_VARIABLE
- echo $MY_CUSTOM_VARIABLE >> .env
- echo $CI_COMMIT_BRANCH >> .env
- cat .env
$ echo "Before script section"
Before script section
$ echo $CI_JOB_STAGE
build
$ echo $MY_CUSTOM_VARIABLE
build
$ echo $MY_CUSTOM_VARIABLE >> .env
$ echo $CI_COMMIT_BRANCH >> .env
$ cat .env
build
exper/ci-var-into-env
$ echo "Do your build here"
Do your build here
image: busybox:latest
variables:
MY_CUSTOM_VARIABLE: "${CI_JOB_STAGE}"
ANIMAL_TESTING: "cats"
before_script:
- echo "Before script section"
- echo $CI_JOB_STAGE
- echo $MY_CUSTOM_VARIABLE
- echo $MY_CUSTOM_VARIABLE >> .env
- cat .env
example: https://gitlab.com/codeangler/make-ci-var-custom-var-in-script/-/blob/master/.gitlab-ci.yml示例: https://gitlab.com/codeangler/make-ci-var-custom-var-in-script/-/blob/master/.gitlab-ci.yml
Check if this matchesgitlab-org/gitlab-runner
issue 1809 :检查这是否与
gitlab-org/gitlab-runner
问题 1809匹配:
Description
描述
- In the
.gitlab-ci.yml
file, a user can define a variable and use it in another variable definition within the same.gitlab-ci.yml
file.在
.gitlab-ci.yml
文件中,用户可以定义一个变量并在同一个.gitlab-ci.yml
文件中的另一个变量定义中使用它。- A user can also use a GitLab pre-defined variable in a variable declaration.
用户还可以在变量声明中使用 GitLab 预定义变量。
Example
例子
variables: variable_1: "foo" # here, variable_1 is assigned the value foo variable_2: "${variable_1}" # variable_2 is assigned the value variable_1. # The expectation is that the value in variable_2 = value set for variable_1
If it is, it should be completed/implemented for GitLab 14.1 (July 2021)如果是,则应针对 GitLab 14.1(2021 年 7 月)完成/实施
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.