Repository navigation
147 lines (128 loc) · 4.48 KB
/
Copy pathpreview.yml
File metadata and controls
147 lines (128 loc) · 4.48 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
name: Preview
on:
pull_request:
branches:
- main
types: [opened, synchronize, reopened, closed]
concurrency:
group: pr-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
PAGE_DIR: pr-preview-${{ github.event.pull_request.number }}
jobs:
build:
name: Build
runs-on: ubuntu-latest
if: github.event.action != 'closed' && github.event.pull_request.head.repo.full_name == github.repository
steps:
- name: Checkout repository
uses: actions/checkout@v7
- name: Set up Node.js
uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
cache-dependency-path: package-lock.json
- name: Install Dependencies
run: npm ci
- name: Build
run: npm run build
env:
BASE_PATH: /${{ env.PAGE_DIR }}/
GH_TOKEN: ${{ secrets.GH_TOKEN }}
- name: Upload artifact
uses: actions/upload-artifact@v7
with:
name: site-preview
path: dist/
retention-days: 1
preview:
name: Deploy preview
needs: [build]
runs-on: ubuntu-latest
if: github.event.action != 'closed' && github.event.pull_request.head.repo.full_name == github.repository
concurrency:
group: gh-pages
cancel-in-progress: false
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v7
- name: Checkout or create gh-pages
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git fetch origin gh-pages 2>/dev/null
if git rev-parse --verify origin/gh-pages >/dev/null 2>&1; then
git checkout gh-pages
else
git checkout --orphan gh-pages
git rm -rf . > /dev/null
fi
- name: Download artifact
uses: actions/download-artifact@v8
with:
name: site-preview
path: ${{ env.PAGE_DIR }}
- name: Commit and push
run: |
touch .nojekyll
git add .
git diff --staged --quiet || git commit -m "Deploy PR preview for #${{ github.event.pull_request.number }}"
git push origin gh-pages
- name: Comment PR
uses: actions/github-script@v9
with:
script: |
const url = `https://wktk.moe/${process.env.PAGE_DIR}/`;
const marker = "<!-- pr-preview-comment -->";
const body = `${marker}\n🔍 Preview deployed to: ${url}`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
const existing = comments.find((comment) => comment.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body
});
}
cleanup:
name: Cleanup preview
runs-on: ubuntu-latest
if: github.event.action == 'closed' && github.event.pull_request.head.repo.full_name == github.repository
concurrency:
group: gh-pages
cancel-in-progress: false
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v7
- name: Checkout gh-pages
id: checkout-gh-pages
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git fetch origin gh-pages 2>/dev/null || echo "skip=true" >> "$GITHUB_OUTPUT"
git rev-parse --verify origin/gh-pages >/dev/null 2>&1 && git checkout gh-pages || echo "skip=true" >> "$GITHUB_OUTPUT"
- name: Remove preview directory
if: steps.checkout-gh-pages.outputs.skip != 'true'
run: |
rm -rf "${{ env.PAGE_DIR }}"
git add .
git diff --staged --quiet || git commit -m "Remove PR preview for #${{ github.event.pull_request.number }}"
git push origin gh-pages