-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathnotify-teable-on-mintlify-deploy.yml
More file actions
179 lines (168 loc) · 7.9 KB
/
Copy pathnotify-teable-on-mintlify-deploy.yml
File metadata and controls
179 lines (168 loc) · 7.9 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
name: Notify Teable on Mintlify Deploy
on:
pull_request:
types: [closed]
branches: [main]
permissions:
deployments: read
pull-requests: read
contents: read
jobs:
notify-teable:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Detect Mintlify relevance
id: relevance
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -euo pipefail
FILES="[]"
PAGE=1
while true; do
PAGE_FILES=$(curl -fsS -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$REPO/pulls/$PR_NUMBER/files?per_page=100&page=$PAGE")
COUNT=$(echo "$PAGE_FILES" | python3 -c "import json,sys;print(len(json.load(sys.stdin)))")
FILES=$(jq -s 'add' <(echo "$FILES") <(echo "$PAGE_FILES"))
if [ "$COUNT" -lt 100 ]; then
break
fi
PAGE=$((PAGE + 1))
done
NEEDS_DEPLOY=$(echo "$FILES" | python3 -c "
import json,sys
paths = [f.get('filename','') for f in json.load(sys.stdin)]
prefixes = ('en/', 'zh/', 'images/', 'snippets/', 'api-reference/')
exact = {'docs.json', 'mint.json', 'swagger.json', 'style.css', 'changelog-toc.js'}
needs = any(p in exact or p.startswith(prefixes) for p in paths)
print('true' if needs else 'false')
")
echo "needs_mintlify_deploy=$NEEDS_DEPLOY" >> "$GITHUB_OUTPUT"
echo "needs_mintlify_deploy=$NEEDS_DEPLOY"
- name: Record GitHub merge
env:
TEABLE_WEBHOOK_URL: ${{ secrets.TEABLE_WEBHOOK_URL }}
TEABLE_WEBHOOK_TOKEN: ${{ secrets.TEABLE_WEBHOOK_TOKEN }}
DOCS_BRANCH: ${{ github.event.pull_request.head.ref }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_URL: ${{ github.event.pull_request.html_url }}
MERGE_SHA: ${{ github.event.pull_request.merge_commit_sha }}
MERGED_AT: ${{ github.event.pull_request.merged_at }}
run: |
set -euo pipefail
curl -fsS -X POST \
"$TEABLE_WEBHOOK_URL" \
-H "Authorization: Bearer $TEABLE_WEBHOOK_TOKEN" \
-H "Content-Type: application/json" \
-d "$(jq -nc \
--arg b "$DOCS_BRANCH" \
--arg s "pending" \
--arg p "$PR_NUMBER" \
--arg r "$PR_URL" \
--arg m "$MERGE_SHA" \
--arg t "$MERGED_AT" \
'{docs_branch:$b, mintlify_status:$s, pr_number:$p, pr_url:$r, merge_sha:$m, merged_at:$t}')"
- name: Resolve Mintlify deployment status
id: mintlify
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
SHA: ${{ github.event.pull_request.merge_commit_sha }}
NEEDS_MINTLIFY_DEPLOY: ${{ steps.relevance.outputs.needs_mintlify_deploy }}
run: |
set -euo pipefail
if [ "$NEEDS_MINTLIFY_DEPLOY" != "true" ]; then
echo "Mintlify deployment not required for this PR"
echo "docs_url=" >> "$GITHUB_OUTPUT"
echo "mintlify_status=not_required" >> "$GITHUB_OUTPUT"
echo "mintlify_deployment_id=" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "Waiting for Mintlify staging deployment of $SHA"
DEADLINE=$(( $(date +%s) + 720 ))
LAST_DEPLOYMENT_ID=""
LAST_DEPLOYMENT_STATE=""
while [ "$(date +%s)" -lt "$DEADLINE" ]; do
DEPS=$(curl -fsS -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$REPO/deployments?sha=$SHA&environment=staging&per_page=20")
DID=$(echo "$DEPS" | python3 -c "
import json,sys
for d in json.load(sys.stdin):
if d.get('creator',{}).get('login') == 'mintlify[bot]':
print(d['id']); break
")
if [ -n "$DID" ]; then
LAST_DEPLOYMENT_ID="$DID"
STATE=$(curl -fsS -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$REPO/deployments/$DID/statuses?per_page=1" \
| python3 -c "import json,sys;ss=json.load(sys.stdin);print(ss[0]['state'] if ss else '')")
LAST_DEPLOYMENT_STATE="$STATE"
echo "deployment=$DID state=$STATE"
case "$STATE" in
success)
ENV_URL=$(curl -fsS -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$REPO/deployments/$DID/statuses?per_page=1" \
| python3 -c "import json,sys;ss=json.load(sys.stdin);print(ss[0].get('environment_url') or ss[0].get('target_url') or '')")
echo "docs_url=$ENV_URL" >> "$GITHUB_OUTPUT"
echo "mintlify_status=success" >> "$GITHUB_OUTPUT"
echo "mintlify_deployment_id=$DID" >> "$GITHUB_OUTPUT"
exit 0
;;
failure|error)
echo "Mintlify deployment $DID failed: $STATE" >&2
echo "mintlify_status=$STATE" >> "$GITHUB_OUTPUT"
echo "mintlify_deployment_id=$DID" >> "$GITHUB_OUTPUT"
exit 0
;;
esac
fi
sleep 15
done
if [ -n "$LAST_DEPLOYMENT_ID" ]; then
echo "Timed out waiting for Mintlify deployment $LAST_DEPLOYMENT_ID to succeed; last state: ${LAST_DEPLOYMENT_STATE:-unknown}" >&2
echo "mintlify_status=deployment_timeout" >> "$GITHUB_OUTPUT"
echo "mintlify_deployment_id=$LAST_DEPLOYMENT_ID" >> "$GITHUB_OUTPUT"
else
echo "Timed out waiting for Mintlify to create a deployment for $SHA" >&2
echo "mintlify_status=deployment_not_created" >> "$GITHUB_OUTPUT"
echo "mintlify_deployment_id=" >> "$GITHUB_OUTPUT"
fi
- name: Notify Teable webhook
if: always()
env:
TEABLE_WEBHOOK_URL: ${{ secrets.TEABLE_WEBHOOK_URL }}
TEABLE_WEBHOOK_TOKEN: ${{ secrets.TEABLE_WEBHOOK_TOKEN }}
DOCS_BRANCH: ${{ github.event.pull_request.head.ref }}
DOCS_URL: ${{ steps.mintlify.outputs.docs_url }}
MINTLIFY_STATUS: ${{ steps.mintlify.outputs.mintlify_status }}
MINTLIFY_DEPLOYMENT_ID: ${{ steps.mintlify.outputs.mintlify_deployment_id }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_URL: ${{ github.event.pull_request.html_url }}
MERGE_SHA: ${{ github.event.pull_request.merge_commit_sha }}
MERGED_AT: ${{ github.event.pull_request.merged_at }}
run: |
set -euo pipefail
STATUS="${MINTLIFY_STATUS:-error}"
curl -fsS -X POST \
"$TEABLE_WEBHOOK_URL" \
-H "Authorization: Bearer $TEABLE_WEBHOOK_TOKEN" \
-H "Content-Type: application/json" \
-d "$(jq -nc \
--arg b "$DOCS_BRANCH" \
--arg u "$DOCS_URL" \
--arg s "$STATUS" \
--arg d "$MINTLIFY_DEPLOYMENT_ID" \
--arg p "$PR_NUMBER" \
--arg r "$PR_URL" \
--arg m "$MERGE_SHA" \
--arg t "$MERGED_AT" \
'{docs_branch:$b, docs_url:$u, mintlify_status:$s, mintlify_deployment_id:$d, pr_number:$p, pr_url:$r, merge_sha:$m, merged_at:$t}')"
- name: Fail if Mintlify did not deploy
if: steps.relevance.outputs.needs_mintlify_deploy == 'true' && steps.mintlify.outputs.mintlify_status != 'success'
run: |
echo "Mintlify did not deploy successfully: ${{ steps.mintlify.outputs.mintlify_status }}" >&2
exit 1