# BitBucket

With Paradime Turbo CI, you can run and test code changes to your dbt™️ project prior to merge them in your main branch, by configuring a Bolt schedule to run when a new pull request is opened against your dbt™️ repository.

Paradime will build the models affected by your changed into a temporary schema, and will run tests that you have written against the changes models to validate your test are still passing. When opening a pull request, Paradime Turbo CI run status will be showing in the pull request.

## How Turbo CI works <a href="#how-turbo-ci-works" id="how-turbo-ci-works"></a>

When Turbo CI is configured, a BitBucket Pipeline will trigger a TurboCI schedule in Paradime executing your configured dbt™️ command to run and test your dbt™️ changes.

When running, models will be built in a temporary schema using the prefix **`paradime_turbo_ci_pr_`** followed by the commit SHA number (e.g **`paradime_turbo_ci_pr_6d8f55c0`**). This will enabled you to see the results of your changes associated with the code in your pull request in your production data warehouse

<figure><img src="https://2337193041-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHET0AD04uHMgdeLAjptq%2Fuploads%2F3cAkXvnt9Zv60jFHv4Sa%2Fimage.png?alt=media&#x26;token=c08843ca-8132-445b-9598-80c3bfd5a26f" alt=""><figcaption></figcaption></figure>

## Pre-requisites <a href="#pre-requisites" id="pre-requisites"></a>

{% hint style="warning" %}
**A Scheduler Connection to your data warehouse where the target is set to `ci`.**

**ℹ️** [**Check our setup guide here based on your data warehouse provider**](https://docs.paradime.io/app-help/documentation/settings/connections/scheduler-environment)**.**
{% endhint %}

### Configure a custom schema for CI runs <a href="#configuring-bolt-turbo-ci-job" id="configuring-bolt-turbo-ci-job"></a>

Customize you dbt™️ schema generation at runtime, to make sure that when a PR is opened, Paradime Turbo CI will build and test your changed dbt™️ models in a temporary schema. We will want to update the dbt™️  `generate_schema_name.sql` macro.

In your dbt™️ project, in your *macros* folder create a macro called: `generate_schema_name.sql` and use a similar logic as below, where when a Bolt run is executed using the `ci` target the schema where your models will be built will use the **`paradime_turbo_ci_pr`** prefix.

```sql
{% macro generate_schema_name(custom_schema_name, node) -%}

    {%- set default_schema = target.schema -%}
    {%- if target.name == 'prod' -%}

        {{ custom_schema_name | trim }}

    {%- elif target.name == 'ci' -%}

        {{ default_schema }}

    {%- else -%}

        {%- if custom_schema_name is none -%}

            {{ default_schema }}

        {%- else -%}

            {{ default_schema }}_{{ custom_schema_name | trim }}

        {%- endif -%}

    {%- endif -%}

{%- endmacro %}
```

See also:

{% embed url="<https://docs.getdbt.com/docs/build/custom-schemas#advanced-custom-schema-configuration>" %}

### Deferral and State Comparison explained <a href="#deferral-and-state-comparison-explained" id="deferral-and-state-comparison-explained"></a>

When creating the Turbo CI job in Paradime, you can set your execution settings to defer to a previous run state by adding in the `deferred_schedule_name` configuration the name of the Bolt productions schedule you want to defer to

Paradime will look at the `manifest.json` from the specified schedule's most recent successful run *(unless setting the optional parameter `successful_run_only: false`)* to determined the set of new and modified dbt™️ resources. In your Turbo CI commands, you can then use the `state:modified+` argument to only run the modified resources and their downstream dependencies.

**A common example is:**

```bash
dbt build --select state:modified+ --target ci
```

This will run and test all modified models with the downstream dependencies. Useful to validate that test are still passing after making changes to upstream dbt™️ models.

👉 To read more about state comparison check the dbt Core™️ documentation [here](https://docs.getdbt.com/reference/node-selection/state-comparison-caveats).

## 1. Configuring Bolt Turbo CI job <a href="#configuring-bolt-turbo-ci-job" id="configuring-bolt-turbo-ci-job"></a>

Setting up your Turbo CI Bolt Schedule is very similar to a normal bolt schedule with the addition of a couple more configurations.

A Turbo CI job differ from a bolt schedule as it involves:

* The Bolt schedule to defer to
* The `commands` to use the `state:modified+` selector to build / tests only new dbt™️ models and their downstream dependencies. State comparison can only happen when there is a deferred schedule name configured to compare state to.
* Being triggered by a pull request opened in your  BitBucket dbt™️ repository

**Check our template:**

{% content-ref url="../../creating-schedules/templates/ci-cd-templates/test-code-changes-on-pull-requests" %}
[test-code-changes-on-pull-requests](https://docs.paradime.io/app-help/documentation/bolt/creating-schedules/templates/ci-cd-templates/test-code-changes-on-pull-requests)
{% endcontent-ref %}

## 2. Create a BitBucket Pipeline&#x20;

### Generate API keys and find you workspace token

{% hint style="warning" %}
**API keys are generate at a workspace level.**
{% endhint %}

To be able to trigger Bolt using the API, you will first need to generate API keys for your workspace. Got to account settings and generate your API keys, make sure to save in your password manager:

* API key
* API secret
* API Endpoint

You will need this later when setting up the secrets in BitBucket.

{% content-ref url="../../../../developers/generate-api-keys" %}
[generate-api-keys](https://docs.paradime.io/app-help/developers/generate-api-keys)
{% endcontent-ref %}

### Create a BitBucket Pipeline

Now you will need to create a new `bitbucket-pipelines.yml` file in your dbt™️ repository. Copy the code block below and enter the values required.

<details>

<summary>Example BitBucket pipelines configuration file</summary>

{% code title="bitbucket-pipelines.yml" lineNumbers="true" %}

```yaml
# Specify the base Docker image to use for all pipeline steps
image: python:3.11

# Define reusable pipeline steps
definitions:
 steps:
   # Create a reusable step template using YAML anchor (&)
   - step: &paradime-turbo-ci
       # Name of this pipeline step for identification
       name: Paradime Turbo CI
       
       # Commands to execute in this step
       script:
         # Export required Paradime environment variables from repository variables
         - export PARADIME_API_KEY=$PARADIME_API_KEY
         - export PARADIME_API_SECRET=$PARADIME_API_SECRET
         - export PARADIME_API_ENDPOINT=$PARADIME_API_ENDPOINT
         
         # Get the PR number from Bitbucket's predefined variable
         - export PR_NUMBER=$BITBUCKET_PR_ID
         - echo "PR Number: $PR_NUMBER"
         
         # Get the commit hash
         - echo "Commit Hash: $BITBUCKET_COMMIT"
         
         # Install Paradime Python SDK 
         - pip install paradime-io==4.18.0  # Check for latest version of the Paradime Python SDK on https://github.com/paradime-io/paradime-python-sdk/releases
         
         # Run Paradime bolt schedule with PR number
         # --branch flag uses Bitbucket's commit hash variable
         # --pr-number flag passes the PR ID to Paradime
         # --wait flag makes the pipeline wait for completion
         - paradime bolt run "turbo_ci_run" --branch $BITBUCKET_COMMIT --pr-number $PR_NUMBER --wait
       
       # Specify compute resources for this step
       size: 2x  # Double the default resources
       
       # Set maximum execution time in minutes
       max-time: 60

# Define when and how pipelines should run
pipelines:
 pull-requests:
   # '**' means this will run on PRs from any branch
   '**':
     - step:
         # Reference the template step defined above using YAML alias (*)
         <<: *paradime-turbo-ci
         
         # Define environment variables needed for this step
         # These should be configured in Bitbucket repository settings
         variables:
           API_KEY: PARADIME_API_KEY      # API key for Paradime authentication
           API_SECRET: PARADIME_API_SECRET # API secret for Paradime authentication
           API_ENDPOINT: PARADIME_API_ENDPOINT # Paradime API endpoint URL
```

{% endcode %}

</details>

### Add the API keys and Credentials in the BitBucket Pipeline variables

Finally you need to add the API key and credentials generated in the [previous step](#generate-api-keys-and-find-you-workspace-uid) in BitBucket Pipelines.

Set the corresponding values using your credentials for the variable names:

* `PARADIME_API_KEY`
* `PARADIME_API_SECRET`
* `PARADIME_API_ENDPOINT`

{% @arcade/embed flowId="F07biP3yTQk37Ntyc095" url="<https://app.arcade.software/share/F07biP3yTQk37Ntyc095>" %}

## View Turbo CI run Logs <a href="#view-turbo-ci-run-logs" id="view-turbo-ci-run-logs"></a>

As per other bolt schedules, you can inspect run logs for each of the Turbo CI jobs running when a pull request is opened.

{% content-ref url="../../managing-schedules/viewing-run-log-history" %}
[viewing-run-log-history](https://docs.paradime.io/app-help/documentation/bolt/managing-schedules/viewing-run-log-history)
{% endcontent-ref %}

<figure><img src="https://2337193041-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHET0AD04uHMgdeLAjptq%2Fuploads%2FOQNPYgdk2IufImyY1Kwl%2Fimage.png?alt=media&#x26;token=f9694cc0-8410-4c28-b530-4d1f70968d51" alt=""><figcaption></figcaption></figure>
