-
-
Notifications
You must be signed in to change notification settings - Fork 39
196 lines (171 loc) · 7.76 KB
/
Copy pathpublish.yml
File metadata and controls
196 lines (171 loc) · 7.76 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Publish
name: Publish
# Controls when the workflow will run
on:
# Triggers the workflow on push event, but only for main branch
push:
branches:
- main
# Triggers the workflow on pull request event, but only for pull request from not forked repo
pull_request:
types:
- opened
- synchronize
# Triggers the workflow on pull request event, but only for pull request opened or pull request labeled with "🚀request-deploy" (from forked repo)
# pull_request is not allowed to use secrets, so we use pull_request_target instead (in forked repos)
pull_request_target:
types:
# When a created pull request from forked repo, it will be comment 'Should deploy to add label'
- opened
# When a labeled '🚀request-deploy' pull request from forked repo, it will be deploy to Cloudflare Pages
- labeled
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
permissions:
# default contents: read & write (in forked repos, only read)
contents: write
# default deployments: read & write (in forked repos, only read)
deployments: write
# default pull-requests: read & write (in forked repos, only read)
pull-requests: write
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# Deploy
deploy:
name: Deploy
runs-on: ${{ matrix.os }}
# push event in main branch
# workflow_dispatch event
# pull_request event from not forked repo
# pull_request_target event with label "🚀request-deploy" from forked repo
if: ${{
github.event_name == 'push' ||
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false) ||
(github.event_name == 'pull_request_target' &&
github.event.action == 'labeled' &&
github.event.pull_request.head.repo.fork == true &&
contains(github.event.label.name, '🚀request-deploy'))
}}
strategy:
matrix:
os: [ubuntu-latest]
node: [current]
# Cancel previous runs that are not completed yet
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.head.ref || github.ref }}
cancel-in-progress: true
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@v7
with:
ref: ${{ github.event.pull_request.head.sha || github.ref }}
fetch-depth: 0
- name: Remove built-in Yarn
run: npm uninstall -g yarn
- name: Enable Corepack
run: corepack enable
- name: Setup node
uses: actions/setup-node@v6
with:
node-version: ${{ matrix.node }}
cache: 'yarn'
- name: Install dependencies
run: yarn install --check-cache
- name: Build website
run: yarn build
- name: Publish to Cloudflare Pages
id: cloudflare-pages-deploy
uses: cloudflare/wrangler-action@v4
with:
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
command: 'pages deploy build --project-name=docusauruscommunitywebsite'
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
- name: Resolve deployment URLs
id: deploy-urls
run: |
DEPLOYMENT_URL="${{ steps.cloudflare-pages-deploy.outputs['deployment-url'] }}"
ALIAS_URL="${{ steps.cloudflare-pages-deploy.outputs['pages-deployment-alias-url'] }}"
# Prefer alias URL for preview builds; fall back to deployment URL.
FINAL_URL="$ALIAS_URL"
if [ -z "$FINAL_URL" ]; then
FINAL_URL="$DEPLOYMENT_URL"
fi
echo "deployment_url=$DEPLOYMENT_URL" >> "$GITHUB_OUTPUT"
echo "alias_url=$ALIAS_URL" >> "$GITHUB_OUTPUT"
echo "final_url=$FINAL_URL" >> "$GITHUB_OUTPUT"
- name: Write deployment summary
shell: bash
env:
COMMIT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
ENVIRONMENT: ${{ steps.cloudflare-pages-deploy.outputs['pages-environment'] }}
DEPLOYMENT_URL: ${{ steps.deploy-urls.outputs.deployment_url }}
PREVIEW_URL: ${{ steps.deploy-urls.outputs.final_url }}
run: |
{
echo "## Deploying with Cloudflare Pages"
echo
echo "| Name | Result |"
echo "| --- | --- |"
echo "| Last commit | \`$COMMIT_SHA\` |"
echo "| Deploy log | [View run]($RUN_URL) |"
echo "| Preview URL | [$PREVIEW_URL]($PREVIEW_URL) |"
echo "| Deployment URL | [$DEPLOYMENT_URL]($DEPLOYMENT_URL) |"
echo "| Environment | $ENVIRONMENT |"
} >> "$GITHUB_STEP_SUMMARY"
- name: Create commit comment
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
uses: peter-evans/commit-comment@v4
with:
body: |
### ⚡ Successfully deployed to Cloudflare Pages!
- 🔨 **Latest commit:** `${{ github.event.pull_request.head.sha || github.sha }}`
- 🔍 **Latest deploy log:** [${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
- 😎 **Deploy preview URL:** [${{ steps.deploy-urls.outputs.final_url }}](${{ steps.deploy-urls.outputs.final_url }})
- 🌳 **Environment:** ${{ steps.cloudflare-pages-deploy.outputs['pages-environment'] }}
- name: Create PR comment
if: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }}
uses: mshick/add-pr-comment@v3
with:
message: |
### ⚡ Successfully deployed to Cloudflare Pages!
- 🔨 **Latest commit:** `${{ github.event.pull_request.head.sha || github.sha }}`
- 🔍 **Latest deploy log:** [${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
- 😎 **Deploy preview URL:** [${{ steps.deploy-urls.outputs.final_url }}](${{ steps.deploy-urls.outputs.final_url }})
- 🌳 **Environment:** ${{ steps.cloudflare-pages-deploy.outputs['pages-environment'] }}
- name: Remove label
if: ${{ github.event_name == 'pull_request_target' && contains(github.event.label.name, '🚀request-deploy') }}
uses: actions/github-script@v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: '🚀request-deploy'
})
# Comment on PR from the fork
comment:
name: Comment
runs-on: ubuntu-latest
# pull_request_target opened event from forked repo
if: ${{
github.event_name == 'pull_request_target' &&
github.event.action == 'opened' &&
github.event.pull_request.head.repo.fork == true
}}
steps:
- name: Create PR comment
run: |
cat << EOF > comment.md
# ⚠️ Deployments require the '🚀request-deploy' label
This repository is a forked repository. For security reasons, deployments from forked repositories are not automatic.
To request a deployment, add the '🚀request-deploy' label to this pull request. (Only some members can add labels).
EOF
gh pr comment ${{ github.event.number }} -R ${{ github.repository }} -F comment.md
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}