GitLab
This walkthrough shows you how to use CloudTruth parameters with GitLab CI /CD jobs.

Prerequisites

Provide GitLab Access

Add your generated CloudTruth API Access token as a GitLab variable called CLOUDTRUTH_API_KEY in your project settings. This allows the GitLab project to securely access parameters stored in CloudTruth.

Configure a CI/CD pipeline with CloudTruth Parameters

Install the CloudTruth CLI with a Build Stage

You can install the CLI with the following code snippet in a GitLab build stage. This will install the latest supported version and also verify the CLI is installed correctly by printing the version.
1
Install CloudTruth CLI:
2
stage: build
3
script:
4
- curl -sL https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh | sh
5
- cloudtruth -V
Copied!

Passing CloudTruth parameters to GitLab test stages

In the build stage, we can access and pass variables to GitLab with artifacts. This snippet will add environment variables FLASK RUN PORT and FLASK_MESSAGE to build.env with values pulled from CloudTruth.
1
# Adds CloudTruth Values to build.env file to be passed with aritifacts.reports.dotenv
2
- echo "FLASK_RUN_PORT=$(cloudtruth --project MyFirstProject --env default parameters get FLASK_RUN_PORT)" >> build.env
3
- echo "FLASK_MESSAGE=$(cloudtruth --project MyFirstProject --env default parameters get FLASK_MESSAGE)" >> build.env
4
5
# Passes environment variables between stages
6
artifacts:
7
reports:
8
dotenv: build.env
Copied!

Using CloudTruth parameters in test stages

Now you can reference the variables in downstream stages.
1
Using CloudTruth Variables:
2
stage: test
3
script:
4
- echo $FLASK_RUN_PORT
5
- echo $FLASK_MESSAGE
6
dependencies:
7
- "Install CloudTruth CLI"
Copied!
Running this pipeline will install the CloudTruth CLI and print the parameter values.
The complete .gitlab-ci.yml
1
Install CloudTruth CLI:
2
stage: build
3
script:
4
- curl -sL https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh | sh
5
- cloudtruth -V
6
7
# Adds CloudTruth Values to build.env file to be passed with artifacts.reports.dotenv
8
- echo "FLASK_RUN_PORT=$(cloudtruth --project MyFirstProject --env default parameters get FLASK_RUN_PORT)" >> build.env
9
- echo "FLASK_MESSAGE=$(cloudtruth --project MyFirstProject --env default parameters get FLASK_MESSAGE)" >> build.env
10
11
# Passes environment variables between stages
12
artifacts:
13
reports:
14
dotenv: build.env
15
16
Using CloudTruth Variables:
17
stage: test
18
script:
19
- echo $FLASK_RUN_PORT
20
- echo $FLASK_MESSAGE
21
dependencies:
22
- "Install CloudTruth CLI"
Copied!

Parameter export

Alternatively you can use the CloudTruth parameter export command to create a GitLab .env file. GitLab pipelines will fail to upload a .env file if it has a newline at the end of the file. We will use a sed command in the job to strip the export of empty lines.
sed -i '/^$/d' build.env
1
Install CloudTruth CLI:
2
stage: build
3
script:
4
- curl -sL https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh | sh
5
- cloudtruth -V
6
7
8
# Adds CloudTruth Values to build.env file to be passed with artifacts.reports.dotenv
9
- cloudtruth --project MyFirstProject parameter export docker >> build.env
10
- sed -i '/^$/d' build.env
11
12
# Passes environement variables between stages
13
artifacts:
14
reports:
15
dotenv: build.env
16
17
Using CloudTruth Variables:
18
stage: test
19
script:
20
- printenv
21
22
dependencies:
23
- "Install CloudTruth CLI"
Copied!
Take caution building environment variables that contain secret values as they can be exposed in your pipeline. Parameter export redacts secret values by default.
Last modified 2mo ago