> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Authenticate GitHub Actions with OIDC Canonical URL: https://posthog.com/docs/api/oidc/github-actions ## On this page - [What the job needs](#what-the-job-needs) - [How the job gets a token](#how-the-job-gets-a-token) - [Which runs a policy matches](#which-runs-a-policy-matches) - [Presets in PostHog settings](#presets-in-posthog-settings) - [Why the default is safe](#why-the-default-is-safe) - [Set up a trusted workload](#set-up-a-trusted-workload) - [Configure the job](#configure-the-job) - [Troubleshooting](#troubleshooting) A GitHub Actions job can trade the OIDC token GitHub issues for each run for a short-lived PostHog token. You don't store a personal API key as a repository secret, 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 - The `id-token: write` permission, so GitHub issues an OIDC token to the job - A trusted workload in your PostHog project that matches the repository and the runs you pick - Your PostHog project ID and host, in `posthog.json` or in the job's environment PostHog trusts GitHub's issuer, `https://token.actions.githubusercontent.com`. GitHub Enterprise Cloud can customize the issuer, so tokens come from a URL that includes your enterprise's slug. PostHog doesn't support customized issuers. ## How the job gets a token When a job has `id-token: write`, GitHub sets `ACTIONS_ID_TOKEN_REQUEST_URL` and `ACTIONS_ID_TOKEN_REQUEST_TOKEN` in its environment. hog uses them to ask GitHub for an OIDC token whose audience is your PostHog host, then exchanges that token at `/oauth/token/`. GitHub's OIDC tokens live about 5 minutes, and the PostHog token never outlives the token it came from. hog keeps the PostHog token in memory and exchanges a new GitHub token when it's close to expiring, or after a `401`, so a long job keeps working. See GitHub's [OpenID Connect docs](https://docs.github.com/en/actions/concepts/security/openid-connect) and [OIDC reference](https://docs.github.com/en/actions/reference/security/oidc) for how GitHub issues these tokens. ## Which runs a policy matches A policy identifies the repository with two claims, and both must match: - **`repository_id`** - the repository's numeric ID. It never changes, so a repository that is deleted and created again with the same name doesn't match. - **`repository`** - the repository's full name, such as `acme/web`. If you rename the repository, update the policy. Conditions narrow which runs in that repository get a token. A policy sets at least one. | Claim | What it holds | Example value | | --- | --- | --- | | `ref` | The Git ref the run is for | `refs/heads/main` | | `environment` | The deployment environment the job uses | `production` | | `ref_protected` | Whether branch protection rules or rulesets cover the ref | `true` | | `event_name` | The event that started the workflow | `push` | | `workflow_ref` | The workflow file and the ref it ran from | `acme/web/.github/workflows/deploy.yml@refs/heads/main` | | `job_workflow_ref` | The reusable workflow the job called, and its ref | `acme/ci/.github/workflows/release.yml@refs/heads/main` | | `runner_environment` | Whether the job ran on a GitHub-hosted or self-hosted runner | `github-hosted` | A policy that sets `ref`, `workflow_ref`, `job_workflow_ref`, `ref_protected`, or `runner_environment` must also set `event_name` or `environment`. Pull request and comment runs, including ones from forks, carry the base branch's ref and workflow. Without an event or an environment, the policy could trust code from outside the repository. ## Presets in PostHog settings Under **Which runs can get a token**, the settings page offers these presets: | Preset | Conditions it sets | When to use it | | --- | --- | --- | | **Pushes to a branch** (default, branch `main`) | `ref` = `refs/heads/` and `event_name` = `push` | Jobs that run after code lands on a branch, such as deploys from `main` | | **A deployment environment** (environment `production`) | `environment` = `` | Jobs that deploy through a GitHub environment. Its protection rules decide who can start them. GitHub creates a missing environment without rules, so add the rules first. | To use another claim from the table above, pick it under **One claim of the token** and enter its exact value. There is no preset for protected branches. `ref_protected` is `true` whenever any branch protection rule or ruleset covers the ref, even a weak organization-wide one, so it says little about who can push. ## Why the default is safe **Pushes to a branch** pins both the branch and the event, because the branch alone isn't enough. A workflow that runs on `pull_request_target` can get an OIDC token when the pull request comes from a fork, and its `ref` is the base branch. A policy that only pins `ref` = `refs/heads/main` would give that run a token. Requiring `event_name` = `push` means only code pushed to the branch matches. In a public repository, workflows that run on `pull_request` from a fork don't get an OIDC token. In a private or internal repository, they can when **Send write tokens to workflows from pull requests** is on, so keep `event_name` or `environment` in the policy either way. ## 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 **GitHub Actions**. 3. In **Repository**, enter `owner/repo` and click **Look up**. PostHog fills in the **Repository ID**. 4. Under **Which runs can get a token**, keep **Pushes to a branch** and change the branch if it isn't `main`, or pick **A deployment 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 repository up through your project's GitHub integration first, so private repositories work when the integration can see them. Without the integration, PostHog only finds public repositories. If neither finds the repository, enter the **Repository ID** yourself. You can get it with the GitHub CLI: `gh api repos// --jq .id`. ### With `hog ci setup` Logged in with `hog auth login`, run this in your repository: ```bash hog ci setup --preset branch_push=main --scope error_tracking:write ``` hog reads `owner/repo` from your `origin` remote, asks PostHog to look up the repository ID, and applies the **Pushes to a branch** preset for `main`. GitHub Actions offers two presets, and neither is required: - `--preset branch_push=` - **Pushes to a branch** - `--preset environment=` - **A deployment environment** To pass both, repeat the flag: `--preset branch_push=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 GitHub Actions`. If PostHog can't look up the repository, pass its ID with `--subject-id` and its exact name with `--repository`: ```bash hog ci setup --preset branch_push=main --scope error_tracking:write --subject-id 123456789 --repository acme/web ``` 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 OIDC token ```yaml permissions: id-token: write # lets hog get an OIDC token for PostHog contents: read ``` Add these keys to the job that runs hog. `id-token: write` lets the job ask GitHub for an OIDC token. Setting `permissions` turns off every permission you don't list, so `contents: read` keeps `actions/checkout` working. ### 3\. Run hog in the job Run hog commands in the job as usual. hog finds GitHub's token request variables 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 jobs: build: runs-on: ubuntu-latest permissions: id-token: write # lets hog get an OIDC token for PostHog contents: read steps: - uses: actions/checkout@v4 - uses: PostHog/setup-hog@v1 # ...build your app into ./dist - run: hog error-tracking process ./dist ``` [`PostHog/setup-hog`](https://github.com/PostHog/setup-hog) installs the newest hog 1.x and puts it on `PATH`. It works on Linux, macOS, and Windows runners. 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 | Problem | Likely cause | What to do | | --- | --- | --- | | hog can't get an OIDC token | The job doesn't have `id-token: write`, or the run is a `pull_request` from a fork | Add the permission to the job. Fork pull requests get a token only in a private or internal repository with **Send write tokens to workflows from pull requests** on. | | [`workload_issuer_not_allowed`](https://posthog.com/docs/api/oidc.md#workload_issuer_not_allowed) | Your enterprise uses a customized issuer, which PostHog doesn't support | 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 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 | Run the job again. hog asks the provider for a fresh token on each exchange. | | [`workload_no_matching_policy`](https://posthog.com/docs/api/oidc.md#workload_no_matching_policy) | The run doesn't match the policy, for example a pull request, a tag, a manual run, or another branch. A renamed or recreated 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_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.