Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

github.ts

169 lines5.7 KB

1import * as Alchemy from "alchemy";
2import * as AWS from "alchemy/AWS";
3import * as Cloudflare from "alchemy/Cloudflare";
4import * as GitHub from "alchemy/GitHub";
5import * as Neon from "alchemy/Neon";
6import * as Output from "alchemy/Output";
7import * as Config from "effect/Config";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10import * as Redacted from "effect/Redacted";
11​
12const REPO = { owner: "alchemy-run", repository: "alchemy-effect" } as const;
13​
14export default Alchemy.Stack(
15 "AlchemyGitHubSecrets",
16 {
17 providers: Layer.mergeAll(
18 AWS.providers(),
19 Cloudflare.providers(),
20 GitHub.providers(),
21 Neon.providers(),
22 ),
23 state: Cloudflare.state(),
24 },
25 Effect.gen(function* () {
26 const testAccountId = yield* Config.string("TEST_CLOUDFLARE_ACCOUNT_ID");
27 const prodAccountId = yield* Config.string("PROD_CLOUDFLARE_ACCOUNT_ID");
28 const discordWebhookUrl = yield* Config.string("DISCORD_WEBHOOK_URL");
29 const prPackageAuthToken = yield* Config.string("PR_PACKAGE_TOKEN");
30​
31 const testApiToken = yield* token("TestApiToken", {
32 accountId: testAccountId,
33 });
34​
35 const prodApiToken = yield* token("ProdApiToken", {
36 accountId: prodAccountId,
37 });
38​
39 // GitHub OIDC trust for AWS — lets `.github/workflows/test.yml` (and any
40 // future workflow) assume an IAM role via `aws-actions/configure-aws-credentials`
41 // with no long-lived AWS_ACCESS_KEY_ID secrets in the repo.
42 const oidc = yield* AWS.IAM.OpenIDConnectProvider("GitHubOidc", {
43 url: "https://token.actions.githubusercontent.com",
44 clientIDList: ["sts.amazonaws.com"],
45 // GitHub's well-known OIDC thumbprint. AWS auto-discovers thumbprints
46 // for github.com these days, but our `iam.updateOpenIDConnectProviderThumbprint`
47 // sync still requires a non-empty list when comparing against the
48 // cloud-observed value.
49 // https://aws.amazon.com/blogs/security/use-iam-roles-to-connect-github-actions-to-actions-in-aws/
50 thumbprintList: ["6938fd4d98bab03faadb97b34396831e3780aea1"],
51 });
52​
53 const role = yield* AWS.IAM.Role("GitHubActionsRole", {
54 roleName: "alchemy-github-actions",
55 assumeRolePolicyDocument: {
56 Version: "2012-10-17",
57 Statement: [
58 {
59 Effect: "Allow",
60 Principal: {
61 Federated: oidc.openIDConnectProviderArn,
62 },
63 Action: ["sts:AssumeRoleWithWebIdentity"],
64 Condition: {
65 StringEquals: {
66 "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
67 },
68 // Restrict to any branch / PR / tag inside this repo. Tighten
69 // further (e.g. `repo:.../environment:prod`) once we wire up
70 // GitHub Environments.
71 StringLike: {
72 "token.actions.githubusercontent.com:sub": `repo:${REPO.owner}/${REPO.repository}:*`,
73 },
74 },
75 },
76 ],
77 },
78 // The smoke suite deploys real Cloudflare workers, AWS Lambdas, S3
79 // buckets, DynamoDB tables, etc., so it needs broad access. Swap for
80 // a custom-managed policy enumerating `lambda:*`, `dynamodb:*`, … if
81 // you want least-privilege CI.
82 managedPolicyArns: ["arn:aws:iam::aws:policy/AdministratorAccess"],
83 });
84​
85 const region = yield* AWS.Region;
86​
87 yield* GitHub.Secrets({
88 ...REPO,
89 secrets: {
90 TEST_CLOUDFLARE_API_TOKEN: testApiToken.value,
91 TEST_CLOUDFLARE_ACCOUNT_ID: testAccountId,
92 PROD_CLOUDFLARE_API_TOKEN: prodApiToken.value,
93 PROD_CLOUDFLARE_ACCOUNT_ID: prodAccountId,
94 DISCORD_WEBHOOK_URL: discordWebhookUrl,
95 PR_PACKAGE_TOKEN: prPackageAuthToken,
96 NEON_API_KEY: (yield* Neon.NeonEnvironment).apiKey,
97 },
98 });
99​
100 // Role ARN + region are not secret — publish as repo-level Variables
101 // so workflows can reference `vars.AWS_ROLE_ARN` / `vars.AWS_REGION`.
102 yield* GitHub.Variables({
103 ...REPO,
104 variables: {
105 AWS_ROLE_ARN: role.roleArn,
106 AWS_REGION: region,
107 },
108 });
109​
110 return {
111 TEST_CLOUDFLARE_API_TOKEN: testApiToken.value.pipe(
112 Output.map(Redacted.value),
113 ),
114 TEST_CLOUDFLARE_ACCOUNT_ID: testAccountId,
115 PROD_CLOUDFLARE_API_TOKEN: prodApiToken.value.pipe(
116 Output.map(Redacted.value),
117 ),
118 PROD_CLOUDFLARE_ACCOUNT_ID: prodAccountId,
119 DISCORD_WEBHOOK_URL: discordWebhookUrl,
120 AWS_ROLE_ARN: role.roleArn,
121 AWS_REGION: region,
122 };
123 }).pipe(Effect.orDie),
124);
125​
126const token = (
127 id: string,
128 props: {
129 accountId: string;
130 },
131) =>
132 Cloudflare.AccountApiToken(id, {
133 accountId: props.accountId,
134 policies: [
135 {
136 effect: "allow",
137 permissionGroups: [
138 // Worker / runtime data plane
139 "Workers Scripts Write",
140 "Workers KV Storage Write",
141 "Workers R2 Storage Write",
142 "Workers Routes Write",
143 "Workers Tail Read",
144 "Workers Observability Write",
145 // Storage / data services
146 "D1 Write",
147 "Queues Write",
148 "Hyperdrive Write",
149 "Pipelines Write",
150 "Vectorize Write",
151 // Higher-level Worker features used by examples
152 "AI Gateway Write",
153 // Containers
154 "Workers Containers Write",
155 "Cloudchamber Write",
156 "Browser Rendering Write",
157 // Static assets / sites
158 "Pages Write",
159 // Misc
160 "Account Settings Write",
161 "Secrets Store Write",
162 "Logs Write",
163 ],
164 resources: {
165 [`com.cloudflare.api.account.${props.accountId}`]: "*",
166 },
167 },
168 ],
169 });