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.
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.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With