-
Notifications
You must be signed in to change notification settings - Fork 0
174 lines (148 loc) · 5.52 KB
/
preview-deploy.yaml
File metadata and controls
174 lines (148 loc) · 5.52 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
name: Preview Deploy
on:
pull_request:
types: [opened, synchronize, reopened]
branches:
- main
paths:
- "src/**"
- "public/**"
- "package.json"
- "pnpm-lock.yaml"
- "next.config.mjs"
- "contentlayer.config.ts"
- "tailwind.config.ts"
- "wrangler.jsonc"
- "tsconfig.json"
- "drizzle/**"
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
WORKER_NAME: blog
WORKERS_SUBDOMAIN: ${{ secrets.WORKERS_SUBDOMAIN }}
jobs:
preview-deploy:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v6
- name: Post deploying comment
uses: actions/github-script@v8
with:
script: |
const prNum = context.issue.number;
const alias = `pr-${prNum}`;
const previewUrl = `https://${alias}-${{ env.WORKER_NAME }}.${{ env.WORKERS_SUBDOMAIN }}.workers.dev`;
const body = `## Preview Deploy\n\n**Status**: :hourglass_flowing_sand: デプロイ中...\n\n予定URL: ${previewUrl}`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('Preview Deploy')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
body: body,
});
}
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 10
- name: Use Node.js
uses: actions/setup-node@v6
with:
node-version: 24.12.0
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: Build content
run: pnpm build:content
- name: Deploy preview
id: deploy
run: |
PR_NUM=${{ github.event.pull_request.number }}
ALIAS="pr-${PR_NUM}"
# OpenNext ビルド
pnpm exec opennextjs-cloudflare build
# プレビューバージョンアップロード
pnpm exec opennextjs-cloudflare upload --preview-alias ${ALIAS}
# プレビューURL生成
PREVIEW_URL="https://${ALIAS}-${{ env.WORKER_NAME }}.${{ env.WORKERS_SUBDOMAIN }}.workers.dev"
echo "preview_url=${PREVIEW_URL}" >> $GITHUB_OUTPUT
- name: Update comment on success
if: success()
uses: actions/github-script@v8
with:
script: |
const prNum = context.issue.number;
const previewUrl = '${{ steps.deploy.outputs.preview_url }}';
const body = `## Preview Deploy\n\n**Status**: :white_check_mark: デプロイ完了\n\n**Preview URL**: ${previewUrl}\n\n---\n*このプレビューはPRがクローズされるまで利用可能です。*`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('Preview Deploy')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
body: body,
});
}
- name: Update comment on failure
if: failure()
uses: actions/github-script@v8
with:
script: |
const prNum = context.issue.number;
const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${{ github.run_id }}`;
const body = `## Preview Deploy\n\n**Status**: :x: デプロイ失敗\n\n詳細は[ワークフローログ](${runUrl})を確認してください。`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('Preview Deploy')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
body: body,
});
}