Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
Expand Down
8 changes: 7 additions & 1 deletion src/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T>(path: string): Promise<T> => 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");
Expand Down
24 changes: 24 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ function integer(raw: string, name: string, min: number, max: number): number {
return Number(raw);
}

const CLOUD_APIS: Record<string, string> = {
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);
Expand Down Expand Up @@ -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 || "",
};
}
Expand Down
21 changes: 21 additions & 0 deletions tests/cloud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
16 changes: 15 additions & 1 deletion tests/config.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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();
}
});
});
Loading