> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Authenticate GitLab.com CI/CD with OIDC Canonical URL: https://posthog.com/docs/api/oidc/gitlab ## On this page - [What the job needs](#what-the-job-needs) - [How the job gets a token](#how-the-job-gets-a-token) - [Which pipelines a policy matches](#which-pipelines-a-policy-matches) - [Presets in PostHog settings](#presets-in-posthog-settings) - [Why the defaults are safe](#why-the-defaults-are-safe) - [Set up a trusted workload](#set-up-a-trusted-workload) - [Configure the job](#configure-the-job) - [Troubleshooting](#troubleshooting) A GitLab CI/CD job on GitLab.com can trade the ID token GitLab issues for the job for a short-lived PostHog token. You don't store a personal API key as a CI/CD variable, and the token only works on one project. The [overview](https://posthog.com/docs/api/oidc.md) covers trust policies, requests, and the token exchange for every provider. ## What the job needs - An `id_tokens` entry named `HOG_ID_TOKEN`, with `aud` set to your PostHog host - A trusted workload in your PostHog project that matches the GitLab project and the pipelines you pick - Your PostHog project ID and host, in `posthog.json` or in the job's variables PostHog trusts the GitLab.com issuer, `https://gitlab.com`. Tokens from a self-managed GitLab instance come from a different issuer, so PostHog refuses them, and hog only uses an ID token in jobs that run on GitLab.com. ## How the job gets a token GitLab creates the ID tokens a job lists under `id_tokens` when the job starts, and puts each one in a variable with the entry's name. hog reads the token from `HOG_ID_TOKEN` and exchanges it at `/oauth/token/`. The `aud` you set must match the PostHog host hog sends the token to. The ID token lives as long as the job's timeout, or 5 minutes by default. The PostHog token never outlives the ID token it came from. hog keeps the PostHog token in memory and exchanges again when it's close to expiring, or after a `401`. See GitLab's [ID token authentication docs](https://docs.gitlab.com/ci/secrets/id_token_authentication/) and the [`id_tokens` keyword reference](https://docs.gitlab.com/ci/yaml/#id_tokens) for how GitLab issues these tokens. ## Which pipelines a policy matches A policy identifies the GitLab project with two claims, and both must match: - **`project_id`** - the project's numeric ID. It never changes, so a project that is deleted and created again with the same path doesn't match. - **`project_path`** - the project's full path, such as `acme/web`. If you rename or move the project, update the policy. A merge request pipeline from a fork carries the fork's project ID, not yours, so your project's policy never matches it. PostHog also refuses any pipeline that runs in another project than its source, where `job_project_id` differs from `project_id`, such as a fork's merge request pipeline that runs in your project. Conditions narrow which pipelines in that project get a token. A policy sets at least one. | Claim | What it holds | Example value | | --- | --- | --- | | `ref` | The short name of the branch or tag | `main` | | `ref_type` | Whether `ref` is a branch or a tag | `branch` | | `ref_protected` | Whether the branch or tag is protected | `true` | | `environment` | The environment the job deploys to | `production` | | `environment_protected` | Whether that environment is protected | `true` | | `environment_action` | What the job does to the environment: `start` deploys, while `prepare`, `verify`, and `access` don't wait for approval | `start` | | `pipeline_source` | What started the pipeline | `push` | | `ci_config_ref_uri` | The pipeline configuration file and the ref it came from | `gitlab.com/acme/web//.gitlab-ci.yml@refs/heads/main` | Two conditions need a companion: - A policy that sets `ref` must also set `ref_type`. GitLab names a branch or a tag by its short name, so without the ref type a tag with a protected branch's name also matches. - A policy that sets `environment` must also set `environment_action`. Jobs that prepare, verify, or access a protected environment don't wait for deployment approval, so without the action they also match. ## Presets in PostHog settings Under **Which runs can get a token**, the settings page offers these presets. Both require GitLab's protection on the branch or environment. | Preset | Conditions it sets | When to use it | | --- | --- | --- | | **A protected branch** (default, branch `main`) | `ref` = ``, `ref_type` = `branch`, and `ref_protected` = `true` | Pipelines that run on a protected branch, such as deploys from `main` | | **A protected environment** (environment `production`) | `environment` = ``, `environment_protected` = `true`, and `environment_action` = `start` | Jobs that deploy to a protected environment. Protected environments need GitLab Premium or Ultimate. | To use another claim from the table above, such as `pipeline_source`, pick it under **One claim of the token** and enter its exact value. ## Why the defaults are safe **A protected branch** pins the branch name, the ref type, and the protection: - `ref` alone holds a short name, so a tag called `main` has the same `ref` as the `main` branch. `ref_type` = `branch` stops a tag from matching. - `ref_protected` = `true` keeps the policy safe if the branch loses its protection, or someone later reuses the branch name. - Any pipeline that runs on the branch matches, including scheduled and manual pipelines. To allow only pushes, add a `pipeline_source` = `push` condition. **A protected environment** requires `environment_protected` = `true`, because anyone who can push a branch can name an unprotected environment in a job. It also requires `environment_action` = `start`, so only jobs that deploy, and wait for approval, match. Merge request pipelines from a fork can't match at all: they carry the fork's project ID, and PostHog refuses one that runs in your project. ## Set up a trusted workload Only organization admins and owners can add a trusted workload. Anyone else in the project can [ask an admin](https://posthog.com/docs/api/oidc.md#asking-an-admin-for-a-trusted-workload) for one. ### In PostHog settings 1. In your project settings, go to **Trusted workloads** and click **Add trusted workload**. 2. Under **Provider**, pick **GitLab**. 3. In **Project path**, enter the full path with any subgroups, such as `acme/web`, and click **Look up**. PostHog fills in the **Project ID**. 4. Under **Which runs can get a token**, keep **A protected branch** and change the branch if it isn't `main`, or pick **A protected environment**. 5. Give the trusted workload a **Name**, and pick the **Scopes** the job needs. 6. Click **Add trusted workload**, then copy the `posthog.json` keys and the job snippet PostHog shows. PostHog looks the project up through your project's GitLab integration first, if it connects to GitLab.com, so a private project works when it's the project the integration connects to. PostHog doesn't look up other private projects. Without the integration, PostHog only finds public projects. If neither finds the project, enter the **Project ID** yourself. GitLab shows it under **Settings** > **General** in the project. ### With `hog ci setup` Logged in with `hog auth login`, run this in your repository: ```bash hog ci setup --preset branch=main --scope error_tracking:write ``` hog finds `.gitlab-ci.yml`, reads the project path from your `origin` remote, asks PostHog to look up the project ID, and applies **A protected branch** for `main`. GitLab offers two presets, and neither is required: - `--preset branch=` - **A protected branch** - `--preset environment=` - **A protected environment** To pass both, repeat the flag: `--preset branch=main --preset environment=production`. Without `--preset`, hog asks which runs may get a token. List the scopes the job needs in `--scope`, separated by commas, such as `--scope error_tracking:write,query:read`. hog names the policy ` on GitLab`. If PostHog can't look up the project, pass `--subject-id` with the project ID and `--repository` with the exact project path. hog then creates a policy request. If you're an admin at a terminal, it opens the approval page and waits. Otherwise it prints the link to send to an admin. `hog ci setup --wait` waits for up to 10 minutes and keeps the request, so you can run it again later. The [overview](https://posthog.com/docs/api/oidc.md#ask-with-hog-ci-setup) lists every flag. ## Configure the job A job needs your PostHog project ID and host, and a way to get its token. The settings page, and `hog ci setup`, print all of this with your values filled in. ### 1\. Add the project to `posthog.json` (preferred) Add these keys to `posthog.json` in the repository. hog looks for the file from the current folder up to the git root. ```json { "host": "https://us.posthog.com", "projectId": 12345 } ``` - `projectId` is your project's ID: the number in your project URL, such as `12345` in `https://us.posthog.com/project/12345`. - `host` counts in a CI job only when it's PostHog Cloud, `https://us.posthog.com` or `https://eu.posthog.com`, so a change to the file can't send the job's token elsewhere. Without a host, hog uses `https://us.posthog.com`. - Never put `HOG_PERSONAL_API_KEY` in `posthog.json`. ### 2\. Give the job an ID token ```yaml your-job: id_tokens: HOG_ID_TOKEN: # lets hog get a PostHog token aud: https://us.posthog.com ``` Add these keys to the job in `.gitlab-ci.yml` that runs hog, and rename `your-job` to your job's name. `id_tokens` asks GitLab for an ID token, and hog reads it from `HOG_ID_TOKEN`. Its `aud` must equal your PostHog host. ### 3\. Run hog in the job Run hog commands in the job's `script` as usual. hog reads the ID token from `HOG_ID_TOKEN` and exchanges it for you. This example uploads source maps for error tracking, so the trusted workload needs the `error_tracking:write` scope: ```yaml upload-sourcemaps: image: node:22 id_tokens: HOG_ID_TOKEN: # lets hog get a PostHog token aud: https://us.posthog.com script: # ...build your app into ./dist - npx -y hog error-tracking process ./dist ``` With an image that doesn't have Node.js, install hog with `curl -fsSL https://releases.posthog.com/hog/install.sh | sh` and run `~/.local/bin/hog`. On Alpine and other musl images, run `apk add libstdc++ libgcc` first. ### Or set environment variables Instead of `posthog.json`, set `HOG_HOST` and `HOG_PROJECT_ID` in the job's `variables`. They win when both are set. A self-hosted PostHog always needs `HOG_HOST` in the job. When `posthog.json` names a host that isn't PostHog Cloud and the job doesn't set `HOG_HOST`, hog stops with a `workload_host_not_allowed` error that names the host to set. Remove `HOG_PERSONAL_API_KEY` from the job. hog uses a personal API key ahead of the workload token. hog's usage analytics from this job carry a hashed workload ID, not a person. ## Troubleshooting | Problem | Likely cause | What to do | | --- | --- | --- | | hog can't find an ID token | The job has no `id_tokens` entry named `HOG_ID_TOKEN` | Add the `id_tokens` block from the snippet to the job. | | Self-managed GitLab, or [`workload_issuer_not_allowed`](https://posthog.com/docs/api/oidc.md#workload_issuer_not_allowed) | PostHog only trusts tokens from GitLab.com | Use `HOG_PERSONAL_API_KEY` and `HOG_PROJECT_ID` in this job instead. | | [`workload_audience_mismatch`](https://posthog.com/docs/api/oidc.md#workload_audience_mismatch) | The token's `aud` isn't the PostHog site URL hog sends it to | Set `aud`, and the host in `posthog.json` or `HOG_HOST`, to the address you open PostHog at. | | [`workload_token_invalid`](https://posthog.com/docs/api/oidc.md#workload_token_invalid) | The token expired, its signature doesn't verify, or it lacks a claim PostHog needs. A job that runs in another project than its source, such as a fork's merge request pipeline that runs in your project, also causes it. | Run the job again. GitLab issues a new ID token for each job. PostHog never trusts a job that runs in another project. | | [`workload_no_matching_policy`](https://posthog.com/docs/api/oidc.md#workload_no_matching_policy) | The pipeline doesn't match the policy, for example a tag, another branch, or a branch that isn't protected. A fork, a renamed or recreated project, or the wrong project ID, also cause it. | The error lists the claims the job sent. Compare them with the policy, or run `hog ci setup` to ask for a policy that matches. | | [`workload_policy_request_pending`](https://posthog.com/docs/api/oidc.md#workload_policy_request_pending) | An admin hasn't approved the request yet | Send the approval link from the error to an admin. | | [`workload_no_usable_scopes`](https://posthog.com/docs/api/oidc.md#workload_no_usable_scopes) | The policy only grants scopes a workload can't use | Ask an admin to add scopes the job can use. | | [`workload_rate_limited`](https://posthog.com/docs/api/oidc.md#workload_rate_limited) | The policy used up its exchanges, or too many exchange attempts came from the runner's IP address | hog retries while the total wait stays within 60 seconds. Otherwise, run the job again later. | | `workload_host_not_allowed` from hog | `posthog.json` names a host that isn't PostHog Cloud, and the job doesn't set `HOG_HOST` | Set `HOG_HOST` in the job to the host the error names. | | A call fails with `403` | The policy doesn't grant the scope the call needs | The `WWW-Authenticate` header names the scope. Ask an admin to add it. | See [errors](https://posthog.com/docs/api/oidc.md#errors) in the overview for every error code.