-
Notifications
You must be signed in to change notification settings - Fork 1
132 lines (116 loc) · 4.83 KB
/
Copy pathdeploy.yml
File metadata and controls
132 lines (116 loc) · 4.83 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
name: Deploy
on:
push:
branches: master
paths-ignore:
- 'README.md'
- 'LICENSE'
- '.github/dependabot.yml'
pull_request_target:
branches: master
repository_dispatch:
types: [deploy]
workflow_dispatch:
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- name: Clone repository
uses: actions/checkout@v6
with:
submodules: true
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.sha }}
- name: Install tooling
uses: jdx/mise-action@v4
with:
cache: true
- name: Get pnpm store directory
id: pnpm-cache
run: echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_ENV
- name: Cache pnpm dependencies and build directory
uses: actions/cache@v5
with:
path: |
${{ env.STORE_PATH }}
dist
.astro
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml', 'package.json') }}
restore-keys: ${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build
env:
GITHUB_TOKEN: ${{ secrets.GH_SECRET }}
run: mise run build
- name: Get deployment info
uses: actions/github-script@v9
id: deployment-info
with:
script: |
const sha = context.sha.slice(0, 7);
const headCommit = context.payload.head_commit?.message?.split('\n')[0];
const prTitle = context.payload.pull_request?.title;
const message = headCommit || prTitle || '';
const commit = message ? `${message} (${sha})` : sha;
const alias = `pr-${context.payload.pull_request?.number}`;
core.setOutput('commit-message', `--message '${commit}'`);
core.setOutput('wrangler-command', context.eventName === 'pull_request_target' ? 'versions upload' : 'deploy');
core.setOutput('preview-alias', context.eventName === 'pull_request_target' ? `--preview-alias ${alias}` : '');
- name: Upload build artifact
id: deploy
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
OUTPUT=$(pnpm wrangler \
${{ steps.deployment-info.outputs.wrangler-command }} \
${{ steps.deployment-info.outputs.commit-message }} \
${{ steps.deployment-info.outputs.preview-alias }} | tee /dev/stderr)
echo "version-id=$(echo "$OUTPUT" | grep -oP 'Worker Version ID: \K\S+')" >> $GITHUB_OUTPUT
echo "preview-url=$(echo "$OUTPUT" | grep -oP 'Version Preview URL: \K\S+')" >> $GITHUB_OUTPUT
echo "alias-url=$(echo "$OUTPUT" | grep -oP 'Version Preview Alias URL: \K\S+')" >> $GITHUB_OUTPUT
- name: Comment deployment info
if: github.event_name == 'pull_request_target'
uses: actions/github-script@v9
with:
script: |
const pr = context.payload.pull_request.number;
const sha = context.sha.slice(0, 7);
const aliasUrl = '${{ steps.deploy.outputs.alias-url }}';
const previewUrl = '${{ steps.deploy.outputs.preview-url }}';
const versionId = '${{ steps.deploy.outputs.version-id }}';
const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${context.sha}`;
const newRow = `| [\`${sha}\`](${commitUrl}) | [\`${versionId}\`](${previewUrl}) |`;
const { data: comments } = await github.rest.issues.listComments({
issue_number: pr,
owner: context.repo.owner,
repo: context.repo.repo,
});
const COMMENT_HEADING = "🚀 Preview Deployment Ready";
const existingComment = comments.find(c => c.user.login === 'github-actions[bot]' && c.body.includes(COMMENT_HEADING));
if (existingComment) {
const updatedBody = existingComment.body + '\n' + newRow;
await github.rest.issues.updateComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: updatedBody,
});
} else {
await github.rest.issues.createComment({
issue_number: pr,
owner: context.repo.owner,
repo: context.repo.repo,
body: [
`## [${COMMENT_HEADING}](${aliasUrl})`,
'',
'| Commit | Preview Deployment |',
'|---|---|',
newRow,
].join('\n')
});
}