Authenticate CI jobs with OIDC

A CI job can call PostHog without an API key. The job proves who it is with the OIDC token that its CI provider issues for each run, and PostHog exchanges that token for a PostHog token that works on one project for about 10 minutes. You store no secret in your CI provider, so there is no long-lived key to leak or rotate.

The general name for this is workload identity federation. Other platforms have the same idea under different names. Azure calls it a federated identity credential, AWS a role trust policy, Google Cloud a workload identity provider, and PyPI and npm call it trusted publishing. PostHog's trust policies follow the same OIDC model: an issuer, a subject, and conditions on the token's claims. In PostHog settings, this feature is called Trusted workloads.

Use it with hog

hog is PostHog's command line. In a CI job, hog gets the OIDC token from the provider, exchanges it, and renews it when it's close to expiring, so a job only runs hog commands. On your computer, hog ci setup asks PostHog to trust your repository and prints the job config.

Install hog with one of these:

Terminal
curl -fsSL https://releases.posthog.com/hog/install.sh | sh # macOS and Linux, into ~/.local/bin
brew install posthog/tap/hog # Homebrew
npm install -g hog # npm, or pnpm add -g, bun add -g

In GitHub Actions and Depot CI, add the PostHog/setup-hog action to the job. It installs hog, checks the download against the release checksums, and puts hog on PATH:

YAML
- uses: PostHog/setup-hog@v1

In other CI providers, use the install script. A Node.js project that has hog in its package.json already has hog in the job. Without hog, your job can do the exchange itself, as How the token exchange works describes.

Supported CI providers

Each provider page explains what the job needs, which claims a policy matches, the presets PostHog offers, and how to configure the job.

ProviderIssuerGuide
GitHub Actionshttps://token.actions.githubusercontent.comOIDC for GitHub Actions
GitLab.comhttps://gitlab.comOIDC for GitLab.com
Depot CIhttps://identity.depot.devOIDC for Depot CI

More providers may follow later.

How it works

  1. An organization admin adds a trusted workload to a project. This creates a trust policy that names the provider, the workload's subject, and the conditions a token must meet, such as a branch or an environment, plus the scopes those tokens carry.
  2. The workload asks its provider for an OIDC token whose audience is your PostHog instance.
  3. The workload sends that token to PostHog's token endpoint, naming the project.
  4. PostHog checks the token's signature, issuer, audience, and expiry, finds the project's trust policy that matches the token's claims, and returns a PostHog access token.
  5. The workload calls the PostHog API with that token until it expires, then exchanges a new OIDC token.

Workload tokens act as the oldest owner of the project's organization, limited to the project and to the scopes in the policy. Changes they make show up in the activity log under the workload, named with the claims its provider records, for example the repository, workflow, and run. The entry links to the trust policy, and to the run on GitHub Actions and GitLab.com.

Trusted workloads

Find trusted workloads in your project settings, under Trusted workloads. Only organization admins and owners can see them. If you haven't logged in recently, PostHog asks you to log in again before it shows the section.

Each trust policy has:

  • Provider - the issuer PostHog trusts for this policy.
  • Subject and name - the repository or project's ID and its readable name, from the claims the provider names. For GitHub Actions and Depot CI these are repository_id and repository, and for GitLab.com they're project_id and project_path. Both must match. The ID never changes, so a repository that is deleted and created again with the same name doesn't match.
  • Conditions - claims the token must carry with an exact value, such as a branch or an environment. Each provider lists the claims a policy can use, and when it lists any, a policy must set at least one. A condition you leave empty matches any value. A token that doesn't carry a condition's claim never matches. Values are matched exactly, without wildcards.
  • Scopes - every token from the policy gets all of these scopes, and no others.

When more than one policy matches a token, the most specific one wins: a policy with more conditions beats one with fewer. If two are equally specific, the older one wins.

Turning a policy off, editing it, or deleting it stops its tokens from working within about 10 seconds. Every change emails the organization's admins and owners and appears in the activity log.

Add a trusted workload in settings

  1. In your project settings, go to Trusted workloads and click Add trusted workload.
  2. Under Provider, pick your CI provider.
  3. Enter the repository or project name and click Look up. PostHog fills in its ID. If PostHog can't find it, enter the ID yourself. Each provider page says where to find it.
  4. Under Which runs can get a token, pick one of the common choices, or pick one claim of the token and enter its value. Click Add condition to add more. A token must meet every condition.
  5. Give the trusted workload a Name, and pick its Scopes.
  6. Click Add trusted workload. PostHog shows the posthog.json keys and the snippet for your CI job, with your PostHog address and project ID filled in, and buttons to copy them.

Scopes that need care

A policy can grant almost any scope. Some scopes let a token do things that reach beyond the project, last longer than the token, or touch credentials. PostHog shows a warning next to these scopes when you pick them for a trusted workload or a personal API key, and on the approval page. Add them only when the workload needs them.

Privileged and hidden scopes, such as llm_gateway, are never available to a workload. A few more scopes can never be granted to a workload either, because they would let a token outlive itself, change its own policy, or read one person's private data:

  • workload_trust_policy and workload_trust_policy_request
  • project:write, which can rotate the project's secret token
  • user:write, which changes the account of the organization owner the token acts as
  • task:write, conversation:write, signal_scout:write, business_knowledge:write, autoresearch:write, and hog_flow:write, which start agent runs with their own longer-lived tokens (a workflow can create a task)
  • streamlit_app, which hands out a one-hour token for a running app
  • webhook:write, batch_export:write, and subscription:write, which set up data feeds or emails that keep sending after the token expires
  • sharing_configuration, because a public sharing link keeps working after the token expires
  • conversation:read and task:read, because a workload token acts as an organization owner, and these read that person's private PostHog AI conversations and tasks

Asking an admin for a trusted workload

Any project member can ask for a trusted workload. A request grants nothing on its own. It gives you an approval link to send to an organization admin or owner. They open the link, review the policy, who asked for it and, for an OAuth token, through which app, and approve or reject it. Approving creates the trust policy. Requests expire after 24 hours and can't be edited. Each person can create 10 requests an hour and 30 a day in a project.

Ask with hog ci setup

Log in with hog auth login and run hog ci setup in your repository:

Terminal
hog ci setup --preset branch_push=main --scope error_tracking:write

The command:

  1. Detects the provider from the CI config in the repository: .github/workflows, .gitlab-ci.yml, or .depot/workflows. It reads owner/repo or the GitLab project path from your origin remote.
  2. Reads the provider's presets, questions, and job snippet from PostHog, so it works the same way for every provider.
  3. Applies the presets you pass with --preset, plus any the provider requires. It asks for anything still missing, such as which runs may get a token. When no person can answer, such as when an agent runs it, it returns status: input_required with the presets and values it still needs, and PostHog's explanation of each.
  4. Asks PostHog to look up the repository or project's ID. If PostHog can't, pass the ID with --subject-id.
  5. Creates a policy request named <repository> on <provider>. If you're an admin, hog opens the approval page and waits. Otherwise it names the admins and prints the link to send them.
  6. Prints the posthog.json keys and the snippet for your CI job.

Each provider has its own preset keys. A preset that takes a value is passed as --preset <key>=<value>.

ProviderPresetsRequiredA new policy in settings starts with
GitHub Actionsbranch_push=<branch>, environment=<environment>Nonebranch_push
GitLab.combranch=<branch>, environment=<environment>Nonebranch
Depot CIorganization=<organization ID>, branch_push=<branch>organizationorganization and branch_push

FlagWhat it does
--preset <key>=<value>Applies one of the provider's presets, such as branch_push=main. Repeat it for each preset.
--scope <scopes>Sets the scopes the job gets, separated by commas, such as error_tracking:write,query:read. Repeating the flag works too. At least one scope is required.
--provider <key>Names the provider: github_actions, gitlab, or depot_ci. Pass it when hog can't tell, or when the repository has config for more than one provider.
--repository <name>Names the repository or project when it isn't your origin remote
--subject-id <id>Sets the immutable ID when PostHog can't look it up. Pass --repository with the exact name too, because PostHog matches both.
--waitWaits for a request an earlier run started. It stops after 10 minutes and keeps the request, so you can run it again.

Ask through the API

Send a request with an OAuth token or a personal API key that has the workload_trust_policy_request:write scope. This example asks to trust pushes to main from a GitHub Actions repository:

Terminal
curl -X POST "https://us.posthog.com/api/projects/<project_id>/workload_trust_policy_requests/" \
-H "Authorization: Bearer <oauth_access_token>" \
-H "Content-Type: application/json" \
-d '{
"name": "Production deploys",
"issuer": "https://token.actions.githubusercontent.com",
"subject_name": "acme/web",
"subject": "123456789",
"conditions": {"ref": "refs/heads/main", "event_name": "push"},
"scopes": ["error_tracking:write"]
}'

The keys in conditions are claim names from the provider's OIDC token, and only the claims the provider lists are allowed. On GitHub Actions, a ref, ref_protected, workflow_ref, job_workflow_ref, or runner_environment condition also needs event_name or environment, so a pull_request_target run from a fork can't match. The response includes an approval_url to send to an admin.

How the token exchange works

The job sends its OIDC token to PostHog's token endpoint, /oauth/token/, as an RFC 8693 token exchange that names the project. PostHog verifies the token, finds the matching trust policy, and returns a PostHog access token with the policy's scopes. The token works for at most 10 minutes. hog does all of this for you, as How hog exchanges tokens describes.

Without hog, send the exchange yourself. No client credentials are needed:

Terminal
curl -X POST "https://us.posthog.com/oauth/token/" \
-d "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" \
-d "subject_token_type=urn:ietf:params:oauth:token-type:id_token" \
-d "subject_token=$OIDC_TOKEN" \
-d "resource=https://us.posthog.com/api/projects/<project_id>"

The response has the PostHog token in access_token, with token_type, expires_in, and scope. Send it as Authorization: Bearer <access_token>. The OIDC token's audience must be the same PostHog address, here https://us.posthog.com.

The token never outlives the OIDC token it came from, and there is no refresh token. When a call needs a scope the policy doesn't have, the API answers with a 403, a WWW-Authenticate header that names the scopes the call needs, and missing_scopes in the body. Ask an admin to add them to the policy. When the call needs a scope that no trust policy can grant, the 403 says so, and the call needs a personal API key instead.

The exchange has two rate limits. Past either one, PostHog answers with a 429, a Retry-After header, and the workload_rate_limited error.

  • Per policy - each trust policy can make 300 exchanges at once, and gets one more each second. Runs that another policy covers don't use up this budget.
  • Per IP address - before PostHog verifies a token, it limits the exchange attempts from one IP address to 600 a minute and 7,200 an hour. CI runners often share an IP address, so these limits are twice one policy's budget.

How hog exchanges tokens

hog does the exchange for you:

  • It reads the project ID from projectId in posthog.json, or from HOG_PROJECT_ID. The environment variable wins when both are set.
  • On GitHub Actions and Depot CI, it asks the provider for an OIDC token whose audience is the PostHog host it sends the token to. On GitLab.com, it reads the ID token from HOG_ID_TOKEN, whose audience you set in the job. That host is HOG_HOST, or else a PostHog Cloud host in posthog.json, or else https://us.posthog.com.
  • It reads a host from posthog.json only when it's PostHog Cloud, so a change to the file can't send the job's token elsewhere. A self-hosted PostHog needs HOG_HOST. When posthog.json names another host and HOG_HOST isn't set, hog stops with a workload_host_not_allowed error.
  • It sends the RFC 8693 exchange to /oauth/token/ and keeps the PostHog token in memory only. It never writes the token to disk. If PostHog answers with a redirect, hog stops with workload_exchange_redirected and doesn't send the OIDC token to the new address.
  • It exchanges again when the token is close to expiring, or after the API answers with a 401.
  • When the exchange is rate limited, it waits for the time PostHog gives, plus up to the same time again at random, and tries again up to three times, as long as the total wait stays within 60 seconds.
  • It picks credentials in this order: HOG_PERSONAL_API_KEY, then a key from HOG_DOTENV_FILE, then the workload token, then a stored login. Remove HOG_PERSONAL_API_KEY from the job's environment, or hog uses that instead of the workload token. Never put it in posthog.json.
  • Its usage analytics from a job that uses a workload token carry an ID hashed from the provider and the repository, the provider's name, and your organization. They aren't tied to a person. Turn them off with HOG_NO_TELEMETRY=1.

Errors

A refused exchange returns an OAuth error body with an error_code and an error_uri that links to the matching entry below. When PostHog itself has a temporary problem, the exchange returns a 503 with temporarily_unavailable and no error_code. Try again later.

workload_invalid_request

The request is missing subject_token, uses an unsupported subject_token_type, or resource isn't a project URL on this PostHog instance.

workload_issuer_not_allowed

The OIDC token comes from an issuer PostHog doesn't trust. Only the supported providers are trusted.

workload_token_invalid

PostHog couldn't verify the OIDC token: it isn't a readable JWT, it expired, its signature doesn't verify, or it lacks a claim PostHog needs, such as the subject or the name. On GitLab.com, PostHog also refuses a token from a job that runs in another project than the one the token names, where job_project_id differs from project_id.

workload_audience_mismatch

The token's aud isn't this PostHog instance's site URL. The error names the audience PostHog expects. Ask your provider for a token with that audience. With hog, put the address you open PostHog at in posthog.json as host, or set HOG_HOST.

workload_provider_unavailable

PostHog couldn't fetch the provider's signing keys. Try again in a minute.

workload_no_matching_policy

No enabled trust policy in the project matches the token's subject, name, and conditions. The same error covers a project or environment ID that doesn't exist, so the exchange can't be used to find out which projects exist.

The error lists the claims the job sent, so you can compare them with the policy. To ask for a policy that matches, run hog ci setup in the repository.

workload_policy_request_pending

No policy matches yet, but a request for one is waiting for an admin. The error includes the request's approval link.

workload_no_usable_scopes

The matching policy only grants scopes a workload token can't use. Ask an admin to edit the policy.

workload_rate_limited

Either the trust policy used up its 300 exchanges, which refill at one each second, or too many exchange attempts came from one IP address before verification. The response has a 429 status and a Retry-After header with the seconds to wait. hog retries while its total wait stays within 60 seconds. Otherwise, run the job again later.

workload_no_owner

The project's organization has no active owner for workload tokens to act as.

workload_not_configured

This PostHog instance isn't set up to issue workload tokens.

Still have questions?

Was this page useful?