Skip to content

Commit 8256ad9

Browse files
authored
Merge pull request #64 from OpenRailAssociation/preview-prs-2
Deploy preview for pull requests on separate URL
2 parents 79d8ad5 + 012f33f commit 8256ad9

File tree

1 file changed

+118
-7
lines changed

1 file changed

+118
-7
lines changed

.github/workflows/hugo.yaml

Lines changed: 118 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ on:
77

88
# Run on PRs, but only build
99
pull_request:
10+
types:
11+
- opened
12+
- reopened
13+
- synchronize
14+
- closed
1015
# Allows to run this workflow manually from the Actions tab
1116
workflow_dispatch:
1217

@@ -15,17 +20,42 @@ permissions:
1520
contents: read
1621
pages: write
1722
id-token: write
23+
pull-requests: write
1824

1925
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
2026
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
2127
concurrency:
22-
group: "pages"
28+
group: "preview-pages"
2329
cancel-in-progress: false
2430

31+
# Set environment variables
32+
env:
33+
domain: openrailassociation.org
34+
preview_subdomain: web-preview
35+
preview_segment: pr-${{ github.event.number }}
36+
uberspace_host: uberspace1.openrailassociation.org
37+
uberspace_user: openrail
38+
uberspace_virtual_base: /var/www/virtual
39+
2540
jobs:
41+
# Set vars
42+
vars:
43+
runs-on: ubuntu-22.04
44+
outputs:
45+
pr_closed_merged: ${{ steps.pr_status.outputs.pr_closed_merged }}
46+
datetime: ${{ steps.datetime.outputs.datetime }}
47+
steps:
48+
- id: pr_status
49+
run: echo "pr_closed_merged=${{ github.event.pull_request.closed_at || github.event.pull_request.merged }}" >> "$GITHUB_OUTPUT"
50+
- id: datetime
51+
run: echo "datetime=$(date '+%Y-%m-%d %H:%M:%S %Z')" >> "$GITHUB_OUTPUT"
52+
2653
# Build job
2754
build:
2855
runs-on: ubuntu-22.04
56+
needs: vars
57+
# Only if PR not closed or merged
58+
if: needs.vars.outputs.pr_closed_merged == 'false'
2959
steps:
3060
- uses: actions/checkout@v4
3161
with:
@@ -38,8 +68,13 @@ jobs:
3868
hugo-version: "0.132.2"
3969
extended: true
4070

41-
- name: Build
71+
- name: Build production site
4272
run: hugo
73+
if: github.ref == 'refs/heads/main'
74+
75+
- name: Build preview site
76+
run: hugo -b https://${{ env.preview_subdomain }}.${{ env.domain }}/${{ env.preview_segment }}/
77+
if: ${{ github.event.pull_request }}
4378

4479
- name: Upload artifact
4580
uses: actions/upload-artifact@v4
@@ -52,7 +87,10 @@ jobs:
5287
# Checking links using lychee
5388
linkchecker:
5489
runs-on: ubuntu-22.04
55-
needs: build
90+
needs:
91+
- vars
92+
- build
93+
if: needs.vars.outputs.pr_closed_merged == 'false'
5694
steps:
5795
- name: Download artifact
5896
uses: actions/download-artifact@v4
@@ -64,18 +102,20 @@ jobs:
64102
id: lychee
65103
uses: lycheeverse/lychee-action@v1
66104
with:
67-
args: '-r 5 -u "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Apple WebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36" --exclude sncf.com --exclude nge.flatland.cloud --max-concurrency 1 .'
105+
args: '-r 5 -u "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Apple WebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36" --include-fragments --exclude sncf.com --exclude nge.flatland.cloud --max-concurrency 1 .'
68106
# Fail on errors
69107
fail: false
70108
# TODO: Implement https://github.com/lycheeverse/lychee/issues/989 once it's done. Will help with LinkedIn rate limits
71109

72-
# Deployment job
73-
deploy:
110+
# Deployment jobs
111+
deploy-production:
74112
environment:
75113
name: github-pages
76114
url: ${{ steps.deployment.outputs.page_url }}
77115
runs-on: ubuntu-22.04
78-
needs: build
116+
needs:
117+
- vars
118+
- build
79119
# Only deploy if on main branch
80120
if: github.ref == 'refs/heads/main'
81121
steps:
@@ -93,3 +133,74 @@ jobs:
93133
- name: Deploy to GitHub Pages
94134
id: deployment
95135
uses: actions/deploy-pages@v4
136+
137+
deploy-preview:
138+
runs-on: ubuntu-22.04
139+
needs:
140+
- vars
141+
- build
142+
# Only deploy if non-closed/non-merged pull request
143+
if: github.event.pull_request && needs.vars.outputs.pr_closed_merged == 'false'
144+
steps:
145+
- name: Download artifact
146+
uses: actions/download-artifact@v4
147+
with:
148+
name: public
149+
path: ./public
150+
151+
- name: Copy website to host
152+
uses: appleboy/scp-action@v0.1.7
153+
with:
154+
host: ${{ env.uberspace_host }}
155+
username: ${{ env.uberspace_user }}
156+
key: ${{ secrets.SSH_KEY }}
157+
port: 22
158+
timeout: 1m
159+
command_timeout: 2m
160+
target: ${{ env.uberspace_virtual_base }}/${{ env.uberspace_user }}/${{ env.preview_subdomain }}.${{ env.domain }}/${{ env.preview_segment }}
161+
source: "public/*"
162+
rm: true
163+
strip_components: 1
164+
165+
- name: Inform about Preview URL
166+
uses: marocchino/sticky-pull-request-comment@52423e01640425a022ef5fd42c6fb5f633a02728 # v2.9.1
167+
with:
168+
header: pr-preview
169+
message: |
170+
Pull Request Live Preview
171+
:---:
172+
| <p><br />:rocket: View preview at <br />https://${{ env.preview_subdomain }}.${{ env.domain }}/${{ env.preview_segment }}/<br /><br /></p>
173+
| <p>Last updated: ${{ needs.vars.outputs.datetime }}</p>
174+
175+
remove-preview:
176+
runs-on: ubuntu-22.04
177+
needs:
178+
- vars
179+
# Only deploy if non-closed/non-merged pull request
180+
if: github.event.pull_request && needs.vars.outputs.pr_closed_merged != 'false'
181+
steps:
182+
- name: Create deletion notice
183+
run: mkdir -p public && echo "This PR has been closed or merged. The preview has been removed." > public/index.html
184+
185+
- name: Copy empty folder to host
186+
uses: appleboy/scp-action@v0.1.7
187+
with:
188+
host: ${{ env.uberspace_host }}
189+
username: ${{ env.uberspace_user }}
190+
key: ${{ secrets.SSH_KEY }}
191+
port: 22
192+
timeout: 1m
193+
command_timeout: 2m
194+
target: ${{ env.uberspace_virtual_base }}/${{ env.uberspace_user }}/${{ env.preview_subdomain }}.${{ env.domain }}/${{ env.preview_segment }}
195+
source: "public/*"
196+
rm: true
197+
strip_components: 1
198+
199+
- name: Inform about preview removal
200+
uses: marocchino/sticky-pull-request-comment@52423e01640425a022ef5fd42c6fb5f633a02728 # v2.9.1
201+
with:
202+
header: pr-preview
203+
message: |
204+
Pull Request Live Preview
205+
:---:
206+
| <p><br />:x: Preview has been removed.<br /><br /></p>

0 commit comments

Comments
 (0)