18

GitLab CI allows adding custom variables to a project.

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.

Ultimately I need to copy this file to a Docker container that is created when building the project. (docker build ... in the yml)

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.

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.

Nicolas Pepinster
  • 4,119
  • 23
  • 44
robliv
  • 991
  • 2
  • 8
  • 24
  • This is answered better here https://stackoverflow.com/questions/40229182/simplest-way-of-passing-all-host-environment-variables-to-docker-container – Peeter Kokk Aug 20 '21 at 04:24

4 Answers4

16

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.

robliv
  • 991
  • 2
  • 8
  • 24
11

Unfortunately, it's not possible like this because the file from CI/CD variable are copied at build time into a tmp directory ($CI_PROJECT_DIR.tmp) which is not in the docker build context. However, ADD need files present in the build context as documented

A workaround could be to copy the content of file in the current directory (supposing the Dockerfile is in ${CI_PROJECT_DIR}) before the docker build command :

cat $VARIABLE > ${CI_PROJECT_DIR}\mynewfile

and refer the the file in the Dockerfile :

ADD mynewfile /tmp/
Nicolas Pepinster
  • 4,119
  • 23
  • 44
0

I made a similar thing with the maven settings:

before_script:
  - mkdir -p ${CI_PROJECT_DIR}/.m2/
  - cp $M2_SETTINGS ${CI_PROJECT_DIR}/.m2/settings.xml && chmod 600 ${CI_PROJECT_DIR}/.m2/settings.xml
Pwnstar
  • 2,398
  • 2
  • 24
  • 44
-1

You should try doing something like this:

ADD ${VARIABLE}/tmp

kooskoos
  • 4,168
  • 1
  • 10
  • 22
  • 1
    This is not exact, like @robliv said `--build-arg` and `ARG` are necessary. Check this [anwser](https://stackoverflow.com/a/34600106/2653911). – Nicolas Pepinster Nov 22 '19 at 14:30