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