# GitLab

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

## Prerequisites

* You have a [GitLab account](https://gitlab.com/users/sign_in).
* You have created one or more [CloudTruth Parameters](../parameters/parameter-management/managing-parameters).
* You have created a [CloudTruth API Access token](..#api-tokens).

## 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.

![](https://2952342643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MGjN2Xg1mE8iTvg49dw%2Fuploads%2Fgit-blob-ada31885151d877698d21dba4a31007ef9592b74%2FPrtScr%20capture%20\(5\).jpg?alt=media)

## 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](https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html#artifactsreportsdotenv). 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 artifacts.reports.dotenv
    - echo "FLASK_RUN_PORT=$(cloudtruth --project MyFirstProject --env default parameters get FLASK_RUN_PORT)" >> build.env
    - echo "FLASK_MESSAGE=$(cloudtruth --project MyFirstProject --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.

![](https://2952342643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MGjN2Xg1mE8iTvg49dw%2Fuploads%2Fgit-blob-cf605e7df45a2570991aaffa4d39afe7e11b4173%2Fimage%20\(13\)%20\(1\)%20\(1\).png?alt=media)

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 --project MyFirstProject --env default parameters get FLASK_RUN_PORT)" >> build.env
    - echo "FLASK_MESSAGE=$(cloudtruth --project MyFirstProject --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](https://docs.gitlab.com/ee/ci/variables/#pass-an-environment-variable-to-another-job). 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 --project MyFirstProject parameter export docker  >> build.env
    - sed -i '/^$/d' build.env
      
  # Passes environment variables between stages
  artifacts:
    reports:
      dotenv: build.env

Using CloudTruth Variables:
  stage: test
  script:
    - printenv

  dependencies:
    - "Install CloudTruth CLI"
```

{% hint style="danger" %}
Take caution building environment variables that contain secret values as they can be exposed in your pipeline. Parameter export redacts secret values by default.
{% endhint %}
