-
Notifications
You must be signed in to change notification settings - Fork 14
121 lines (109 loc) · 4.13 KB
/
Copy pathdocs.yml
File metadata and controls
121 lines (109 loc) · 4.13 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
name: Build and Deploy Docs
on:
push:
branches: [dev]
paths:
- 'docs/**'
- 'README.md'
- 'package.json'
- 'package-lock.json'
- '.github/workflows/docs.yml'
workflow_dispatch:
jobs:
build-and-deploy-docs:
name: Build and Deploy Docs
runs-on: ubuntu-latest
env:
DOCS_SSH_HOST: ${{ secrets.DOCS_SSH_HOST }}
DOCS_SSH_USER: ${{ secrets.DOCS_SSH_USER }}
DOCS_SSH_KEY: ${{ secrets.DOCS_SSH_KEY }}
DOCS_SSH_PORT: ${{ secrets.DOCS_SSH_PORT }}
DOCS_DEPLOY_PATH: ${{ secrets.DOCS_DEPLOY_PATH }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ZONE_ID: ${{ secrets.CLOUDFLARE_ZONE_ID }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
cache: npm
- name: Install docs dependencies
run: npm ci
- name: Build docs
run: npm run docs:build
- name: Validate deployment secrets
run: |
set -euo pipefail
for name in DOCS_SSH_HOST DOCS_SSH_USER DOCS_SSH_KEY DOCS_SSH_PORT DOCS_DEPLOY_PATH; do
if [ -z "${!name}" ]; then
echo "::error::$name is required"
exit 1
fi
done
- name: Configure SSH
run: |
set -euo pipefail
install -m 700 -d ~/.ssh
printf '%s\n' "$DOCS_SSH_KEY" | perl -pe 's/\\n/\n/g; s/\r//g' > ~/.ssh/id_docs
chmod 600 ~/.ssh/id_docs
if ! ssh-keygen -y -f ~/.ssh/id_docs > /dev/null; then
echo "::error::DOCS_SSH_KEY is not a readable private key. Paste the full private key, not the .pub public key; preserve BEGIN/END lines; use an unencrypted deploy key."
exit 1
fi
ssh-keyscan -p "$DOCS_SSH_PORT" -H "$DOCS_SSH_HOST" >> ~/.ssh/known_hosts
- name: Create remote docs directory
run: |
set -euo pipefail
remote_path="${DOCS_DEPLOY_PATH%/}"
ssh -i ~/.ssh/id_docs -o IdentitiesOnly=yes -p "$DOCS_SSH_PORT" \
"$DOCS_SSH_USER@$DOCS_SSH_HOST" \
"mkdir -p -- '$remote_path'"
- name: Deploy docs with rsync
run: |
set -euo pipefail
remote_path="${DOCS_DEPLOY_PATH%/}"
rsync -az --delete \
--exclude='.user.ini' \
-e "ssh -i ~/.ssh/id_docs -o IdentitiesOnly=yes -p $DOCS_SSH_PORT" \
docs/.vitepress/dist/ \
"$DOCS_SSH_USER@$DOCS_SSH_HOST:$remote_path/"
- name: Validate Cloudflare purge secrets
run: |
set -euo pipefail
for name in CLOUDFLARE_API_TOKEN CLOUDFLARE_ZONE_ID; do
if [ -z "${!name}" ]; then
echo "::error::$name is required"
exit 1
fi
done
- name: Purge www.buaa.team cache
run: |
set -euo pipefail
response_file="$(mktemp)"
status_code="$(curl -sS -o "$response_file" -w "%{http_code}" \
-X POST "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/purge_cache" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json" \
--data '{"hosts":["www.buaa.team"]}')"
python - "$response_file" "$status_code" <<'PY'
import json
import pathlib
import sys
path = pathlib.Path(sys.argv[1])
status_code = int(sys.argv[2])
text = path.read_text(encoding="utf-8")
try:
payload = json.loads(text)
except json.JSONDecodeError as exc:
print(f"::error::Failed to parse Cloudflare response as JSON: {exc}")
print(text)
raise SystemExit(1)
if status_code >= 400 or not payload.get("success"):
print(f"::error::Cloudflare purge failed with HTTP {status_code}")
print(text)
raise SystemExit(1)
request_id = payload.get("result", {}).get("id", "unknown")
print(f"Cloudflare cache purge succeeded for www.buaa.team (request id: {request_id})")
PY