如何将 GitLab CI 文件变量传递给 Dockerfile 和 docker 容器? [英] How to pass GitLab CI file variable to Dockerfile and docker container?
问题描述
GitLab CI 允许向项目添加自定义变量.
GitLab CI allows adding custom variables to a project.
它允许使用类型为 file
的秘密变量,其中我指定一个 Key 是变量名称和 Value 是文件的内容(例如证书的内容)
It allows to use a secret variable of type file
where I specify a Key that is the variable name and Value that is the content of a file(e.g. content of certificate)
然后在管道执行过程中,内容将被保存为临时文件,调用变量名将返回创建文件的路径.
Then during execution of the pipeline the content will be saved as a temporary file and calling the variable name will return the path to the created file.
最终我需要将此文件复制到构建项目时创建的 Docker 容器中.(docker build ...
在 yml)
Ultimately I need to copy this file to a Docker container that is created when building the project. (docker build ...
in the yml)
在测试变量是否有效时,我尝试了 .gitlab-ci.yml
中的 echo $VARIABLE
并且它有效,返回临时文件的路径.但是在 Dockerfile
中执行 RUN echo $VARIABLE
时,它是空的.因此我也不能使用 ADD $VARIABLE/tmp/
这是我的目标.
When testing if the variable works, I tried echo $VARIABLE
in .gitlab-ci.yml
and it works, returns path of temp file. But when doing RUN echo $VARIABLE
in the Dockerfile
, it is empty. Therefore I also cannot use ADD $VARIABLE /tmp/
which is my goal.
有没有办法解决这个问题并使 Dockerfile
可以使用这个文件?我是 Docker 和 GitLab 的新手,不知道还能去哪里看.
Is there a way to solve this and make this file available to the Dockerfile
? I am new to Docker and GitLab and not sure where else to look.
推荐答案
必须使用 .yml 文件 docker build
参数 --build-arg VARIABLE
并在 Dockerfile 中使用ARG VARIABLE
以便 Dockerfile 知道它需要使用环境变量.
Had to use .yml file docker build
argument --build-arg VARIABLE
and in Dockerfile use ARG VARIABLE
so the Dockerfile knows it needs to use variable from environment.
这篇关于如何将 GitLab CI 文件变量传递给 Dockerfile 和 docker 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!