> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cognite.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Setting up GitLab CI/CD

> Configure GitLab CI/CD to build and deploy Cognite Toolkit modules. You create stages and jobs, add secure variables for authentication, run a dry run, and verify deployments on the main branch.

## Create a CI/CD pipeline

<Steps>
  <Step title="Create a CI/CD pipeline">
    1. In the GitLab UI, open your repository. In the left menu, select <span class="ui-element">Build</span> > <span class="ui-element">Pipeline editor</span>.
    2. Click <span class="ui-element">Configure pipeline</span> to be guided through creating a `.gitlab-ci.yml` file to add to your repository to orchestrate the pipeline.

       The edit section shows a skeleton pipeline that you can configure. You can use this example CI/CD pipeline as a starting point:

       ```yaml theme={"languages":{"custom":["/_languages/kuiper.json","../_languages/kuiper.json"]}}
       stages:
         - build
         - deploy_dry_run
         - deploy_to_cdf

       variables:
         CDF_CLUSTER: $CDF_CLUSTER
         CDF_URL: $CDF_URL
         CDF_PROJECT: $CDF_PROJECT
         CDF_CLIENT_TIMEOUT: 100
         IDP_CLIENT_ID: $IDP_CLIENT_ID
         IDP_CLIENT_SECRET: $IDP_CLIENT_SECRET
         IDP_TENANT_ID: $IDP_TENANT_ID
         CONTAINER_IMAGE: 'cognite/toolkit:0.5.35'

       build_modules:
         stage: build
         image: $CONTAINER_IMAGE
         script:
           - cdf build
         artifacts:
           paths:
             - build/
         rules:
           - if: '$CI_PIPELINE_SOURCE == "push"'
         retry: 2

       deploy_dry_run:
         stage: deploy_dry_run
         image: $CONTAINER_IMAGE
         script:
           - cdf deploy --dry-run
         dependencies:
           - build_modules
         rules:
           - if: '$CI_PIPELINE_SOURCE == "push"'
         retry: 2

       deploy_to_cdf:
         stage: deploy_to_cdf
         image: $CONTAINER_IMAGE
         script:
           - cdf deploy
         dependencies:
           - build_modules
         rules:
           - if: '$CI_COMMIT_BRANCH == "main"'
         retry: 2
       ```

    Where:

    * `stages` defines the order of the jobs in a pipeline. A job has to finish successfully before the next one starts.

    * `variables` defines configuration data to authenticate to CDF. `CONTAINER_IMAGE` defines the version of the Cognite Toolkit to use for the jobs.

    The next three blocks describe a job, in this example: `build_modules`, `deploy_dry_run` and `deploy_to_cdf`.

    * Use `stage` to define the name of the job.

    * Use `script` to pass the code to run during the job.

    * Within the build job, the `artifacts` section defines where build files are stored. Go to <span class="ui-element">Build</span> > <span class="ui-element">Artifacts</span> to see previous artifacts.

    * For deployment jobs (for example, `deploy_dry_run`), include a `dependencies` section. The deployment jobs depend on output from the previous job.

    * The `rules` section lists all rules for the job. In this example configuration, the rules are identical for `build` and `deploy_dry_run`, so these jobs run on push to any branch. `deploy_to_cdf` run only on pushes to main branch.

    * Use the `retry` section to define how many retry attempts to allow on failure. 2 is the maximum.

    3. Commit your `.gitlab-ci.yml` file to a branch. The jobs will fail until you finish the setup in the next section.

    For more information, see the [GitLab CI/CD pipelines documentation](https://docs.gitlab.com/ci/pipelines).
  </Step>
</Steps>

## Create variables for running the pipeline

<Steps>
  <Step title="Create variables for running the pipeline">
    1. In the left-hand menu of your project, navigate to <span class="ui-element">Settings</span> > <span class="ui-element">CI/CD</span>.

    2. Select <span class="ui-element">Variables</span>.

    3. In empty table in the <span class="ui-element">Project variables</span> section, use the <span class="ui-element">Add variable</span> button to enter these variables with the correct values for your environment:

       * CDF\_CLUSTER
       * CDF\_PROJECT
       * CDF\_URL
       * IDP\_CLIENT\_ID
       * IDP\_CLIENT\_SECRET (**Visibility** > "Masked and hidden")
       * IDP\_TENANT\_ID

    4. **Important**: Hide the `IDP_CLIENT_SECRET` value. Visibility for other variables can normally be set to "Visible."
  </Step>
</Steps>

## Confirm that the pipeline runs as expected

<Steps>
  <Step title="Confirm that the pipeline runs as expected">
    To check that the setup is successful, push a small change to a branch other than *main*. Follow the progress of your pipeline in <span class="ui-element">Build</span> > <span class="ui-element">Pipelines</span>.
  </Step>
</Steps>
