Bear
Bear

Reputation: 51

How to pass GitLab CI file variable to docker container?

This question is a little like this question. How to pass GitLab CI file variable to Dockerfile and docker container?

I used the most voted answer however I failed.

The below is what I want to do.

I set up the gitlab ci variable called PIP_CONFIG with file type. and the value is like

[global]
timeout = 60

in .gitlab-ci.yml

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

in Dockerfile

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

And then I got an error

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
...

It seems like it only wrote the path of temp file rather than the content of file.

I also try use COPY $PIP_CONFIG ~/.pip/pip.conf but it said the path is not exsist in /builds/xxx/xxx/project_name.tmp/PIPCONFIG.

Could someone tell me what should I do correctly? Thanks.

PS: The reason why I do not write the config directly in repository and jus use COPY from repo is that there is some sensitive token in pip config.

Upvotes: 2

Views: 2364

Answers (1)

Bear
Bear

Reputation: 51

After some try, I understand that just use type 'variable' in the gitlab ci setting.

And then pass the value with quote("$VARABLE") for maybe you have the space or break line in your value.

like this in .gitlab-ci.yml

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

Remember add quote for Dockerfile, too.

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

And then, I can do what I want, write a pip config to image from gitlab ci variable.

Hope can help others.

Upvotes: 1

Related Questions