Authenticate Depot CI with OIDC

A Depot CI job can trade the OIDC token Depot issues for each job for a short-lived PostHog token. You don't store a personal API key as a secret, and the token only works on one project. The overview covers trust policies, requests, and the token exchange for every provider.

What the job needs

  • A job that runs in Depot CI
  • The id-token: write permission, so Depot issues an OIDC token to the job
  • A trusted workload in your PostHog project that matches your Depot organization, the repository, and the runs you pick
  • Your PostHog project ID and host, in posthog.json or in the job's environment

PostHog trusts Depot's issuer, https://identity.depot.dev.

How the job gets a token

hog knows it runs in Depot CI when the job has a DEPOT_JOB_URL variable. Depot runners for GitHub Actions don't set it, so their jobs use GitHub's tokens, as described on the GitHub Actions page.

Depot sets the same token request variables in the job that GitHub Actions does, ACTIONS_ID_TOKEN_REQUEST_URL and ACTIONS_ID_TOKEN_REQUEST_TOKEN. hog uses them to ask for a token whose audience is your PostHog host, then exchanges it at /oauth/token/.

Depot's OIDC tokens live 5 minutes, and the PostHog token never outlives the token it came from. hog keeps the PostHog token in memory and exchanges a new Depot token when it's close to expiring, or after a 401, so a long job keeps working.

See Depot's OIDC docs for how Depot issues these tokens and what each claim holds.

Which runs a policy matches

A policy identifies the repository with two claims, and both must match:

  • repository_id - the repository's ID, assigned by the forge that hosts it. For a GitHub repository, this is the GitHub repository ID.
  • repository - the repository's full name, such as acme/web. If you rename the repository, update the policy.

Every Depot policy must also set two conditions:

  • org_id - your Depot organization ID, so no other Depot organization's job matches
  • forge - the forge that hosts the repository, github by default. Two forges can give the same ID to different repositories.

These two pin your account, not the runs, so they don't count as the condition every policy needs. repository_owner_id also pins your account, so it doesn't count either. A policy also sets at least one of ref, event_name, or workflow_ref.

ClaimWhat it holdsExample value
org_idYour Depot organization IDabcd1234efgh
forgeThe forge that hosts the repositorygithub
refThe Git ref the run is forrefs/heads/main
event_nameThe event that started the workflowpush
workflow_refThe workflow file and the ref it ran fromacme/web/ci.yaml@refs/heads/main
repository_owner_idThe forge's ID for the account that owns the repository. It survives renames. Optional.123456

A policy that sets ref or workflow_ref must also set event_name. Pull request runs, and runs started with depot ci run, carry a branch's ref and workflow too, so without an event the policy could trust code nobody pushed.

Which runs carry which claims:

  • A run for a push to main carries event_name = push and ref = refs/heads/main.
  • A pull request from a branch in the same repository carries event_name = pull_request and ref = refs/pull/<number>/merge.
  • Pull requests from forks get no token.
  • A run you start with depot ci run, for example from your machine, carries event_name = api, so it never matches a policy that requires push.

Depot tokens don't carry environment or protected-branch claims, so a Depot policy can't pin those.

Presets in PostHog settings

Under Which runs can get a token, the settings page offers these presets. A new policy starts with both.

PresetConditions it setsWhen to use it
Your Depot organizationorg_id = <organization ID> and forge = githubEvery Depot policy for a GitHub repository
Pushes to a branch (branch main)ref = refs/heads/<branch> and event_name = pushJobs that run after code lands on a branch, such as deploys from main. Runs started with depot ci run don't match.

To use another claim from the table above, pick it under One claim of the token and enter its exact value. For a repository on another forge, set org_id and forge that way instead of the organization preset.

Why the defaults are safe

  • Pinning org_id and forge means only jobs your Depot organization runs, for the repository on that forge, can match.
  • Pushes to a branch pins both the branch and the event. Requiring event_name = push means runs that other events start, such as pull requests, don't match. Only code pushed to the branch gets a token.

Set up a trusted workload

Only organization admins and owners can add a trusted workload. Anyone else in the project can ask an admin for one.

In PostHog settings

  1. In your project settings, go to Trusted workloads and click Add trusted workload.
  2. Under Provider, pick Depot CI.
  3. In Repository, enter owner/repo and click Look up. PostHog fills in the Repository ID.
  4. Under Which runs can get a token, enter your Depot organization ID in Your Depot organization. Copy it exactly as Depot shows it in your organization settings.
  5. Keep Pushes to a branch and change the branch if it isn't main, or pick another claim.
  6. Give the trusted workload a Name, and pick the Scopes the job needs.
  7. Click Add trusted workload, then copy the posthog.json keys and the job snippet PostHog shows.

Look up works only when the forge is github. PostHog looks the repository up on GitHub, through your project's GitHub integration first and then for public repositories, like it does for GitHub Actions. For a repository on another forge, or one PostHog can't find, enter the Repository ID yourself. For a GitHub repository, you can get it with the GitHub CLI: gh api repos/<owner>/<repo> --jq .id.

With hog ci setup

Logged in with hog auth login, run this in your repository:

Terminal
hog ci setup --preset organization=<your Depot organization ID> --preset branch_push=main --scope error_tracking:write

hog finds .depot/workflows, reads owner/repo from your origin remote, asks PostHog to look up the repository ID, and applies both presets. Depot CI offers two presets:

  • --preset organization=<organization ID> - Your Depot organization, required. It also sets forge to github.
  • --preset branch_push=<branch> - Pushes to a branch

hog always adds the required preset. Without a value for it, or without a preset that says which runs may get a token, hog asks.

  • If PostHog can't look up the repository, pass --subject-id with its ID and --repository with its exact name.
  • hog ci setup sets up GitHub repositories only. For a repository on another forge, add the trusted workload in PostHog settings.
  • When the repository also has .github/workflows, hog can't tell which provider you mean, so pass --provider depot_ci.
  • List the scopes the job needs in --scope, separated by commas, such as --scope error_tracking:write,query:read.
  • hog names the policy <repository> on Depot CI.

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 lists every flag. Before you approve a request, check its Depot organization ID.

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 OIDC token

YAML
permissions:
id-token: write # lets hog get an OIDC token for PostHog
contents: read

Add these keys to the job in your Depot CI workflow that runs hog. id-token: write lets the job ask Depot for an OIDC token. Setting permissions turns off every permission you don't list, so contents: read keeps checkout working.

3. Run hog in the job

Run hog commands in the job as usual. hog sees that it runs in Depot CI and exchanges the OIDC token for you. This example uploads source maps for error tracking, so the trusted workload needs the error_tracking:write scope:

YAML
steps:
- uses: actions/checkout@v4
- uses: PostHog/setup-hog@v1
# ...build your app into ./dist
- run: hog error-tracking process ./dist

PostHog/setup-hog installs the newest hog 1.x and puts it on PATH. To keep a new hog release from changing your build, pin version and sha256, as its README shows.

Or set environment variables

Instead of posthog.json, set HOG_HOST and HOG_PROJECT_ID in the job's env. 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

ProblemLikely causeWhat to do
hog can't get an OIDC tokenThe job doesn't have id-token: writeAdd the permission to the job.
workload_audience_mismatchThe token's aud isn't the PostHog site URL hog sends it toSet the host in posthog.json, or HOG_HOST, to the address you open PostHog at.
workload_token_invalidThe token expired, its signature doesn't verify, or it lacks a claim PostHog needsRun the job again. hog asks the provider for a fresh token on each exchange.
workload_no_matching_policyThe run doesn't match the policy, for example a pull request, a run started with depot ci run, or another branch. A different Depot organization or forge, a renamed repository, 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_pendingAn admin hasn't approved the request yetSend the approval link from the error to an admin.
workload_no_usable_scopesThe policy only grants scopes a workload can't useAsk an admin to add scopes the job can use.
workload_rate_limitedThe policy used up its exchanges, or too many exchange attempts came from the runner's IP addresshog retries while the total wait stays within 60 seconds. Otherwise, run the job again later.
workload_host_not_allowed from hogposthog.json names a host that isn't PostHog Cloud, and the job doesn't set HOG_HOSTSet HOG_HOST in the job to the host the error names.
A call fails with 403The policy doesn't grant the scope the call needsThe WWW-Authenticate header names the scope. Ask an admin to add it.

See errors in the overview for every error code.

Still have questions?

Was this page useful?