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
3338 uberspace_virtual_base : /var/www/virtual
3439
3540jobs :
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+
3653 # Build job
3754 build :
3855 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'
3959 steps :
4060 - uses : actions/checkout@v4
4161 with :
@@ -85,12 +105,14 @@ jobs:
85105 # # TODO: Implement https://github.com/lycheeverse/lychee/issues/989 once it's done. Will help with LinkedIn rate limits
86106
87107 # Deployment jobs
88- deploy :
108+ deploy-production :
89109 environment :
90110 name : github-pages
91111 url : ${{ steps.deployment.outputs.page_url }}
92112 runs-on : ubuntu-22.04
93- needs : build
113+ needs :
114+ - vars
115+ - build
94116 # Only deploy if on main branch
95117 if : github.ref == 'refs/heads/main'
96118 steps :
@@ -111,9 +133,11 @@ jobs:
111133
112134 deploy-preview :
113135 runs-on : ubuntu-22.04
114- needs : build
115- # Only deploy if pull request
116- if : ${{ github.event.pull_request }}
136+ needs :
137+ - vars
138+ - build
139+ # Only deploy if non-closed/non-merged pull request
140+ if : github.event.pull_request && needs.vars.outputs.pr_closed_merged == 'false'
117141 steps :
118142 - name : Download artifact
119143 uses : actions/download-artifact@v4
@@ -142,4 +166,38 @@ jobs:
142166 message : |
143167 Pull Request Live Preview
144168 :---:
145- | <p><br />:rocket: View preview at <br />https://${{ env.preview_subdomain }}.${{ env.domain }}/${{ env.preview_segment }}/<br /></p>
169+ | <p><br />:rocket: View preview at <br />https://${{ env.preview_subdomain }}.${{ env.domain }}/${{ env.preview_segment }}/<br /><br /></p>
170+ | <p>Last updated: ${{ needs.vars.outputs.datetime }}</p>
171+
172+ remove-preview :
173+ runs-on : ubuntu-22.04
174+ needs :
175+ - vars
176+ # Only deploy if non-closed/non-merged pull request
177+ if : github.event.pull_request && needs.vars.outputs.pr_closed_merged != 'false'
178+ steps :
179+ - name : Create deletion notice
180+ run : mkdir -p public && echo "This PR has been closed or merged. The preview has been removed." > public/index.html
181+
182+ - name : Copy empty folder to host
183+ uses : appleboy/scp-action@v0.1.7
184+ with :
185+ host : ${{ env.uberspace_host }}
186+ username : ${{ env.uberspace_user }}
187+ key : ${{ secrets.SSH_KEY }}
188+ port : 22
189+ timeout : 1m
190+ command_timeout : 2m
191+ target : ${{ env.uberspace_virtual_base }}/${{ env.uberspace_user }}/${{ env.preview_subdomain }}.${{ env.domain }}/${{ env.preview_segment }}
192+ source : " public/*"
193+ rm : true
194+ strip_components : 1
195+
196+ - name : Inform about preview removal
197+ uses : marocchino/sticky-pull-request-comment@52423e01640425a022ef5fd42c6fb5f633a02728 # v2.9.1
198+ with :
199+ header : pr-preview
200+ message : |
201+ Pull Request Live Preview
202+ :---:
203+ | <p><br />:x: Preview has been removed.<br /><br /></p>
0 commit comments