forked from withastro/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
146 lines (136 loc) · 6.15 KB
/
Copy pathdeploy-preview.yml
File metadata and controls
146 lines (136 loc) · 6.15 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
name: Deploy Preview
on:
workflow_run:
workflows: ["Deploy"]
types: [completed]
permissions: {}
# Cancel older preview runs for the same branch so a stale deploy doesn't
# overwrite the comment from a newer one.
concurrency:
group: preview-${{ github.event.workflow_run.head_branch }}
cancel-in-progress: true
jobs:
deploy-preview:
if: >-
github.repository_owner == 'withastro' &&
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
pull-requests: write
steps:
- name: Download PR metadata
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: pr-metadata
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
path: pr-metadata
- name: Read PR metadata
id: metadata
run: |
echo "pr_number=$(cat pr-metadata/pr_number)" >> "$GITHUB_OUTPUT"
echo "preview_alias=$(cat pr-metadata/preview_alias)" >> "$GITHUB_OUTPUT"
echo "preview_url=$(cat pr-metadata/preview_url)" >> "$GITHUB_OUTPUT"
- name: Comment deployment in progress
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const prNumber = parseInt('${{ steps.metadata.outputs.pr_number }}', 10);
const previewUrl = '${{ steps.metadata.outputs.preview_url }}';
const commitSHA = '${{ github.event.workflow_run.head_sha }}';
const runUrl = '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}';
const body = `**Preview deployment**\n\n`
+ `🔄 Deployment in progress...\n\n`
+ `- **Latest commit:** ${commitSHA}\n`
+ `- **Preview:** ${previewUrl}\n`
+ `- **Workflow run:** [View logs](${runUrl})`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = comments.find(c => c.user.login === 'github-actions[bot]' && c.body.includes('**Preview deployment**'));
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: prNumber,
body,
});
}
- name: Download build output
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: build-output
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Deploy preview
uses: cloudflare/wrangler-action@ebbaa1584979971c8614a24965b4405ff95890e0 # v4.0.0
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
# Always upload to the main "docs" Worker so previews use the
# *.previews.docs.astro.build domain, even for PRs targeting version branches.
command: versions upload --name docs --preview-alias "${{ steps.metadata.outputs.preview_alias }}"
- name: Comment deployment complete
if: success()
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const prNumber = parseInt('${{ steps.metadata.outputs.pr_number }}', 10);
const previewUrl = '${{ steps.metadata.outputs.preview_url }}';
const commitSHA = '${{ github.event.workflow_run.head_sha }}';
const runUrl = '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}';
const body = `**Preview deployment**\n\n`
+ `✅ Deployment complete!\n\n`
+ `- **Latest commit:** ${commitSHA}\n`
+ `- **Preview:** ${previewUrl}\n`
+ `- **Workflow run:** [View logs](${runUrl})`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = comments.find(c => c.user.login === 'github-actions[bot]' && c.body.includes('**Preview deployment**'));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
}
- name: Comment deployment failed
if: failure()
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const prNumber = parseInt('${{ steps.metadata.outputs.pr_number }}', 10);
const commitSHA = '${{ github.event.workflow_run.head_sha }}';
const runUrl = '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}';
const body = `**Preview deployment**\n\n`
+ `❌ Deployment failed!\n\n`
+ `- **Latest commit:** ${commitSHA}\n`
+ `- **Workflow run:** [View logs](${runUrl})`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = comments.find(c => c.user.login === 'github-actions[bot]' && c.body.includes('**Preview deployment**'));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
}