-
Notifications
You must be signed in to change notification settings - Fork 13
233 lines (200 loc) · 7.34 KB
/
pr-preview-build.yaml
File metadata and controls
233 lines (200 loc) · 7.34 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
name: Site build preview
on:
pull_request_target:
types: [opened, synchronize]
branches:
- main
permissions:
contents: read
pull-requests: write
env:
SYNC_REPO: ${{ secrets.SYNC_REPO }}
SYNC_DIRS: "api-design docs guides mcp openapi"
BRANCH_NAME: docs-preview/pr-${{ github.event.pull_request.number }}
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
VERCEL_TEAM_ID: ${{ secrets.VERCEL_TEAM_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
VERCEL_PROJECT_NAME: ${{ secrets.VERCEL_PROJECT_NAME }}
VERCEL_GIT_REPO_ID: ${{ secrets.VERCEL_GIT_REPO_ID }}
concurrency:
group: preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
preview-sync:
runs-on: ubuntu-latest
outputs:
changed: ${{ steps.diff.outputs.changed }}
steps:
- name: Checkout developer-docs PR head
uses: actions/checkout@v4
with:
fetch-depth: 0
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
- name: Clone marketing-site preview branch
env:
TOKEN: ${{ secrets.SERVICE_BOT_TOKEN }}
run: |
set -euo pipefail
git clone "https://x-access-token:${TOKEN}@github.com/${SYNC_REPO}.git" private-repo
cd private-repo
git checkout -B "$BRANCH_NAME"
- name: Prepare destination dirs
run: |
set -euo pipefail
for dir in $SYNC_DIRS; do
mkdir -p "private-repo/src/content/$dir"
done
- name: Sync content folders to preview branch
run: |
set -euo pipefail
for dir in $SYNC_DIRS; do
src_dir="$dir"
dest_dir="private-repo/src/content/$dir"
if [ -d "$src_dir" ]; then
echo "Syncing $src_dir → $dest_dir"
rsync -a --delete "$src_dir/" "$dest_dir/"
else
echo "Warning: $src_dir does not exist, skipping..."
fi
done
- name: Sync public assets to preview branch
run: |
set -euo pipefail
base_src="public/assets"
base_dest="private-repo/public/assets"
for dir in $SYNC_DIRS; do
src_assets="${base_src}/${dir}"
dest_assets="${base_dest}/${dir}"
if [ -d "$src_assets" ]; then
echo "Syncing assets $src_assets → $dest_assets"
mkdir -p "$dest_assets"
rsync -a --delete "$src_assets/" "$dest_assets/"
else
echo "Warning: $src_assets does not exist, skipping..."
fi
done
- name: Sync _meta.global.tsx into preview branch
run: |
set -euo pipefail
src_file="_meta.global.tsx"
dest_file="private-repo/src/app/_meta.global.tsx"
if [ -f "$src_file" ]; then
echo "Syncing file $src_file → $dest_file"
mkdir -p "$(dirname "$dest_file")"
cp "$src_file" "$dest_file"
else
echo "Warning: $src_file does not exist, skipping..."
fi
- name: Commit and push preview branch
id: diff
run: |
set -euo pipefail
cd private-repo
git config user.name "Beezy the bot"
git config user.email "marketing@speakeasy.com"
git add .
if git diff --cached --quiet; then
echo "No changes to sync for preview"
echo "changed=false" >> "$GITHUB_OUTPUT"
exit 0
fi
git commit -m "🔍 Preview sync from developer-docs PR #${{ github.event.pull_request.number }}"
git push origin "$BRANCH_NAME" --force
echo "changed=true" >> "$GITHUB_OUTPUT"
create-vercel-deployment:
needs: preview-sync
if: needs.preview-sync.outputs.changed == 'true'
runs-on: ubuntu-latest
outputs:
deployment_url: ${{ steps.trigger.outputs.deployment_url }}
steps:
- name: Install jq
run: |
sudo apt-get update
sudo apt-get install -y jq
- name: Trigger Vercel deployment
id: trigger
env:
SYNC_REPO: ${{ env.SYNC_REPO }}
BRANCH_NAME: ${{ env.BRANCH_NAME }}
VERCEL_TOKEN: ${{ env.VERCEL_TOKEN }}
VERCEL_TEAM_ID: ${{ env.VERCEL_TEAM_ID }}
VERCEL_PROJECT_ID: ${{ env.VERCEL_PROJECT_ID }}
VERCEL_PROJECT_NAME: ${{ env.VERCEL_PROJECT_NAME }}
VERCEL_GIT_REPO_ID: ${{ env.VERCEL_GIT_REPO_ID }}
run: |
set -euo pipefail
required=(VERCEL_TOKEN VERCEL_PROJECT_ID VERCEL_PROJECT_NAME)
for var in "${required[@]}"; do
if [ -z "${!var:-}" ]; then
echo "Missing required secret: $var" >&2
exit 1
fi
done
if [[ -z "${SYNC_REPO:-}" || "$SYNC_REPO" != */* ]]; then
echo "Invalid SYNC_REPO value: ${SYNC_REPO:-<empty>}" >&2
exit 1
fi
org="${SYNC_REPO%%/*}"
repo="${SYNC_REPO#*/}"
payload=$(jq -n \
--arg name "$VERCEL_PROJECT_NAME" \
--arg project "$VERCEL_PROJECT_ID" \
--arg org "$org" \
--arg repo "$repo" \
--arg ref "$BRANCH_NAME" \
'{
name: $name,
project: $project,
target: "staging",
gitSource: {
type: "github",
org: $org,
repo: $repo,
ref: $ref
}
}')
if [ -n "${VERCEL_GIT_REPO_ID:-}" ]; then
payload=$(printf '%s' "$payload" | jq --arg repoId "$VERCEL_GIT_REPO_ID" '.gitSource.repoId = $repoId')
fi
api_url="https://api.vercel.com/v13/deployments"
if [ -n "${VERCEL_TEAM_ID:-}" ]; then
api_url="${api_url}?teamId=${VERCEL_TEAM_ID}"
fi
response=$(curl -sS -X POST "$api_url" \
-H "Authorization: Bearer ${VERCEL_TOKEN}" \
-H "Content-Type: application/json" \
-d "$payload")
if echo "$response" | jq -e '.error' >/dev/null; then
echo "Vercel API error:" >&2
echo "$response" | jq -r '.error.message // .' >&2
exit 1
fi
url=$(echo "$response" | jq -r '.url // empty')
if [ -z "$url" ]; then
echo "Vercel API response did not include a deployment URL." >&2
echo "$response" >&2
exit 1
fi
if [[ "$url" == http*://* ]]; then
full_url="$url"
else
full_url="https://$url"
fi
echo "deployment_url=$full_url" >> "$GITHUB_OUTPUT"
comment-vercel-preview:
needs: [preview-sync, create-vercel-deployment]
if: needs.preview-sync.outputs.changed == 'true'
runs-on: ubuntu-latest
steps:
- name: Comment with Vercel preview URL
uses: marocchino/sticky-pull-request-comment@v2
with:
number: ${{ github.event.pull_request.number }}
header: VercelPreview
message: |
🔗 **Preview your changes**
[${{ needs.create-vercel-deployment.outputs.deployment_url }}](${{ needs.create-vercel-deployment.outputs.deployment_url }})
_(The preview may still be building. Check back at this link in a few minutes.)_