-
Notifications
You must be signed in to change notification settings - Fork 45
192 lines (169 loc) · 8.29 KB
/
vercel-preview.yml
File metadata and controls
192 lines (169 loc) · 8.29 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
---
name: "Vercel Preview"
"on":
pull_request:
types:
- "opened"
- "synchronize"
- "reopened"
jobs:
deploy-preview:
name: "Deploy Preview"
runs-on: "depot-ubuntu-latest"
timeout-minutes: 15
concurrency:
group: "vercel-preview-ga-${{ github.event.pull_request.number }}"
cancel-in-progress: true
permissions:
contents: "read"
deployments: "write"
pull-requests: "write"
steps:
- name: "Checkout code"
uses: "actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd" # v6
- name: "Check collaborator write access"
id: "permission"
uses: "actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b" # v7
with:
result-encoding: "string"
script: |
const author = context.payload.pull_request.user.login;
try {
const { data } = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: author,
});
const hasWrite = ['write', 'maintain', 'admin'].includes(data.permission);
core.info(`${author} has permission '${data.permission}' — hasWrite: ${hasWrite}`);
return String(hasWrite);
} catch (e) {
if (e.status === 404) {
core.info(`${author} is not a collaborator — hasWrite: false`);
return 'false';
}
throw e;
}
- name: "Find existing comment"
uses: "peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e" # v3
id: "find-comment"
with:
issue-number: "${{ github.event.pull_request.number }}"
comment-author: "github-actions[bot]"
body-includes: "<!-- vercel-preview-deployment -->"
- name: "Post no-permission comment"
if: "steps.permission.outputs.result == 'false'"
uses: "peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043" # v4
with:
comment-id: "${{ steps.find-comment.outputs.comment-id }}"
issue-number: "${{ github.event.pull_request.number }}"
edit-mode: "replace"
body: |
<!-- vercel-preview-deployment -->
Preview deployment skipped — **@${{ github.event.pull_request.user.login }}** does not have write access to this repository.
A maintainer can trigger a preview deployment by [re-running this workflow](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}).
- name: "Setup Node.js"
if: "steps.permission.outputs.result == 'true'"
uses: "actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238" # v6
with:
node-version: 22
- name: "Generate timestamp"
if: "steps.permission.outputs.result == 'true'"
id: "timestamp-building"
run: |
echo "time=$(date -u '+%b %d, %Y %I:%M%P')" >> $GITHUB_OUTPUT
- name: "Post initial building comment"
if: "steps.permission.outputs.result == 'true'"
uses: "peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043" # v4
id: "building-comment"
with:
comment-id: "${{ steps.find-comment.outputs.comment-id }}"
issue-number: "${{ github.event.pull_request.number }}"
edit-mode: "replace"
body: |
<!-- vercel-preview-deployment -->
Preview deployment status for this pull request.
| Name | Status | Preview | Updated (UTC) |
| :--- | :----- | :------ | :------------ |
| **docs** | 🟡 [Building](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) | [View Workflow](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) | ${{ steps.timestamp-building.outputs.time }} |
- name: "Deploy to Vercel"
if: "steps.permission.outputs.result == 'true'"
id: "deploy"
env:
VERCEL_TOKEN: "${{ secrets.VERCEL_TOKEN }}"
VERCEL_ORG_ID: "${{ secrets.VERCEL_ORG_ID }}"
VERCEL_PROJECT_ID: "${{ secrets.VERCEL_PROJECT_ID }}"
run: |
npm install --global vercel@latest
DEPLOY_URL=$(vercel deploy --token "$VERCEL_TOKEN" --yes | tail -1)
echo "preview-url=$DEPLOY_URL" >> $GITHUB_OUTPUT
# Sanitize branch name for DNS: lowercase, non-alphanumeric to hyphens, collapse runs
# Truncate to 46 chars so the full alias (docs-git-<branch>-authzed) stays within the 63-char DNS label limit
BRANCH=$(echo "${{ github.head_ref }}" \
| tr '[:upper:]' '[:lower:]' \
| sed 's/[^a-z0-9]/-/g' \
| sed 's/-\+/-/g' \
| sed 's/^-//; s/-$//' \
| cut -c1-46 \
| sed 's/-$//')
vercel alias set "$DEPLOY_URL" "docs-git-${BRANCH}-authzed.vercel.app" \
--token "$VERCEL_TOKEN"
- name: "Create GitHub Deployment"
if: "steps.permission.outputs.result == 'true'"
uses: "chrnorm/deployment-action@55729fcebec3d284f60f5bcabbd8376437d696b1" # v2
id: "deployment"
with:
token: "${{ secrets.GITHUB_TOKEN }}"
environment: "Preview (GitHub Actions)"
environment-url: "${{ steps.deploy.outputs.preview-url }}"
transient-environment: true
auto-inactive: true
- name: "Update deployment status to success"
if: "steps.permission.outputs.result == 'true' && success()"
uses: "chrnorm/deployment-status@9a72af4586197112e0491ea843682b5dc280d806" # v2
with:
token: "${{ secrets.GITHUB_TOKEN }}"
deployment-id: "${{ steps.deployment.outputs.deployment_id }}"
state: "success"
environment-url: "${{ steps.deploy.outputs.preview-url }}"
- name: "Update deployment status to failure"
if: "steps.permission.outputs.result == 'true' && failure()"
uses: "chrnorm/deployment-status@9a72af4586197112e0491ea843682b5dc280d806" # v2
with:
token: "${{ secrets.GITHUB_TOKEN }}"
deployment-id: "${{ steps.deployment.outputs.deployment_id }}"
state: "failure"
- name: "Generate timestamp for success"
if: "steps.permission.outputs.result == 'true' && success()"
id: "timestamp-success"
run: |
echo "time=$(date -u '+%b %d, %Y %I:%M%P')" >> $GITHUB_OUTPUT
- name: "Update comment with success"
if: "steps.permission.outputs.result == 'true' && success()"
uses: "peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043" # v4
with:
comment-id: "${{ steps.building-comment.outputs.comment-id }}"
edit-mode: "replace"
body: |
<!-- vercel-preview-deployment -->
Preview deployment status for this pull request.
| Name | Status | Preview | Updated (UTC) |
| :--- | :----- | :------ | :------------ |
| **docs** | 🟢 [Ready](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) | [Visit Preview](${{ steps.deploy.outputs.preview-url }}) | ${{ steps.timestamp-success.outputs.time }} |
- name: "Generate timestamp for failure"
if: "steps.permission.outputs.result == 'true' && failure()"
id: "timestamp-failure"
run: |
echo "time=$(date -u '+%b %d, %Y %I:%M%P')" >> $GITHUB_OUTPUT
- name: "Update comment with failure"
if: "steps.permission.outputs.result == 'true' && failure()"
uses: "peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043" # v4
with:
comment-id: "${{ steps.building-comment.outputs.comment-id }}"
edit-mode: "replace"
body: |
<!-- vercel-preview-deployment -->
Preview deployment status for this pull request.
| Name | Status | Preview | Updated (UTC) |
| :--- | :----- | :------ | :------------ |
| **docs** | 🔴 [Failed](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) | [View Logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) | ${{ steps.timestamp-failure.outputs.time }} |