Managing and Storing Data, Variables, and Secrets
GitLab Variables with Multi-line Values
In the previous section, we saw that using file variables is a good way to store multiple secrets in a single variable since we can source the file and export all the secrets at once to the environment where our job is running. However, as said, it's not possible to simply paste the following content in a GitLab file variable because it contains new lines:
SLACK_WEBHOOK="https://hooks.slack.com/services/xxxxx/yyyyy/zzzzz"
DATABASE_URL="postgresql://user:password@hostname:port/database"
API_KEY="abcdef1234567890"
To work around this limitation, we can encode the content of the file using Base64 encoding. This encoding is commonly used to encode binary data into ASCII characters but can also be used to encode multi-line text into a single line.
Here's how Base64 works:
You have a file or a content with multi-line text:
cat </tmp/test.txt
This is a file
with multiple lines
white spaces and special characters: !@#$%^&*()_+
EOF
You can encode it using the base64 command:
base64 -w0 /tmp/test.txt
ℹ️ The
-w0option is used to prevent line wrapping in the output.
The output will be a single line of text: no new lines or special characters: (except for +, /, and = which are part of the Base64 character set):
VGhpcyBpcyBhIGZpbGUKd2l0aCBtdWx0aXBsZSBsaW5lcwp3aGl0ZSBzcGFjZXMgYW5kIHNwZWNpYWwgY2hhcmFjdGVyczogIUAjJCVeJiooKV8rCg==
A consumer of this encoded content can decode it back to its original form using the base64 --decode command:
echo "VGhpcyBpcyBhIGZpbGUKd2l0aCBtdWx0aXBsZSBsaW5lcwp3aGl0ZSBzcGFjZXMgYW5kIHNwZWNpYWwgY2hhcmFjdGVyczogIUAjJCVeJiooKV8rCg==" | \
base64 --decode
The output will be the original content:
This is a file
with multiple lines
white spaces and special characters: !@#$%^&*()_+
Cloud Native CI/CD with GitLab
From Commit to Production ReadyEnroll now to unlock all content and receive all future updates for free.
