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

# GitLab CI/CD

> Federate GitLab CI/CD ID tokens into short-lived TrueFoundry access tokens for pipelines.

GitLab CI/CD jobs can request an OIDC ID token from your GitLab instance. A job can exchange that token for a short-lived TrueFoundry access token. This way, you don't need to store a Virtual Account token as a CI/CD variable.

TrueFoundry requires a `project_id` claim match on the Virtual Account **Claims** (or Agent **Subject**) mapping.

For concepts and the shared exchange API, see [Workload Identity Federation](https://www.truefoundry.com/docs/platform/identity-providers/workload-identity-federation/overview.md).

## Prerequisites

* A GitLab project on GitLab.com, GitLab Self-Managed, or GitLab Dedicated, where you can edit `.gitlab-ci.yml`.
* The project ID. You can find it on the project overview page and under **Settings > General**.
* A [Virtual Account](https://www.truefoundry.com/docs/platform/virtual-account-management.md) or Agent with the permissions the job needs.

## Configure the job

GitLab issues an ID token only when the job requests one with [`id_tokens`](https://docs.gitlab.com/ci/secrets/id_token_authentication/). Set `aud` to the audience you will register on TrueFoundry:

```yaml theme={"dark"}
deploy:
  id_tokens:
    TFY_ID_TOKEN:
      aud: truefoundry-<tenant>
  script:
    - ./call-truefoundry.sh
```

GitLab makes the token available to the job as `$TFY_ID_TOKEN`. The token expires when the job times out. If the job has no timeout, it expires after 5 minutes. Exchange it early in the job.

A typical token looks like this:

```json theme={"dark"}
{
  "iss": "https://gitlab.com",
  "aud": "truefoundry-<tenant>",
  "sub": "project_path:your-group/your-project:ref_type:branch:ref:main",
  "project_id": "12345678",
  "project_path": "your-group/your-project",
  "namespace_id": "87654321",
  "namespace_path": "your-group",
  "ref": "main",
  "ref_type": "branch",
  "ref_protected": "true"
}
```

GitLab sends `project_id`, `namespace_id`, and `ref_protected` as strings.

## Configure TrueFoundry

1. Go to **Settings > Security & Access > Identity Providers**, then add a provider of type **GitLab**.
2. Enter these values:

| Field | Value |
| - | - |
| Issuer URL | `https://gitlab.com`, or your GitLab instance URL for Self-Managed and Dedicated. It must match the token's `iss` claim. |
| Allowed Audiences | The same `aud` you set under `id_tokens`, for example `truefoundry-<tenant>` |
| JWKS URI | `https://gitlab.com/oauth/discovery/keys`, or the `jwks_uri` from `{issuer}/.well-known/openid-configuration`. TrueFoundry's control plane must be able to reach it. |

An issuer and audience pair can belong to only one identity provider. All GitLab.com projects share the issuer `https://gitlab.com`, so use an audience specific to your tenant, such as `truefoundry-<tenant>`.

3. Map the Virtual Account or Agent to the provider on `project_id`.

**Virtual Account Claims**

```yaml theme={"dark"}
identity_provider_mapping:
  - identity_provider: gitlab-ci
    claims:
      project_id: "12345678"
```

**Agent Subject** (the YAML key is still `claims`)

```yaml theme={"dark"}
identity:
  type: identity-provider-backed
  identity_provider_mapping:
    - identity_provider: gitlab-ci
      claims:
        project_id: "12345678"
```

The required key for `gitlab` is `project_id`. It is unique within a GitLab instance and does not change when the project is renamed. To narrow access further, add claims such as `ref_protected: "true"` or `sub`.

## Exchange and call TrueFoundry

Add the exchange step to your pipeline:

```yaml theme={"dark"}
deploy:
  id_tokens:
    TFY_ID_TOKEN:
      aud: truefoundry-<tenant>
  variables:
    # Issuer origin. The exchange path is /{tenant}/oauth2/v1/token (no /api/svc).
    TFY_CONTROL_PLANE: https://<tenant-host-origin>
    TFY_TENANT: <tenant>
  script:
    - |
      TFY_ACCESS_TOKEN=$(jq -n --arg t "$TFY_ID_TOKEN" '{
          grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",
          subject_token: $t,
          subject_token_type: "urn:ietf:params:oauth:token-type:jwt",
          tfy_subject: "virtual_account:<virtual-account-name>"
        }' \
        | curl -sS "$TFY_CONTROL_PLANE/$TFY_TENANT/oauth2/v1/token" \
            -H 'Content-Type: application/json' -d @- \
        | jq -r .access_token)
      export TFY_ACCESS_TOKEN
```

Use `TFY_ACCESS_TOKEN` as the `Authorization: Bearer` header for the [AI Gateway](https://www.truefoundry.com/docs/ai-gateway/authentication.md) or other APIs. For Agents, set `tfy_subject` to `agent:<agent-name>`. For the full exchange flow, see [Workload Identity Federation](https://www.truefoundry.com/docs/platform/identity-providers/workload-identity-federation/overview.md#set-up-federation).

Don't print the ID token or the access token with `echo`. Job logs are visible to everyone who can access the project.

## Scope the mapping

* `project_id` alone allows every branch, tag, and merge request pipeline in that project. Pipelines from forks carry the fork's `project_id`, so they don't match.
* Add `ref_protected: "true"` to allow only protected branches and tags.
* `sub` includes the branch or tag name, for example `project_path:your-group/your-project:ref_type:branch:ref:main`. Use it only when one specific ref should authenticate, because it changes for every branch, tag, and merge request.
* Don't store a long-lived Virtual Account token in CI/CD variables when federation is available.

## Troubleshooting

| Error | Likely cause |
| - | - |
| `No identity provider matched the subject token issuer and audience.` | The `aud` under `id_tokens` doesn't match the provider's Allowed Audiences, the issuer differs, or the Virtual Account has no mapping to this provider. |
| `The subject token does not match the configured claim project_id.` | The pipeline runs in a different project (or a fork) than the one mapped. |
| `No signing key is available for the subject token.` | TrueFoundry hasn't cached the provider's keys yet. Save the identity provider again and retry. |
| Token expired | The job requested the token too early or ran past its timeout. Exchange the token at the start of the job. |


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