Skip to content

Commit f7484de

Browse files
committed
ci: pull request preview workflow
1 parent c3058a3 commit f7484de

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

.github/workflows/pr-preview.yml

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#################### 🚧 WARNING: READ THIS BEFORE USING THIS FILE 🚧 ####################
2+
#
3+
#
4+
#
5+
# IF YOU DON'T KNOW WHAT YOU'RE DOING, YOU CAN EASILY LEAK SECRETS BY USING A
6+
# `pull_request_target` WORKFLOW INSTEAD OF `pull_request`! SERIOUSLY, DO NOT
7+
# BLINDLY COPY AND PASTE THIS FILE WITHOUT UNDERSTANDING THE FULL IMPLICATIONS
8+
# OF WHAT YOU'RE DOING! WE HAVE TESTED THIS FOR OUR OWN USE CASES, WHICH ARE
9+
# NOT NECESSARILY THE SAME AS YOURS! WHILE WE AREN'T EXPOSING ANY OF OUR SECRETS,
10+
# ONE COULD EASILY DO SO BY MODIFYING OR ADDING A STEP TO THIS WORKFLOW!
11+
#
12+
#
13+
#
14+
#################### 🚧 WARNING: READ THIS BEFORE USING THIS FILE 🚧 ####################
15+
16+
name: Docs - Preview Deployment
17+
on:
18+
pull_request_target:
19+
types:
20+
- opened
21+
- edited
22+
- closed
23+
paths:
24+
- "**/**/**.rs"
25+
- "**/**/**.toml"
26+
- "**/**/**.css"
27+
- "**.config.js"
28+
- package.json
29+
- Cargo.toml
30+
31+
# cancel in-progress runs on new commits to same PR (github.event.number)
32+
concurrency:
33+
group: ${{ github.workflow }}-${{ github.event.number || github.sha }}
34+
cancel-in-progress: true
35+
36+
jobs:
37+
deploy-preview:
38+
if: ${{ github.event.action != "closed" }}
39+
permissions:
40+
contents: read
41+
pull-requests: write
42+
deployments: write
43+
runs-on: ubuntu-latest
44+
name: Deploy Preview to Cloudflare Pages
45+
env:
46+
BRANCH_NAME: preview-${{ github.head_ref }}
47+
ACTION_RUN: ${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}}
48+
steps:
49+
- uses: actions/checkout@v3
50+
with:
51+
submodules: "recursive"
52+
ref: ${{ github.event.pull_request.head.ref }}
53+
repository: ${{ github.event.pull_request.head.repo.full_name }}
54+
- name: Declare some variables
55+
shell: bash
56+
run: |
57+
echo "SHA_SHORT=$(git rev-parse --short "$GITHUB_SHA")" >> "$GITHUB_ENV"
58+
59+
- name: Create comment
60+
id: comment
61+
uses: peter-evans/create-or-update-comment@v4
62+
with:
63+
issue-number: ${{ github.event.pull_request.number }}
64+
comment-author: 'github-actions[bot]'
65+
body: |
66+
## ⚡ Cloudflare Pages Deployment
67+
| Name | Status | Preview |
68+
| :--- | :----- | :------ |
69+
| ${{BRANCH_NAME}} | 🔨 Building ([Logs](${env.ACTION_RUN})) | waiting... |
70+
71+
# Build Rust Page
72+
- uses: actions-rs/toolchain@v1
73+
with:
74+
profile: minimal
75+
toolchain: nightly-2024-02-12
76+
target: wasm32-unknown-unknown
77+
override: true
78+
79+
- uses: Swatinem/rust-cache@v2
80+
- uses: davidB/rust-cargo-make@v1
81+
82+
- name: Pre Build
83+
run: |
84+
npm i
85+
- name: Build
86+
run: cargo make build
87+
88+
- name: Deploy
89+
id: deploy
90+
uses: cloudflare/wrangler-action@v3
91+
with:
92+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
93+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
94+
command: pages deploy ./dist --project-name=homepage --branch="${ env.BRANCH_NAME }"
95+
96+
- name: Create comment
97+
uses: peter-evans/create-or-update-comment@v4
98+
with:
99+
issue-number: ${{ github.event.pull_request.number }}
100+
comment-id: ${{ steps.comment.outputs.comment-id }}
101+
edit-mode: replace
102+
body: |
103+
## ⚡ Cloudflare Pages Deployment
104+
| Name | Status | Preview |
105+
| :--- | :----- | :------ |
106+
| ${{BRANCH_NAME}} | ✅ Ready ([Logs](${env.ACTION_RUN})) | [${{env.SHA_SHORT}}](${{ steps.deploy.outputs.deployment-url }}) |
107+
108+
# remove-preview:
109+
# if: ${{ github.event.action == "closed" }}
110+
# permissions:
111+
# contents: read
112+
# pull-requests: write
113+
# deployments: write
114+
# runs-on: ubuntu-latest
115+
# name: Remove Preview of Cloudflare Pages
116+
# steps:
117+
# - uses: actions/checkout@v3
118+
# with:
119+
# submodules: "recursive"
120+
# ref: ${{ github.event.pull_request.head.ref }}
121+
# repository: ${{ github.event.pull_request.head.repo.full_name }}
122+
123+
# - name: Deploy
124+
# id: deploy
125+
# uses: cloudflare/wrangler-action@v3
126+
# with:
127+
# apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
128+
# accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
129+
# command: pages --project-name=homepage --branch="${ env.BRANCH_NAME }"
130+
131+
# - name: Create comment
132+
# uses: peter-evans/create-or-update-comment@v4
133+
# with:
134+
# issue-number: ${{ github.event.pull_request.number }}
135+
# comment-author: 'github-actions[bot]'
136+
# body: |
137+
# ## ⚡ Removing Cloudflare Pages Preview
138+
# | Name | Status |
139+
# | :--- | :----- |
140+
# | ${{BRANCH_NAME}} | ✅ Removed |

0 commit comments

Comments
 (0)