The best way to control Claude Code, Codex and more from any computer or phone

main
ts 120 lines 3.9 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as GitHub from "@alchemy.run/github";
3 import * as Alchemy from "alchemy";
4 import * as Output from "alchemy/Output";
5 import * as Config from "effect/Config";
6 import * as Effect from "effect/Effect";
7 import * as Layer from "effect/Layer";
8 import * as Redacted from "effect/Redacted";
9
10 const REPO = { owner: "alchemy-run", repository: "alchemy" } as const;
11
12 /**
13 * Repository secrets consumed by `.github/workflows`. Inputs come from
14 * Doppler: `pnpm deploy:github` runs under `doppler run -c prod`. Cloudflare
15 * API tokens are minted here rather than copied.
16 *
17 * Secrets the workflows read that have no Doppler source stay hand-managed
18 * in the repository settings:
19 * - `ALCHEMY_VERSION_BOT_ID`, `ALCHEMY_VERSION_BOT_PRIVATE_KEY` (release.yml, website.yml)
20 */
21 export default Alchemy.Stack(
22 "AlchemyGitHubSecrets",
23 {
24 providers: Layer.mergeAll(Cloudflare.providers(), GitHub.providers()),
25 state: Cloudflare.state(),
26 },
27 Effect.gen(function* () {
28 const CLOUDFLARE_API_TOKEN = yield* Config.Redacted("CLOUDFLARE_API_TOKEN");
29 const TEST_CLOUDFLARE_ACCOUNT_ID = yield* Config.String("TEST_CLOUDFLARE_ACCOUNT_ID");
30 const PROD_CLOUDFLARE_ACCOUNT_ID = yield* Config.String("PROD_CLOUDFLARE_ACCOUNT_ID");
31 const DISCORD_WEBHOOK_URL = yield* Config.Redacted("DISCORD_WEBHOOK_URL");
32
33 // The prod account token is minted with the admin token from Doppler; the
34 // test account token with the profile's own credentials.
35 const PROD_CLOUDFLARE_API_TOKEN = yield* AccountApiToken("ProdApiToken", {
36 accountId: PROD_CLOUDFLARE_ACCOUNT_ID,
37 }).pipe(
38 Effect.provide(
39 Layer.succeed(
40 Cloudflare.Credentials,
41 Effect.succeed({
42 type: "apiToken",
43 apiToken: CLOUDFLARE_API_TOKEN,
44 apiBaseUrl: "https://api.cloudflare.com",
45 }),
46 ),
47 ),
48 );
49 const TEST_CLOUDFLARE_API_TOKEN = yield* AccountApiToken("TestApiToken", {
50 accountId: TEST_CLOUDFLARE_ACCOUNT_ID,
51 });
52
53 yield* GitHub.Secrets({
54 ...REPO,
55 secrets: {
56 // website.yml previews
57 TEST_CLOUDFLARE_ACCOUNT_ID,
58 TEST_CLOUDFLARE_API_TOKEN: TEST_CLOUDFLARE_API_TOKEN.value,
59 // website.yml production deploy
60 PROD_CLOUDFLARE_ACCOUNT_ID,
61 PROD_CLOUDFLARE_API_TOKEN: PROD_CLOUDFLARE_API_TOKEN.value,
62 // release.yml
63 DISCORD_WEBHOOK_URL,
64 },
65 });
66
67 return {
68 TEST_CLOUDFLARE_ACCOUNT_ID,
69 TEST_CLOUDFLARE_API_TOKEN: TEST_CLOUDFLARE_API_TOKEN.value.pipe(Output.map(Redacted.value)),
70 PROD_CLOUDFLARE_ACCOUNT_ID,
71 PROD_CLOUDFLARE_API_TOKEN: PROD_CLOUDFLARE_API_TOKEN.value.pipe(Output.map(Redacted.value)),
72 };
73 }).pipe(Effect.orDie),
74 );
75
76 const AccountApiToken = (
77 id: string,
78 props: {
79 accountId: string;
80 },
81 ) =>
82 Cloudflare.ApiToken.AccountApiToken(id, {
83 name: "alchemy-ci",
84 accountId: props.accountId,
85 policies: [
86 {
87 effect: "allow",
88 permissionGroups: [
89 // Worker / runtime data plane
90 "Workers Scripts Write",
91 "Workers KV Storage Write",
92 "Workers R2 Storage Write",
93 "Workers Routes Write",
94 "Workers Tail Read",
95 "Workers Observability Write",
96 // Storage / data services
97 "D1 Write",
98 "Queues Write",
99 "Hyperdrive Write",
100 "Pipelines Write",
101 "Vectorize Write",
102 // Higher-level Worker features used by examples
103 "AI Gateway Write",
104 // Containers
105 "Workers Containers Write",
106 "Cloudchamber Write",
107 "Browser Rendering Write",
108 // Static assets / sites
109 "Pages Write",
110 // Misc
111 "Account Settings Write",
112 "Secrets Store Write",
113 "Logs Write",
114 ],
115 resources: {
116 [`com.cloudflare.api.account.${props.accountId}`]: "*",
117 },
118 },
119 ],
120 });