【问题标题】:How to pass GitLab CI file variable to docker container?如何将 GitLab CI 文件变量传递给 docker 容器?
【发布时间】:2021-08-09 14:06:02
【问题描述】:

这个问题有点像这个问题。 How to pass GitLab CI file variable to Dockerfile and docker container?

我使用了投票最多的答案,但是我失败了。

下面是我想做的。

我使用 file 类型设置了名为 PIP_CONFIG 的 gitlab ci 变量。 价值就像

[global]
timeout = 60

.gitlab-ci.yml

 ...
 - docker build -t $IMAGe
   --build-arg PIP_CONFIG=$PIP_CONFIG
   src/.
...

Dockerfile

FROM python:3
ARG PIP_CONFIG
RUN mkdir ~/.pip
RUN echo $PIP_CONFIG > ~/.pip/pip.conf
...
...
RUN pip install -r requirements.txt

然后我得到一个错误

Configuration file could not be loaded.
File contains no section headers.
file: '/root/.pip/pip.conf', line: 1
'/builds/xxx/xxx/project_name.tmp/PIPCONFIG\n'   <<<< this line
...

好像只写了临时文件的路径而不是文件的内容。

我也尝试使用COPY $PIP_CONFIG ~/.pip/pip.conf,但它说/builds/xxx/xxx/project_name.tmp/PIPCONFIG中不存在路径。

有人能告诉我我应该怎么做吗?谢谢。

PS:我之所以不直接在存储库中编写配置,而是使用来自 repo 的COPY,是因为 pip config 中有一些敏感令牌。

【问题讨论】:

    标签: docker pip gitlab gitlab-ci


    【解决方案1】:

    经过一番尝试,我知道在 gitlab ci 设置中只使用类型“变量”。

    然后使用 quote("$VARABLE") 传递值,因为您的值中可能有空格或换行符。

    像这样 在.gitlab-ci.yml

     ...
     - docker build -t $IMAGe
       --build-arg PIP_CONFIG="$PIP_CONFIG"
       src/.
    ...
    

    记得添加 Dockerfile 的引号。

    FROM python:3
    ARG PIP_CONFIG
    RUN mkdir ~/.pip
    RUN echo "$PIP_CONFIG" > ~/.pip/pip.conf
    ...
    ...
    RUN pip install -r requirements.txt
    

    然后,我可以做我想做的事,编写一个 pip 配置以从 gitlab ci 变量中获取图像。

    希望可以帮助他人。

    【讨论】:

      猜你喜欢
      • 2020-03-15
      • 2019-11-04
      • 2019-08-05
      • 1970-01-01
      • 2015-08-10
      • 2017-02-19
      • 2021-11-11
      • 2021-04-17
      • 2015-09-18
      相关资源
      最近更新 更多