我想使用分支名称作为标记为docker image name定义变量,如文档https://docs.gitlab.com/ce/ci/docker/using_docker_build.html中所定义:
variables:
CONTAINER_NAME: myimage:$CI_COMMIT_REF_NAME
CONTAINER_PUSH_NAME: myrepo:5000/$CONTAINER_NAME
然而,当我输出CONTAINER_PUSH_NAME时,我得到:
myrepo:5000 / MYIMAGE:$ CI_COMMIT_REF_NAME
这意味着,我自己的变量已经扩展,但预定义的变量没有,尽管它们在脚本执行时可用,因为当我echo $CI_COMMIT_REF_NAME
时,我得到分支名称,如预期的那样。
是否可以将预定义变量用作我定义的变量的一部分,或者文档是误导性的?
答案 0 :(得分:2)
回答你的问题:不,你把一个替换“嵌套”(嵌套)到另一个替换的方式......
...但是有一些干净的解决方法(我应该说解决方案)
请考虑以下事项:
.gitlab-ci.yml
image: ubuntu
variables:
# Original
CONTAINER_NAME: myimage:$CI_COMMIT_REF_NAME
CONTAINER_PUSH_NAME: myrepo:5000/$CONTAINER_NAME
#
# Added to look for a workaround
CONTAINER_NAME_PREFIX: 'myimage:'
CONTAINER_PUSH_PREFIX: myrepo:5000/
CONTAINER_PUSH_NAME_V2: $CONTAINER_PUSH_PREFIX$CONTAINER_NAME_PREFIX$CI_COMMIT_REF_NAME
#
# Suggested workaround
MY_IMAGE: myimage
MY_REPO: myrepo:5000
SUGGESTED_CONTAINER_NAME: $MY_IMAGE:$CI_COMMIT_REF_NAME
SUGGESTED_CONTAINER_PUSH_NAME: $MY_REPO/$MY_IMAGE:$CI_COMMIT_REF_NAME
test:
script:
# Just the relevant variables:
- echo '*** Just the variables:'
- echo $CI_COMMIT_REF_NAME
- echo $CONTAINER_NAME
- echo $CONTAINER_PUSH_NAME
- echo $CONTAINER_PUSH_NAME_V2
# Combinations of text and variables
- echo '*** Combinations of text and variables:'
- echo myrepo:5000/$CONTAINER_NAME
- echo 'myrepo:5000/$CONTAINER_NAME'
- echo "myrepo:5000/$CONTAINER_NAME"
- echo $CONTAINER_PUSH_PREFIX$CONTAINER_NAME_PREFIX$CI_COMMIT_REF_NAME
# Suggested workarounds:
- echo '*** Suggested workarounds:'
- echo $SUGGESTED_CONTAINER_NAME
- echo $SUGGESTED_CONTAINER_PUSH_NAME
生成此输出:
Running with gitlab-ci-multi-runner 9.1.0 (0118d89)
on docker-auto-scale (e11ae361)
Using Docker executor with image ubuntu ...
Using docker image sha256:a9db1f09b71a10fe42d8ac456c24d961dccd06968e72d2f476df20a12cb41f3d for predefined container...
Pulling docker image ubuntu ...
Using docker image ubuntu ID=sha256:ebcd9d4fca80e9e8afc525d8a38e7c56825dfb4a220ed77156f9fb13b14d4ab7 for build container...
Running on runner-e11ae361-project-3324498-concurrent-0 via runner-e11ae361-machine-1495045360-f78e2b0f-digital-ocean-2gb...
Cloning repository...
Cloning into '/builds/elingerojo/CI-sandbox'...
Checking out 09b782b9 as master...
Skipping Git submodules setup
$ echo '*** Just the variables:'
*** Just the variables:
$ echo $CI_COMMIT_REF_NAME
master
$ echo $CONTAINER_NAME
myimage:master
$ echo $CONTAINER_PUSH_NAME
myrepo:5000/myimage:$CI_COMMIT_REF_NAME
$ echo $CONTAINER_PUSH_NAME_V2
myrepo:5000/myimage:master
$ echo '*** Combinations of text and variables:'
*** Combinations of text and variables:
$ echo myrepo:5000/$CONTAINER_NAME
myrepo:5000/myimage:master
$ echo 'myrepo:5000/$CONTAINER_NAME'
myrepo:5000/$CONTAINER_NAME
$ echo "myrepo:5000/$CONTAINER_NAME"
myrepo:5000/myimage:master
$ echo $CONTAINER_PUSH_PREFIX$CONTAINER_NAME_PREFIX$CI_COMMIT_REF_NAME
myrepo:5000/myimage:master
$ echo '*** Suggested workarounds:'
*** Suggested workarounds:
$ echo $SUGGESTED_CONTAINER_NAME
myimage:master
$ echo $SUGGESTED_CONTAINER_PUSH_NAME
myrepo:5000/myimage:master
Job succeeded
有5个正确的输出。选择一个适合你风格的:)
旁注:
“原始”问题在于为每个符号分配多个替换文本级别(符号是作业的左侧,ex CONTAINER_NAME
)。意思是,你不能像CONTAINER_PUSH_NAME
中那样“依赖”$CI_COMMIT_REF_NAME
通过符号CONTAINER_NAME
进行更深层次(嵌套)替换的“嵌套”替换。
只要不嵌套
,您就可以同时使用多个替换image: ubuntu
variables:
# Just two lines, all info at top of the script
CONTAINER_NAME_PREFIX: myimage
CONTAINER_PUSH_PREFIX: myrepo:5000
# Two auxiliary vars
CONTAINER_NAME: $CONTAINER_NAME_PREFIX:$CI_COMMIT_REF_NAME
CONTAINER_PUSH_NAME: $CONTAINER_PUSH_PREFIX/$CONTAINER_NAME_PREFIX:$CI_COMMIT_REF_NAME
test:
script:
- echo $CONTAINER_NAME
- echo $CONTAINER_PUSH_NAME
输出:
$ echo $CONTAINER_NAME
myimage:master
$ echo $CONTAINER_PUSH_NAME
myrepo:5000/myimage:master
image: ubuntu
variables:
CONTAINER_NAME: myimage:$CI_COMMIT_REF_NAME
CONTAINER_PUSH_PREFIX: myrepo:5000
test:
script:
# As bash value with double quotes
# (could be assigned to an environment variable)
- echo "myrepo:5000/$CONTAINER_NAME"
# Or used directly in any command without quotes
- echo $CONTAINER_PUSH_PREFIX/$CONTAINER_NAME
输出:
$ echo "myrepo:5000/$CONTAINER_NAME"
myrepo:5000/myimage:master
$ echo $CONTAINER_PUSH_PREFIX/$CONTAINER_NAME
myrepo:5000/myimage:master
答案 1 :(得分:0)
检查 GitLab 13.10(2021 年 3 月,4 年后)是否可以帮助您解决问题:
<块引用>有时您需要在 .gitlab-ci.yml
文件中定义一个变量并在另一个变量定义中使用它。
我们正在引入新的嵌套变量扩展功能。
启用后,GitLab 会在将最终输出发送到运行器以执行作业之前递归地对作业变量进行排序和扩展。
这种特定的排序允许变量以正确解析的顺序从 GitLab CI/CD 发送到运行器。
现在,您可以轻松地在 .gitlab-ci.yml
文件的其他变量中使用变量。
参见 Documentation 和 Issue。