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.

Install CloudTruth CLI:
stage: build
script:
- curl -sL https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh | sh
- cloudtruth -V

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.

# Adds CloudTruth Values to build.env file to be passed with aritifacts.reports.dotenv
- echo "FLASK_RUN_PORT=$(cloudtruth --env default parameters get FLASK_RUN_PORT)" >> build.env
- echo "FLASK_MESSAGE=$(cloudtruth --env default parameters get FLASK_MESSAGE)" >> build.env
# Passes environment variables between stages
artifacts:
reports:
dotenv: build.env

Using CloudTruth parameters in test stages

Now you can reference the variables in downstream stages.

Using CloudTruth Variables:
stage: test
script:
- echo $FLASK_RUN_PORT
- echo $FLASK_MESSAGE
dependencies:
- "Install CloudTruth CLI"

Running this pipeline will install the CloudTruth CLI and print the parameter values.

The complete .gitlab-ci.yml

Install CloudTruth CLI:
stage: build
script:
- curl -sL https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh | sh
- cloudtruth -V
# Adds CloudTruth Values to build.env file to be passed with artifacts.reports.dotenv
- echo "FLASK_RUN_PORT=$(cloudtruth --env default parameters get FLASK_RUN_PORT)" >> build.env
- echo "FLASK_MESSAGE=$(cloudtruth --env default parameters get FLASK_MESSAGE)" >> build.env
# Passes environment variables between stages
artifacts:
reports:
dotenv: build.env
Using CloudTruth Variables:
stage: test
script:
- echo $FLASK_RUN_PORT
- echo $FLASK_MESSAGE
dependencies:
- "Install CloudTruth CLI"

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

Install CloudTruth CLI:
stage: build
script:
- curl -sL https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh | sh
- cloudtruth -V
# Adds CloudTruth Values to build.env file to be passed with artifacts.reports.dotenv
- cloudtruth parameter export docker >> build.env
- sed -i '/^$/d' build.env
# Passes environement variables between stages
artifacts:
reports:
dotenv: build.env
Using CloudTruth Variables:
stage: test
script:
- printenv
dependencies:
- "Install CloudTruth CLI"

Take caution building environment variables that contain secret values as they can be exposed in your pipeline. Parameter export redacts secret values by default.