Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass GitLab CI file variable to Dockerfile and docker container?

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.

like image 547
robliv Avatar asked Nov 19 '19 17:11

robliv


People also ask

Can you pass variables to Dockerfile?

Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs. Let's see how we can use it to pass a value to our greetings script. There are two different ways to do it.

Does GitLab CI use Dockerfile?

Overview. With GitLab CI, it is possible to build a Docker image from a Dockerfile kept in a GitLab repository and upload it to the GitLab registry (default case) or to any other Docker registry.

How do I add a CI CD variable in GitLab?

In GitLab, CI/CD variables can be defined by going to Settings » CI/CD » Variables, or by simply defining them in the . gitlab-ci. yml file. Variables are useful for configuring third-party services for different environments, such as testing , staging , production , etc.


2 Answers

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.

like image 70
robliv Avatar answered Oct 15 '22 02:10

robliv


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/
like image 25
Nicolas Pepinster Avatar answered Oct 15 '22 02:10

Nicolas Pepinster