GitLab - Retrieve CI/CD Variable With BASH
I'm working on a project that requires that the server update itself when it comes online, rather than me having the GitLab pipelines push updates to it when they execute. This was easily achieved by having the server just pull the latest Docker image when it boots up. However, I need to make sure it has the latest settings file(s), which presented me with a problem. How am I going to have the server automatically retrieve these file(s), which I usually keep as values in the GitLab pipeline variables?
Luckily, this is easily resolved, by simply having the server use the same project access token it uses for pulling the Docker image, to pull the GitLab variable's value, using a simple BASH script provided below.
Steps
Create a project access token with the read_api
permission if you haven't
created this already.
Copy and paste the following script, and fill in the variables, in order to have it download the value into a file.
# Specify the name of the variable you wish to retrieve the value for.
VARIABLE_NAME="MY_VARIABLE_NAME"
# Specify the name of the environment of the variable
ENVIRONMENT="production"
# Provide your project access token for authentication.
TOKEN="xxxxx-XXXXXXXXXXXXXXXXXXXX"
# Specify the URL of your GitLab instance.
GITLAB_URL="https://gitlab.irap.org"
# Specify the ID of the project, which you can get by going to
# settings > general in the repository in GitLab
PROJECT_ID="100"
# Specify the name you wish to put the variable's value into.
OUTPUT_FILENAME="output.txt"
curl \
--silent \
--show-error \
--globoff \
--header "PRIVATE-TOKEN: $TOKEN" \
"$GITLAB_URL/api/v4/projects/$PROJECT_ID/variables/$VARIABLE_NAME?filter[environment_scope]=$ENVIRONMENT" \
| jq '.value' --raw-output > $OUTPUT_FILENAME
Alternate Version - No Environment
This script above assumes that you have multiple variables with the same name, one for each environment. If you don't make use of environments, and there is only one variable with the provided name, then you can use the following simpler script:
# Specify the name of the variable you wish to retrieve the value for.
VARIABLE_NAME="MY_VARIABLE_NAME"
# Provide your project access token for authentication.
TOKEN="xxxxx-XXXXXXXXXXXXXXXXXXXX"
# Specify the URL of your GitLab instance.
GITLAB_URL="https://gitlab.irap.org"
# Specify the ID of the project, which you can get by going to
# settings > general in the repository in GitLab
PROJECT_ID="100"
# Specify the name you wish to put the variable's value into.
OUTPUT_FILENAME="output.txt"
curl \
--silent \
--show-error \
--globoff \
--header "PRIVATE-TOKEN: $TOKEN" \
"$GITLAB_URL/api/v4/projects/$PROJECT_ID/variables/$VARIABLE_NAME" \
| jq '.value' --raw-output > $OUTPUT_FILENAME
First published: 4th December 2024