From a12bbf6bc1ddccdf7b1767ad2b7c31c46533ad4f Mon Sep 17 00:00:00 2001 From: Kacper Wojciechowski <39823706+jog1t@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:02:39 +0100 Subject: [PATCH] feat: deploy with github actions oidc instead of a rivet token --- README.md | 17 +++++++++++++---- action.yml | 2 +- src/cloud.ts | 3 ++- src/config.ts | 2 ++ tests/cloud.test.ts | 13 ++++++++++++- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 257e133..43cb91b 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,17 @@ Map branches to **existing Rivet Cloud clusters**. Optionally deploy every other ## Setup -Add these GitHub Actions secrets: +Rivet Cloud authenticates the workflow with GitHub's OIDC token, so no Rivet secret is stored in GitHub: + +1. In the cluster's settings, add a GitHub Actions trust for the repository and the branch or environment that deploys, with the deploy scope. +2. Grant the job `id-token: write`, as in the workflow below. + +Each run exchanges its OIDC token for a credential that lasts 15 minutes and has only the trust's scopes. + +Alternatively, set `rivet-token` to a Rivet Cloud credential with cluster deployment access (not an Engine admin token). The action passes it to the [Rivet CLI](https://rivet.dev/docs/cli/) through `RIVET_CLOUD_TOKEN`. + +For Fly previews, add this GitHub Actions secret: -- `RIVET_CLOUD_TOKEN`: a Rivet Cloud credential with cluster deployment access in your organization. An Engine admin token cannot deploy builds. The action uses the [Rivet CLI](https://rivet.dev/docs/cli/) and passes this secret through `RIVET_CLOUD_TOKEN`. - `FLY_API_TOKEN`: a Fly organization token that can create, deploy, and delete preview apps. Only needed for Fly previews. Use the published CLI with v3 support, for example `rivet-cli-version: '3.0.0'`. @@ -34,12 +42,12 @@ jobs: runs-on: ubuntu-latest permissions: contents: read + id-token: write pull-requests: write steps: - uses: actions/checkout@v4 - uses: rivet-dev/deploy-action@v2.0.0-rc.1 with: - rivet-token: ${{ secrets.RIVET_CLOUD_TOKEN }} rivet-cli-version: '3.0.0' rivet-org: my-org branch-clusters: | @@ -74,7 +82,8 @@ Fly creates one Machine with a 20 GiB persistent volume. Increasing `fly-volume- | Input | Default | Purpose | | --- | --- | --- | | `branch-clusters` | `{}` | Branch → cluster mapping | -| `rivet-token`, `rivet-org` | — | Required for mapped Cloud deployments | +| `rivet-org` | — | Required for mapped Cloud deployments | +| `rivet-token` | — | Cloud credential; omit to use the cluster's GitHub Actions trust (`id-token: write`) | | `rivet-cli-version` | `latest` | CLI release with v3 cluster support; pin for reproducibility | | `rivet-cli-path` | — | Use a preinstalled v3 CLI instead of downloading one | | `docker-build-path` | `.` | Build context, relative to checkout | diff --git a/action.yml b/action.yml index a2aaba4..62744cd 100644 --- a/action.yml +++ b/action.yml @@ -9,7 +9,7 @@ inputs: description: 'JSON map of exact branch names to cluster names or {cluster, region, pool} objects' default: '{}' rivet-token: - description: 'Rivet Cloud credential with cluster deployment access (not an Engine admin token)' + description: 'Rivet Cloud credential with cluster deployment access (not an Engine admin token). Omit it to use the cluster''s GitHub Actions trust; needs `id-token: write`' rivet-org: description: 'Organization slug for mapped Rivet Cloud clusters' rivet-cli-version: diff --git a/src/cloud.ts b/src/cloud.ts index 8f2d37e..a146bab 100644 --- a/src/cloud.ts +++ b/src/cloud.ts @@ -6,7 +6,8 @@ export async function deployCloud( target: Cluster, command: Run = run, ): Promise<{ buildId: string; url: string }> { - if (!config.cloudToken || !config.cloudOrg) throw new Error("Cloud deployments need rivet-token and rivet-org; use a Cloud deployment credential, not an Engine admin token"); + if (!config.cloudOrg) throw new Error("Cloud deployments need rivet-org"); + if (!config.cloudToken && !config.githubOidc) throw new Error("Cloud deployments need a Cloud deployment credential: grant the workflow `id-token: write` and add a GitHub Actions trust to the cluster, or set rivet-token (not an Engine admin token)"); const program = config.cliPath || "bun"; const prefix = config.cliPath ? [] : ["x", "--package", `@rivetkit/cli@${config.cliVersion}`, "rivet"]; const options = { cwd: config.workspace, env: { RIVET_CLOUD_TOKEN: config.cloudToken, NO_COLOR: "1" } }; diff --git a/src/config.ts b/src/config.ts index 472185b..ec9932c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -80,6 +80,8 @@ export function readConfig(env: NodeJS.ProcessEnv, event: Event) { cliVersion, cliPath: env.RIVET_CLI_PATH || "", cloudToken: env.RIVET_CLOUD_TOKEN || "", + // Set only when the workflow grants `id-token: write`. + githubOidc: !!env.ACTIONS_ID_TOKEN_REQUEST_URL, cloudOrg: env.RIVET_ORG || "", githubToken: env.GITHUB_TOKEN || "", }; diff --git a/tests/cloud.test.ts b/tests/cloud.test.ts index 09c9fb7..023679a 100644 --- a/tests/cloud.test.ts +++ b/tests/cloud.test.ts @@ -68,7 +68,18 @@ test('preinstalled CLI bypasses package installation and a failed rollout is not }); test('missing deployment credentials fail without starting the CLI', async () => { - await expect(deployCloud({ ...config, cloudToken: '' }, { cluster: 'production' }, async () => { throw new Error('must not start'); })).rejects.toThrow('Cloud deployment credential'); + await expect(deployCloud({ ...config, cloudToken: '', githubOidc: false }, { cluster: 'production' }, async () => { throw new Error('must not start'); })).rejects.toThrow('Cloud deployment credential'); +}); + +test('without rivet-token the CLI exchanges the GitHub OIDC token', async () => { + const { buildId } = await deployCloud({ ...config, cloudToken: '', githubOidc: true }, { cluster: 'production' }, async (_program, args, options) => { + expect(options?.env?.RIVET_CLOUD_TOKEN).toBe(''); + if (args.includes('--help')) return '--cluster --build-context'; + if (args.includes('/v2/clusters/{cluster}/regions')) return JSON.stringify({ items: [{ region: 'one' }] }); + if (args.includes('deploy')) return 'test-build\n'; + return JSON.stringify({ build_id: 'test-build', endpoints: { public: 'https://app.example.test' } }); + }); + expect(buildId).toBe('test-build'); }); test('concurrent pool changes cannot produce a misleading success URL', async () => {