Skip to content

Commit d650d39

Browse files
committed
docs: document bring-your-own R2 deployment
1 parent 6a790bc commit d650d39

2 files changed

Lines changed: 63 additions & 16 deletions

File tree

‎README.md‎

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ The service does not receive GitHub credentials and does not call GitHub. Anyone
66

77
## Agent usage
88

9-
Configure credentials once at the user level, then expose the included CLI:
9+
Deploy the service once or configure an existing profile, then expose the included CLI:
1010

1111
```bash
1212
config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/github-pr-attachments"
1313
mkdir -p "$config_dir"
1414
chmod 700 "$config_dir"
1515
$EDITOR "$config_dir/token"
16+
$EDITOR "$config_dir/url"
1617
chmod 600 "$config_dir/token"
18+
chmod 600 "$config_dir/url"
1719

1820
npm install
1921
npm link
2022
github-attach screenshot.png --alt "Settings after the change"
2123
```
2224

23-
The token file contains only the raw token, with no variable name or quotes.
25+
The `token` file contains only the raw upload token. The `url` file contains the Worker origin, such as `https://github-pr-attachments.example.workers.dev`. Neither file uses variable names or quotes. `npm run setup` creates both files automatically.
2426

2527
### Credential resolution
2628

@@ -40,7 +42,15 @@ GITHUB_ATTACHMENTS_TOKEN=repository-specific-token
4042

4143
The repository `.env` must remain uncommitted. This repository ignores `.env` and `.env.*` by default; copy the same rules into repositories that do not already ignore them. Use `.env.example` for placeholder documentation only—never put a real token in it.
4244

43-
`GITHUB_ATTACHMENTS_URL` can be set only in the process environment and otherwise defaults to `https://github-pr-attachments.none23.workers.dev`. Repository `.env` cannot redirect a user-level token to another service. The user token file is not parsed as shell code, and repository `.env` files are read as data rather than sourced.
45+
The service URL resolves separately:
46+
47+
| Priority | Location | Scope |
48+
| ---: | --- | --- |
49+
| 1 | `GITHUB_ATTACHMENTS_URL` in the process environment | Current process override |
50+
| 2 | `${XDG_CONFIG_HOME:-~/.config}/github-pr-attachments/url` | User-level deployment |
51+
| 3 | `https://github-pr-attachments.none23.workers.dev` | Backward-compatible default |
52+
53+
Repository `.env` cannot redirect a user-level token to another service. User profile files and repository `.env` are parsed as data, never sourced as shell code.
4454

4555
The CLI prints only Markdown by default:
4656

@@ -104,31 +114,63 @@ npm run dev
104114

105115
`npm run check` runs Biome, generated binding drift checks, strict TypeScript checks, Workers-runtime integration tests, and the CLI integration test.
106116

107-
## Initial deployment
117+
## Deploy your own service
108118

109-
Authenticate Wrangler first:
119+
Each deployment has one Worker, one upload token, and one statically bound R2 bucket. Upload requests cannot choose or override the bucket.
120+
121+
Install dependencies, then create a least-privilege Cloudflare API token with these account permissions:
122+
123+
- Workers Scripts: Edit
124+
- Workers R2 Storage: Edit
125+
- Account Settings: Read
126+
127+
Limit it to the target account. A short expiration and client-IP restriction further reduce its blast radius when appropriate. Save it outside Git:
110128

111129
```bash
112-
npx wrangler login
130+
deploy_config="${XDG_CONFIG_HOME:-$HOME/.config}/github-pr-attachments"
131+
mkdir -p "$deploy_config"
132+
chmod 700 "$deploy_config"
133+
$EDITOR "$deploy_config/deploy.env"
134+
chmod 600 "$deploy_config/deploy.env"
113135
```
114136

115-
Create the dedicated private bucket and its lifecycle rule:
137+
`deploy.env` contains:
138+
139+
```dotenv
140+
CLOUDFLARE_ACCOUNT_ID=your-account-id
141+
CLOUDFLARE_API_TOKEN=your-deployment-token
142+
```
143+
144+
Create a new private bucket and deploy:
116145

117146
```bash
118-
npx wrangler r2 bucket create github-pr-attachments
119-
npx wrangler r2 bucket lifecycle add \
120-
github-pr-attachments expire-attachments objects/ \
121-
--expire-days 180 --force
147+
npm install
148+
npm run setup -- \
149+
--worker github-pr-attachments-yourname \
150+
--bucket github-pr-attachments-yourname \
151+
--create-bucket
122152
```
123153

124-
Generate a random token, save it somewhere private, and set the Worker secret through Wrangler's interactive prompt:
154+
To reuse an existing private bucket, omit `--create-bucket`:
125155

126156
```bash
127-
npx wrangler secret put UPLOAD_TOKEN
128-
npm run deploy
157+
npm run setup -- \
158+
--worker github-pr-attachments-yourname \
159+
--bucket your-existing-private-bucket \
160+
--prefix github-pr-attachments-yourname/objects/
129161
```
130162

131-
Never commit `.dev.vars`, bearer tokens, or generated credential files.
163+
The setup command:
164+
165+
1. Verifies or creates the named bucket.
166+
2. Adds a prefix-scoped R2 lifecycle rule.
167+
3. Generates `wrangler.user.jsonc` with the selected Worker, bucket, prefix, and retention.
168+
4. Generates a 256-bit upload token and deploys it as a Worker secret.
169+
5. waits for `/healthz`, then writes the user-level `url` and `token` profile.
170+
171+
The default prefix is `<worker-name>/objects/`; the default retention is 180 days. Use `--prefix` and `--retention-days` to change them. Prefixes let multiple deployments safely share one bucket, provided every deployment uses a distinct prefix. `wrangler.user.jsonc`, `.env*`, `.dev.vars`, and `.github-attachments/` are ignored by Git.
172+
173+
The Cloudflare API token is needed only for provisioning and later redeployment. Remove or revoke it after setup if the machine should retain upload access but not deployment access. The user-level `token` is deliberately separate: it can upload through this service but cannot administer Cloudflare.
132174

133175
## Operations
134176

‎openapi.yaml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ info:
77
name: All rights reserved
88
identifier: LicenseRef-Proprietary
99
servers:
10-
- url: https://github-pr-attachments.none23.workers.dev
10+
- url: https://{worker}.{subdomain}.workers.dev
11+
variables:
12+
worker:
13+
default: github-pr-attachments
14+
subdomain:
15+
default: example
1116
paths:
1217
/healthz:
1318
get:

0 commit comments

Comments
 (0)