From b6ab44ff7a13669996ab448c3cdc03d49cc82729 Mon Sep 17 00:00:00 2001 From: Kacper Wojciechowski <39823706+jog1t@users.noreply.github.com> Date: Sat, 21 Nov 2026 01:36:43 +0100 Subject: [PATCH] feat: add rivet-cloud-api input to override the Cloud API endpoint --- README.md | 1 + action.yml | 4 ++++ src/cloud.ts | 8 +++++++- src/config.ts | 24 ++++++++++++++++++++++++ tests/cloud.test.ts | 21 +++++++++++++++++++++ tests/config.test.ts | 16 +++++++++++++++- 6 files changed, 72 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 257e133..e5a6d76 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ Fly creates one Machine with a 20 GiB persistent volume. Increasing `fly-volume- | --- | --- | --- | | `branch-clusters` | `{}` | Branch → cluster mapping | | `rivet-token`, `rivet-org` | — | Required for mapped Cloud deployments | +| `rivet-cloud-api` | CLI default | `production`, `staging`, or an `https://` Cloud API origin | | `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..02e0971 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,9 @@ inputs: description: 'Rivet Cloud credential with cluster deployment access (not an Engine admin token)' rivet-org: description: 'Organization slug for mapped Rivet Cloud clusters' + rivet-cloud-api: + description: 'Rivet Cloud API: production, staging, or an https:// URL; empty uses the CLI default' + default: '' rivet-cli-version: description: '@rivetkit/cli version or dist-tag with v3 --cluster support' default: latest @@ -87,6 +90,7 @@ runs: RIVET_CLI_VERSION: ${{ inputs.rivet-cli-version }} RIVET_CLI_PATH: ${{ inputs.rivet-cli-path }} RIVET_ORG: ${{ inputs.rivet-org }} + RIVET_CLOUD_API: ${{ inputs.rivet-cloud-api }} DOCKER_BUILD_PATH: ${{ inputs.docker-build-path }} DOCKERFILE_PATH: ${{ inputs.dockerfile-path }} FLY_ENABLED: ${{ inputs.fly-enabled }} diff --git a/src/cloud.ts b/src/cloud.ts index 8f2d37e..40fd7a0 100644 --- a/src/cloud.ts +++ b/src/cloud.ts @@ -16,7 +16,13 @@ export async function deployCloud( if (!help.includes("--cluster") || !help.includes("--build-context")) { throw new Error("This Rivet CLI cannot deploy v3 clusters. Set rivet-cli-version to a release supporting --cluster, or rivet-cli-path to an installed v3 binary."); } - const selection = ["--org", config.cloudOrg, "--cluster", target.cluster]; + if (config.cloudApi && !help.includes("--cloud-api")) { + throw new Error("This Rivet CLI does not support --cloud-api; upgrade it or unset rivet-cloud-api"); + } + const selection = [ + "--org", config.cloudOrg, "--cluster", target.cluster, + ...(config.cloudApi ? ["--cloud-api", config.cloudApi] : []), + ]; const api = async (path: string): Promise => JSON.parse(await cli(["api", path, ...selection], true)); // Resolve and validate the region before building or changing the pool. const regions = await api<{ items: { region: string }[] }>("/v2/clusters/{cluster}/regions"); diff --git a/src/config.ts b/src/config.ts index 472185b..7259df4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -44,6 +44,29 @@ function integer(raw: string, name: string, min: number, max: number): number { return Number(raw); } +const CLOUD_APIS: Record = { + production: "https://cloud-api.rivet.dev", + staging: "https://cloud-api.staging.rivet.dev", +}; + +export function parseCloudApi(raw: string): string { + const value = raw.trim(); + if (!value) return ""; + const alias = CLOUD_APIS[value]; + if (alias) return alias; + let url: URL; + try { + url = new URL(value); + } catch { + throw new Error("rivet-cloud-api must be production, staging, or an https:// URL"); + } + // The Cloud token and GitHub OIDC token are sent to this host. + if (url.protocol !== "https:" || url.username || url.password || url.search || url.hash || url.pathname !== "/") { + throw new Error("rivet-cloud-api must be an https:// origin without credentials, path, or query"); + } + return url.origin; +} + export function previewName(repo: string, branch: string): string { const slug = repo.split("/").pop()!.toLowerCase().replace(/[^a-z0-9-]/g, "-").slice(0, 20); const hash = createHash("sha256").update(`${repo.toLowerCase()}\0${branch}`).digest("hex").slice(0, 20); @@ -81,6 +104,7 @@ export function readConfig(env: NodeJS.ProcessEnv, event: Event) { cliPath: env.RIVET_CLI_PATH || "", cloudToken: env.RIVET_CLOUD_TOKEN || "", cloudOrg: env.RIVET_ORG || "", + cloudApi: parseCloudApi(env.RIVET_CLOUD_API || ""), githubToken: env.GITHUB_TOKEN || "", }; } diff --git a/tests/cloud.test.ts b/tests/cloud.test.ts index 09c9fb7..9cc12cb 100644 --- a/tests/cloud.test.ts +++ b/tests/cloud.test.ts @@ -79,3 +79,24 @@ test('concurrent pool changes cannot produce a misleading success URL', async () return JSON.stringify({ build_id: 'different-build', endpoints: { public: 'https://app.example.test' } }); })).rejects.toThrow('pool changed'); }); + +test('cloud api override reaches every CLI call', async () => { + const calls: string[][] = []; + const result = await deployCloud({ ...config, cloudApi: 'https://cloud-api.staging.rivet.dev' }, { cluster: 'production' }, async (_program, args) => { + const call = args.slice(4); + calls.push(call); + if (call.includes('--help')) return '--cluster --build-context --cloud-api'; + if (call[1]?.endsWith('/regions')) return JSON.stringify({ items: [{ region: 'r' }] }); + if (call[0] === 'deploy') return 'b1\n'; + return JSON.stringify({ build_id: 'b1', endpoints: { public: null } }); + }); + expect(result.buildId).toBe('b1'); + for (const call of calls.filter((c) => !c.includes('--help'))) { + expect(call[call.indexOf('--cloud-api') + 1]).toBe('https://cloud-api.staging.rivet.dev'); + } +}); + +test('cloud api override fails on a CLI without --cloud-api', async () => { + await expect(deployCloud({ ...config, cloudApi: 'https://cloud-api.staging.rivet.dev' }, { cluster: 'production' }, async () => '--cluster --build-context')) + .rejects.toThrow('--cloud-api'); +}); diff --git a/tests/config.test.ts b/tests/config.test.ts index 79471de..c5727de 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from 'bun:test'; -import { parseClusters, plan, previewName, readConfig } from '../src/config'; +import { parseCloudApi, parseClusters, plan, previewName, readConfig } from '../src/config'; const env = { GITHUB_REPOSITORY: 'example/app', GITHUB_REF_NAME: 'feature', GITHUB_EVENT_NAME: 'push' }; describe('deployment selection', () => { @@ -53,3 +53,17 @@ describe('deployment selection', () => { expect(name).not.toBe(previewName('other/app', 'feature/A')); }); }); + +describe('cloud api endpoint', () => { + test('aliases, origins and the CLI default', () => { + expect(parseCloudApi('')).toBe(''); + expect(parseCloudApi('production')).toBe('https://cloud-api.rivet.dev'); + expect(parseCloudApi('staging')).toBe('https://cloud-api.staging.rivet.dev'); + expect(parseCloudApi('https://cloud-api.example.test/')).toBe('https://cloud-api.example.test'); + }); + test('rejects endpoints that could leak credentials or misroute', () => { + for (const value of ['prod', 'http://cloud-api.rivet.dev', 'https://user:pw@cloud-api.rivet.dev', 'https://cloud-api.rivet.dev/v2', 'https://cloud-api.rivet.dev?x=1', 'cloud-api.rivet.dev']) { + expect(() => parseCloudApi(value)).toThrow(); + } + }); +});