[Gitlab] File type CI/CD variable
A fledgling engineer dabbling into areas of DevOps, AWS and automation. I enjoy tinkering with technology frameworks and tools to understand and gain visibility in the underlying mechanisms of the "magic" in them.
In the progress of accumulating nuggets of wisdom in the different software engineering disciplines!
TLDR; Summary
Using CI/CD variables with File type allows you to define multiple variables without having to define them individually. This makes it easier to maintain and more portable.
The CI/CD variable defined will store the path to the variable file store under a temporary directory. The path naming convention will be:
/builds/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}.tmp/<VARIABLE_NAME>You can indicate for the variable values to be interpreted as just a raw string/JSON or to allow variable reference expansion.
- By allowing variable reference expansion, you can add variables inside a JSON
Use
$$if you don't want the $ character to be interpreted as the start of another variableEnsure you have defined your JSON correctly.
- You may face the following error
parse error: Expended another key-value keyif you accidentally included an extra comma or didn't close the JSON properly.
- You may face the following error
Create CI/CD file type variable
Typically, we will create a variable with the type as variable to store single value.
Using the file type CI/CD variable, you can define in JSON format or raw string with variable reference expansion capability.

In this case, you can see that I have included another CI/CD variable $TestStandaloneCICDValue which contains a random string.

Usage in pipeline
Without Expand variable reference option, the value will be evaluated as a literal string. Nothing's special, so we will jump straight to using the feature of Expand variable reference!
File Type Variables
In the pipeline, the CI/CD variables will contain the path to the variable file.
The naming convention will be /builds/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}.tmp/<VARIABLE_NAME>
Example:
The project namespace is sample-project-group
The project name is learn-gitlab-variables
The variable name is CI_CD_FILE
The file path will be:
/builds/sample-project-group/learn-gitlab-variables.tmp/CI_CD_FILE
Variable Reference Expansion
JSON Format with Dynamic Values
As seen above, I have defined the following JSON inside theCI_CD_FILE variable.

When running the pipeline, you will observe the following output (Ignoring the next line of command π¬).

For escaping, add an extra $ in front. E.g. $$NotAVariable if you want it to be interpreted as a literal string. When printing, it will show '$NotAVariable' as shown in the screenshot below.

Variable Definition File
This approach is something similar to how Gitlab passes environment variables to another job.
Instead of writing in JSON format, you can simply define your variables in KEY=VALUE format.

There is a need to load the variable file explicitly (e.g. source <variable_file> OR . <variable_file> to use the variables.
Loading of runtime variables
If you use the variable definition file format, you can load the values from variables defined at runtime.
- You will be unable to do it for JSON format since those values must be loaded or "replaced" when creating the variable file for the pipeline usage

Final thoughts
After noting down my small takeaway from experimenting with the file type CI/CD variables, I realize there is a section covering it π€¦π»ββοΈ Lesson noted on taking the time to read through documentation fully π
In hindsight, I am glad I try it out myself anyway. I don't think I would have been able to appreciate it just by reading through the information provided in the documentation π«
Seeing is believing - Hope this will give you insights on what to expect when using file type CI/CD variables ππΌ
Cheers!
![[DH] Keeping an organised inventory of annotated screenshots](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1731688657857%2Fcaeeeb6f-1530-4c88-a820-d22deaf0391e.png&w=3840&q=75)



