# Bitbucket Pipelines

## Prerequisites

* You have a [Bitbucket account](https://id.atlassian.com/login?application=bitbucket\&continue=https%3A%2F%2Fbitbucket.org%2Faccount%2Fsignin%2F%3FredirectCount%3D1%26next%3D%252F).
* You have created one or more [CloudTruth Parameters](https://docs.cloudtruth.com/configuration-management/parameters/parameter-management/managing-parameters).
* You have created a [CloudTruth API Access token](https://docs.cloudtruth.com/configuration-management/integrations/..#api-tokens).

## Provide Bitbucket Pipelines CloudTruth Access

You can get started with Bitbucket using a [starter pipeline template](https://support.atlassian.com/bitbucket-cloud/docs/get-started-with-bitbucket-pipelines/).

Add your generated CloudTruth API access token as a [Bitbucket repository variable](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/) named`CLOUDTRUTH_API_KEY` in your **Repository settings** > ***Pipelines*** > **Repository variables**. This allows the Bitbucket repository to securely access parameters stored in CloudTruth across all deployment environments.

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

## Configure a Bitbucket Pipeline with CloudTruth Parameters

### Using the CloudTruth CLI

You can install the CLI with the following code snippet as part of a Bitbucket step in *bitbucket-pipelines.yml*. This will install the latest supported version and also verify the CLI is installed correctly by printing the version.

We then use the CloudTruth CLI run command to print only CloudTruth variables that can now be accessed in your pipeline.

```
image: atlassian/default-image:2

pipelines:
  default:
      - step:
          name: 'Install CloudTruth CLI:'
          script:
            - (curl -sL https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh || wget -qO- https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh) | sh      
            
            - cloudtruth -V
            - cloudtruth --project MyFirstProject run -i none -- printenv 
```

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

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

###

### Passing CloudTruth parameters across Bitbucket Pipeline steps

You can use variables across Bitbucket pipeline steps by creating an export formatted environment file and storing the file using [Bitbucket artifacts](https://support.atlassian.com/bitbucket-cloud/docs/use-artifacts-in-steps/). This snippet uses the CloudTruth CLI to create an export dotenv file with values pulled from CloudTruth.

```
            - cloudtruth --project MyFirstProject parameter export shell --export > dotenv
          
          # Passes environment variables between stages  
          artifacts:
            - dotenv
```

### Using CloudTruth parameters across Bitbucket Pipeline steps

In future pipeline steps you can source the dotenv artifact. The command `source dotenv` will execute the dotenv export allowing you to reference the variables in downstream stages without the CloudTruth CLI.

```
      - step:
          name: 'CloudTruth Variables Deployment to Staging'
          deployment: staging
          script:
            - echo "Test CloudTruth Staging Step"
            - source dotenv
            - printenv
```

The complete **bitbucket-pipelines.yml** passing variables between steps.

```
image: atlassian/default-image:2

pipelines:
  default:
      - step:
          name: 'Install CloudTruth CLI:'
          script:
            - (curl -sL https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh || wget -qO- https://github.com/cloudtruth/cloudtruth-cli/releases/latest/download/install.sh) | sh      
              
            - cloudtruth -V
            - cloudtruth --project MyFirstProject run -i none -- printenv 
            - cloudtruth --project MyFirstProject parameter export shell --export > dotenv
          
          # Passes environment variables between stages  
          artifacts:
            - dotenv

      - step:
          name: 'CloudTruth Variables Deployment to Staging'
          deployment: staging
          script:
            - echo "Test CloudTruth Staging Step"
            - source dotenv
            - printenv
```

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

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