32

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.

1

4 Answers 4

27

Had to use .yml file docker build argument --build-arg VARIABLE_NAME=variable_value and in Dockerfile use ARG VARIABLE_NAME so the Dockerfile knows it needs to use variable from environment.

Sign up to request clarification or add additional context in comments.

3 Comments

It should be actually --build-arg VARIABLE=VARIABLE . So you have to give the variable a name :)
@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} .
@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.
18

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/

1 Comment

thx! cp $VARIABLE ${CI_PROJECT_DIR}\mynewfile also works, instead of cat
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)

Comments

-2

You should try doing something like this:

ADD ${VARIABLE}/tmp

1 Comment

This is not exact, like @robliv said --build-arg and ARG are necessary. Check this anwser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.