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

# Connect GitHub Actions Without an API Key

> Let PMG and vet in GitHub Actions sync with SafeDep Cloud through GitHub OIDC, with no SafeDep API key in your GitHub secrets.

PMG and vet in GitHub Actions can call SafeDep Cloud with a short-lived credential instead of an API key. The job exchanges its GitHub OIDC ID token for the credential, so GitHub stores no SafeDep secret. The tools call this OIDC mode. For how the exchange works and what the credential can do, see [Workload identity](https://docs.safedep.io/governance/cloud/authentication.md#workload-identity).

> [!NOTE]
>
> This guide needs the SafeDep Cloud Team or Enterprise plan. See [pricing](https://safedep.io/pricing).

## Prerequisites

* **An owner of your GitHub organization**, for step 1.
* **A SafeDep Cloud Owner or Admin** of your tenant, for steps 2 and 3.
* **Your tenant domain**, such as `your-team.your-org.safedep.io`. The `tenant-id` input of the action takes this value. `safedep auth status` shows it in the tenant field. The **Tenant ID** button on the [API keys settings page](https://app.safedep.io/settings/api-keys) also shows it.
* **The [safedep CLI](https://docs.safedep.io/get-started/cli-tools.md)** `v0.12.0` or later, signed in with `safedep auth login`.
* **For PMG:** the `safedep/pmg` action `v0.37.0` or later, which installs PMG `v0.37.0` or later. `safedep/pmg@v2` with the default `version: latest` meets this.
* **For vet:** the vet v2 GitHub Action. `safedep/vet-action@v1` has no OIDC mode and needs an API key.

## Set up the tenant

**1. Link the SafeDep GitHub App**

An owner of the GitHub organization installs the [SafeDep GitHub App](https://docs.safedep.io/governance/integrations/github.md), and links the installation to your tenant when SafeDep Cloud asks.

If the app is already installed:

1. An owner of the GitHub organization accepts the new permission **Members: read** of the SafeDep GitHub App in the organization settings on GitHub.
2. An owner of the GitHub organization links the installation to your tenant again, as at install time.

SafeDep Cloud checks with GitHub that the person is an owner of the organization. A link by a user who is not an owner still runs the pull request scans and the project sync. It creates no provider, so jobs get no credential. For a personal account, the account itself links the installation.

**2. Check the provider**

A provider is the record in your tenant that accepts the GitHub OIDC tokens of one GitHub account. The link of an owner creates it. List the providers:

```bash theme={null}
safedep workload provider list
```

```
Workload identity providers
PROVIDER             ID       OWNER ID  LINK    CREATED
github-actions:acme  wip_7a1  1001      active  2d ago
1 provider
```

`LINK` must be `active`. If your account has no row, see [Troubleshoot](#troubleshoot).

**3. Create the org-wide rule**

Replace `acme` with the account in the `PROVIDER` column:

```bash theme={null}
safedep workload rule create org --provider github-actions:acme --grant tool
```

`--grant tool` lets a job make only the calls that PMG and vet use. For the list, see [What the credential can do](https://docs.safedep.io/governance/cloud/authentication.md#what-the-credential-can-do).

> [!WARNING]
>
> A rule with no `--repository` covers every repository of the GitHub account. This includes the repositories that the app installation does not select. The command prints the same warning.

The rule covers only the [default events](#events-that-a-rule-covers). When a repository has no project, its first run that publishes scan results creates the project. This works only when the app installation selects the repository.

## Change the workflow

Give the job `id-token: write`, set `tenant-id`, and remove `api-key`. Store the tenant domain as a GitHub Actions variable, because it is not a secret. These examples use a variable with the name `SAFEDEP_CLOUD_TENANT_DOMAIN`.

> [tab] PMG
>
> ```yaml theme={null}
> permissions:
>   contents: read
>   id-token: write
>
> jobs:
>   install-deps:
>     runs-on: ubuntu-latest
>     timeout-minutes: 30
>     steps:
>       - uses: actions/checkout@v6
>       - uses: safedep/pmg@v2
>         with:
>           tenant-id: ${{ vars.SAFEDEP_CLOUD_TENANT_DOMAIN }}
>       - run: npm ci
> ```
>
> The action gets the credential before it starts the proxy. Its post step revokes the credential. See [What happens at the end of the job](https://docs.safedep.io/package-security/pmg/github-actions#what-happens-at-the-end-of-the-job). In OIDC mode, the action ignores the `endpoint-id` input and prints a warning. For the result of each failure case, see [Use GitHub OIDC instead of an API key](https://github.com/safedep/pmg/blob/main/docs/github-action.md#use-github-oidc-instead-of-an-api-key) in the PMG action documentation.

> [tab] vet v2
>
> ```yaml theme={null}
> permissions:
>   contents: read
>   pull-requests: write
>   id-token: write
>
> jobs:
>   vet:
>     runs-on: ubuntu-latest
>     steps:
>       - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
>         with:
>           persist-credentials: false
>       - uses: safedep/vet@<commit SHA> # <release tag>
>         with:
>           tenant-id: ${{ vars.SAFEDEP_CLOUD_TENANT_DOMAIN }}
> ```
>
> Replace `<commit SHA>` with the commit of a v2 release tag from the [vet releases](https://github.com/safedep/vet/releases) page. vet gets the credential and revokes it when the scan ends. See [SafeDep Cloud](https://github.com/safedep/vet/blob/v2/docs/github-action.md#safedep-cloud) in the vet v2 action documentation.

## Check the credential

1. Run the workflow.
2. List the exchanges of the last day:

   ```bash theme={null}
   safedep workload exchange list --since 1d
   ```

Each tool that got a credential shows one row with the outcome `issued`. After the job ends, the `DETAIL` column of a revoked credential says `revoked`.

If the list is empty, no exchange reached your provider. Read the log of the PMG or vet step, and see [Troubleshoot](#troubleshoot).

## Move from an API key

While the job sets `api-key`, the PMG action and the vet action use the API key. So you can move one workflow at a time.

> [!WARNING]
>
> In OIDC mode, the endpoint of a job is `gh:<owner>/<repo>`. The PMG action uses the same default with an API key, so that endpoint and its history stay the same. A job that used another endpoint ID reports under a new endpoint. This applies to a job with an `endpoint-id` input, and to a job on an older action that used `gha/<owner>/<repo>`. The history stays on the old endpoint. Labels on the old endpoint do not move.

**1. Add the permission**

Add `id-token: write` to the permissions of the job.

**2. Remove the API key input**

Remove the `api-key` input. Keep `tenant-id`.

**3. Run the workflow**

Run the workflow, and [check the credential](#check-the-credential).

**4. Change the other workflows**

Do steps 1 to 3 for each other workflow that uses the API key.

**5. Delete the secret**

When no workflow uses the API key, delete its secret from GitHub.

**6. Delete the API key**

Delete the API key on the [API keys settings page](https://app.safedep.io/settings/api-keys).

## Narrow the rule

For each flag, see the [`safedep workload` command reference](https://github.com/safedep/cli/tree/main/docs/cmd).

A job gets a credential when any rule matches it. A narrower rule does not limit the org-wide rule. To give credentials to some repositories only, delete the org-wide rule. Then create rules that name the repositories:

```bash theme={null}
safedep workload rule delete org
safedep workload rule create web-release --provider github-actions:acme --grant tool \
  --repository acme/web --ref 'refs/heads/release/*'
```

* `--repository` names a repository of the account. The server stores its numeric ID, so a rename does not change the rule. Repeat the flag for more repositories.
* `--ref` takes a full ref, such as `refs/heads/main`, or a prefix that ends with `/*`, such as `refs/heads/release/*`.
* `--event` lists the events that the rule covers. See [Events that a rule covers](#events-that-a-rule-covers).
* `--max-lifetime` sets the longest life of a credential, from `1m` to `6h`. The default is `6h`. PMG cannot refresh its credential, so give a PMG job a maximum life that is longer than the job.
* `--project-binding manual` stops a run from creating a project for a repository. A member adds the repository as a project first. The default is `auto` for a rule with no `--repository`, and `manual` for a rule with `--repository`.

Each of `--repository`, `--ref` and `--event` adds one condition to the rule. When more than one rule matches a job, SafeDep Cloud uses the rule with the most conditions. Its `--max-lifetime` and `--project-binding` apply. When two such rules have the same number of conditions, the older rule applies.

The CLI has no condition on the runner. For self-hosted runners, see [Self-hosted runners](https://docs.safedep.io/governance/cloud/authentication.md#self-hosted-runners).

### Events that a rule covers

A rule with no `--event` covers only these default events: `push`, `pull_request`, `merge_group`, `schedule`, `workflow_dispatch` and `release`.

An outsider can start most other events, so a rule must list them. For the reason, see [Which events a rule covers](https://docs.safedep.io/governance/cloud/authentication.md#which-events-a-rule-covers). To allow an event such as `pull_request_target`, create a rule that lists it, and name the repositories that need it:

```bash theme={null}
safedep workload rule create web-prt --provider github-actions:acme --grant tool \
  --repository acme/web --event pull_request_target
```

A rule with `--event` covers only the events that it lists. A run of an event outside the defaults can fetch the policy, check in and send events. It publishes no scan results to a project.

## Troubleshoot

When SafeDep Cloud refuses the exchange, the PMG step fails. The error and the job summary name the tenant and the reason that SafeDep Cloud gives. vet scans with no SafeDep Cloud data and writes the reason in the `workload_exchange_failed` diagnostic. To see the refused exchanges of your tenant, run `safedep workload exchange list --denied --since 1d`.

The rows are in the order that a PMG job meets them. The last row is for the admin who creates rules.

| Message | Cause | Fix |
| - | - | - |
| PMG: `This pull_request run comes from a fork, and GitHub gives it no ID token`. vet: `GitHub gives no ID token to a pull request from a fork` | GitHub gives no ID token to a pull request from a fork. The tool runs without SafeDep Cloud. | No fix. See [Fork pull requests](https://docs.safedep.io/governance/cloud/authentication.md#fork-pull-requests). |
| `OIDC mode needs a PMG release with "pmg cloud workload exchange"` | The `version` input names an older PMG release. | Set `version` to `v0.37.0` or later, or remove it. |
| PMG: `PMG cannot get a SafeDep Cloud credential, because the job has no ID token`. vet: `The tenant-id input with no api-key input needs an ID token of GitHub` | The job has no `id-token: write`. The step fails. | Add `id-token: write` to the permissions of the job. |
| `no workload identity provider of the tenant trusts the owner of the token` | The tenant has no provider with an active link for the GitHub account, or `tenant-id` is wrong. SafeDep Cloud records no exchange, so `exchange list` shows nothing. | Check `tenant-id`. Then do [Set up the tenant](#set-up-the-tenant) again: the installation needs **Members: read**, and an owner of the GitHub organization must link it. If the organization uses SAML single sign-on, the owner needs an active SSO session on GitHub. |
| `the plan of the tenant does not include workload identity` | The tenant is not on the Team or Enterprise plan. | Change the plan. See [pricing](https://safedep.io/pricing). |
| `no workload trust rule gives TOOL to acme/web on refs/heads/feature-x` | No rule matches the repository and the ref. | Create a rule, or remove a condition. |
| `no workload trust rule gives TOOL to acme/web on refs/heads/main for the event issues` | The event is not a default event. | See [Events that a rule covers](#events-that-a-rule-covers). |
| `the endpoint gh:acme/web belongs to another repository that once had this name` | A new repository took the name of a deleted or renamed repository. | An admin deletes the old endpoint, or the job uses an API key with its own endpoint ID. |
| `--provider github-actions:acme names more than one provider` | You uninstalled and installed the SafeDep GitHub App again. The new link has a new provider, and the rules of the old provider do not apply to it. | Run `safedep workload provider list`. Create the rules again with the ID of the provider whose `LINK` is `active`. |

> [card] [Workload Identity](https://docs.safedep.io/concepts/workload-identity.md)
>
> See how the exchange works and what a compromised step can do.

> [card] [PMG in GitHub Actions](https://docs.safedep.io/package-security/pmg/github-actions)
>
> Block malicious packages in GitHub Actions.

> [card] [Authentication](https://docs.safedep.io/governance/cloud/authentication.md)
>
> See the other ways that tools sign in to SafeDep Cloud.

> [card] [Review CI Blocks](https://docs.safedep.io/governance/cloud/endpoint-hub/ci-package-blocks.md)
>
> See the packages that PMG blocked across your pipelines.


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