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

# Configuring GitLab CI for CodSpeed

> Learn how to configure GitLab CI to run benchmarks with CodSpeed.

## Authentication

In order to upload benchmark results to CodSpeed, the GitLab CI job needs to
authenticate with CodSpeed. There are two supported methods for authentication:
OpenID Connect (OIDC) and static CodSpeed tokens.

### OIDC (Recommended)

CodSpeed recommends using
[OpenID Connect (OIDC)](https://openid.net/developers/how-connect-works/) for
authentication.

Using this method, a token is generated on-the-fly during the workflow run. This
token is then used to authenticate securely with CodSpeed without needing to
store long-lived credentials, but grants no additional permissions to the
workflow.

Configure your GitLab CI job to use the `CODSPEED_TOKEN` as an OIDC token by
adding the following section to your job definition:

```yaml theme={null}
id_tokens:
  CODSPEED_TOKEN:
    aud: codspeed.io
```

For more information on setting up OIDC in GitLab CI, refer to the
[GitLab documentation on OIDC](https://docs.gitlab.com/ci/secrets/id_token_authentication/#configure-id-tokens-in-a-cicd-job).

### CodSpeed token (Legacy)

While we recommend using OpenID Connect (OIDC) for improved security, you can
use a static CodSpeed token for authentication.

Retrieve your CodSpeed token from your repository settings on CodSpeed

![Upload Token from the settings page](https://mintcdn.com/codspeed/jKaxX6yy-Kzw1C-0/assets/upload-token.png?fit=max\&auto=format\&n=jKaxX6yy-Kzw1C-0\&q=85\&s=84e746ad14c38862e9e72776ee7b1f38)

> [!WARNING]
>
> Be mindful that a token is scoped to a specific repository. Make sure that you
> are on the correct repository settings page when copying the token.

Then, create a new
[CI/CD variable](https://docs.gitlab.com/ee/ci/variables/#for-a-project) in your
repository with the name `CODSPEED_TOKEN` and the value of your token.

> [!WARNING]
>
> Set the `CODSPEED_TOKEN` variable as `"Masked"`, but not `"Protected"`, because
> it must be available on all branches.
>
> ![Upload Token from the settings page](https://mintcdn.com/codspeed/jKaxX6yy-Kzw1C-0/assets/gitlab-ci-variable.png?fit=max\&auto=format\&n=jKaxX6yy-Kzw1C-0\&q=85\&s=800e86e3b9bfef309e2b5683bc6470b0)

## Advanced

### Running benchmarks in parallel CI jobs

With GitlabCI, you can split your benchmarks across multiple jobs.

For example with `pytest`. The `--test-group` options come from
[`pytest-test-groups`](https://codspeed.io/docs/benchmarks/python#running-benchmarks-in-parallel-ci-jobs),
which the benchmarks jobs need installed alongside your other dependencies:

```yaml .gitlab-ci.yml theme={null}
workflow:
  # run on merge requests and pushes on the default branch
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

codspeed-1:
  stage: test
  image: python:3.12
  id_tokens:
    CODSPEED_TOKEN:
      aud: codspeed.io
  before_script:
    - pip install -r requirements.txt
    - curl -fsSL https://codspeed.io/v5.8.0/install.sh | sh
    - source $HOME/.cargo/env
  script:
    - codspeed run --mode instrumentation -- pytest tests/ --codspeed --test-group=1 --test-group-count=2

codspeed-2:
  stage: test
  image: python:3.12
  id_tokens:
    CODSPEED_TOKEN:
      aud: codspeed.io
  before_script:
    - pip install -r requirements.txt
    - curl -fsSL https://codspeed.io/v5.8.0/install.sh | sh
    - source $HOME/.cargo/env
  script:
    - codspeed run --mode instrumentation -- pytest tests/ --codspeed --test-group=2 --test-group-count=2
```

> [!NOTE]
>
> CodSpeed only supports emitting results from your benchmarks if you split them
> within a single CI workflow.
>
> If you run benchmarks in multiple CI workflows, CodSpeed will not be able to
> aggregate the results correctly, and you may see incomplete or missing data in
> your CodSpeed reports.

Learn more about
[benchmark sharding and how to integrate with your CI provider](https://codspeed.io/docs/features/sharded-benchmarks.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.