27

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
  • 5,413
  • 2
  • 30
  • 48
robliv
  • 1,351
  • 3
  • 15
  • 30
  • 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

20

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
  • 1,351
  • 3
  • 15
  • 30
  • It should be actually --build-arg VARIABLE=VARIABLE . So you have to give the variable a name :) – Florian Falk Oct 20 '22 at 13:41
  • @FlorianFalk my command is not working - docker build --build-arg CHECK_STAGE=${CHECK_STAGE} -t ${IMAGE}:latest -t ${IMAGE}:${VERSION}-${CI_COMMIT_SHORT_SHA} -t ${IMAGE}:${VERSION} . – Karan Khurana Apr 13 '23 at 11:51
  • @KaranKhurana If you need help, you must also give others the opportunity to understand your problem. Just a command with the hint that it doesn't work is a bit little ;) Also, I think comments are a wrong way to ask for help. I recommend you to open a new question. – Florian Falk Apr 18 '23 at 07:26
14

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
  • 5,413
  • 2
  • 30
  • 48
3

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

EDIT:

you can also pass - s .m2/settings.xml to the mvn command if you pushed the settings.xml to you branch (which I recommend to be honest)

Pwnstar
  • 2,333
  • 2
  • 29
  • 52
-1

You should try doing something like this:

ADD ${VARIABLE}/tmp

kooskoos
  • 4,622
  • 1
  • 12
  • 29
  • 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