Repository navigation
169 lines (163 loc) · 7.55 KB
/
Copy pathhosted-preview.yml
File metadata and controls
169 lines (163 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
name: Hosted PR preview
on:
pull_request:
types: [opened, synchronize, reopened, labeled]
permissions:
contents: read
# Only a PR labeled `hosted-preview` deploys, so an unlabeled PR never waits on
# the environment's approval. Nothing runs at close: a job holding preview
# credentials waits on that same approval, so each deploy first sweeps away
# the previews of PRs since closed or unlabeled.
jobs:
changes:
if: >-
contains(fromJSON('["opened", "synchronize", "reopened"]'), github.event.action) ||
(github.event.action == 'labeled' && github.event.label.name == 'hosted-preview')
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: read
pull-requests: read
outputs:
hosted: ${{ steps.paths.outputs.hosted }}
steps:
# Build the PR merge revision, including Hosted files added to its base.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.sha }}
persist-credentials: false
- name: Detect changes across the full PR
id: paths
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
CHANGED_FILES: ${{ github.event.pull_request.changed_files }}
run: |
# GitHub caps this API at 3,000 files. Verify conservatively above the cap.
if [ "$CHANGED_FILES" -gt 3000 ]; then
echo "hosted=true" >> "$GITHUB_OUTPUT"
exit 0
fi
# Every page, and rename sources too: moving a file out of Hosted changes Hosted.
gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/files" --jq '.[] | .filename, (.previous_filename // empty)' > "$RUNNER_TEMP/hosted-files"
node hosted/scripts/changed.mjs "$RUNNER_TEMP/hosted-files" >> "$GITHUB_OUTPUT"
verify:
needs: changes
if: needs.changes.outputs.hosted == 'true'
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
# Build the PR merge revision, including Hosted files added to its base.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.sha }}
persist-credentials: false
- uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version-file: package.json
cache: pnpm
# --no-runtime: setup-node already put the pinned Node.js on PATH.
- run: pnpm install --frozen-lockfile --no-runtime
- run: pnpm test:hosted
- run: pnpm build:hosted
# Every Worker's static files, each under its own `dist/<worker>/`.
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: preview-assets
path: hosted/dist
retention-days: 3
- if: vars.HOSTED_PREVIEWS_ENABLED != 'true'
run: echo '::notice::Cloud previews are not configured yet. Follow hosted/README.md -> Provision PR previews, set HOSTED_PREVIEWS_ENABLED=true, then rerun this workflow.'
deploy:
needs: verify
# Never expose deployment credentials to fork code or pull_request_target.
if: >-
contains(github.event.pull_request.labels.*.name, 'hosted-preview') &&
github.event.pull_request.head.repo.full_name == github.repository &&
vars.HOSTED_PREVIEWS_ENABLED == 'true'
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
pull-requests: read
# Let provisioning finish before a redeploy; canceling halfway leaks
# resources. Job-level, so a deploy awaiting approval never holds back the
# next push's verify.
concurrency:
group: hosted-pr-preview-${{ github.event.pull_request.number }}
cancel-in-progress: false
environment:
name: hosted-preview
url: ${{ steps.deploy.outputs.url }}
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
BUILD_SHA: ${{ github.sha }}
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_WORKERS_SUBDOMAIN: ${{ vars.CLOUDFLARE_WORKERS_SUBDOMAIN }}
steps:
# Build the PR merge revision, including Hosted files added to its base.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.sha }}
persist-credentials: false
- uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version-file: package.json
# A job holding deploy credentials restores no cache another job, an
# agent-driven one included, could have written.
package-manager-cache: false
# --no-runtime: setup-node already put the pinned Node.js on PATH.
- run: pnpm install --frozen-lockfile --no-runtime
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: preview-assets
path: hosted/dist
- name: Check preview settings before creating resources
run: |
node --input-type=module <<'JS'
import { required } from './hosted/scripts/preview.mjs';
for (const name of ['CLOUDFLARE_ACCOUNT_ID', 'CLOUDFLARE_WORKERS_SUBDOMAIN',
'NEON_PROJECT_ID', 'NEON_PREVIEW_PARENT_BRANCH',
'CLOUDFLARE_API_TOKEN', 'NEON_API_KEY', 'PREVIEW_AUTH_SECRET'])
required(process.env, name);
JS
env:
NEON_PROJECT_ID: ${{ vars.NEON_PROJECT_ID }}
NEON_PREVIEW_PARENT_BRANCH: ${{ vars.NEON_PREVIEW_PARENT_BRANCH }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
NEON_API_KEY: ${{ secrets.NEON_API_KEY }}
PREVIEW_AUTH_SECRET: ${{ secrets.PREVIEW_AUTH_SECRET }}
- name: Remove the previews of closed or unlabeled PRs
run: pnpm --filter dormouse-hosted preview:sweep
env:
GH_TOKEN: ${{ github.token }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
NEON_PROJECT_ID: ${{ vars.NEON_PROJECT_ID }}
NEON_API_KEY: ${{ secrets.NEON_API_KEY }}
- name: Create or reuse PR database branch
id: database
uses: neondatabase/create-branch-action@fb620d43d4c565abaf088b848a4e28e5c4ea4d9c # 6.3.1
with:
project_id: ${{ vars.NEON_PROJECT_ID }}
parent_branch: ${{ vars.NEON_PREVIEW_PARENT_BRANCH }}
branch_name: dormouse-hosted-pr-${{ github.event.pull_request.number }}
api_key: ${{ secrets.NEON_API_KEY }}
# Use the plan default (5 minutes); Free rejects custom timeouts.
suspend_timeout: 0
- name: Apply and validate SQL migrations on this PR's database
run: pnpm --filter dormouse-hosted db:migrate:preview && pnpm --filter dormouse-hosted db:validate:preview
env:
DATABASE_URL: ${{ steps.database.outputs.db_url }}
- name: Deploy the try, relay, voice, and account Workers and Hyperdrive
id: deploy
run: pnpm --filter dormouse-hosted preview:deploy
env:
DATABASE_URL: ${{ steps.database.outputs.db_url }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
PREVIEW_AUTH_SECRET: ${{ secrets.PREVIEW_AUTH_SECRET }}
- name: Check deployed revisions, database, cookies, routes and the rendezvous
run: pnpm --filter dormouse-hosted preview:smoke "$PREVIEW_ORIGINS" "$BUILD_SHA"
env:
PREVIEW_ORIGINS: ${{ steps.deploy.outputs.origins }}