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.vars1.outputs.pr_closed_merged }}
46+ steps :
47+ - id : vars1
48+ run : echo "pr_closed_merged=${{ github.event.pull_request.closed_at || github.event.pull_request.merged }}" >> "$GITHUB_OUTPUT"
49+
3650 # Build job
3751 build :
3852 runs-on : ubuntu-22.04
53+ needs : vars
54+ # Only if PR not closed or merged
55+ if : ! needs.vars.outputs.pr_closed_merged
3956 steps :
4057 - uses : actions/checkout@v4
4158 with :
90107 name : github-pages
91108 url : ${{ steps.deployment.outputs.page_url }}
92109 runs-on : ubuntu-22.04
93- needs : build
110+ needs :
111+ - vars
112+ - build
94113 # Only deploy if on main branch
95114 if : github.ref == 'refs/heads/main'
96115 steps :
@@ -111,9 +130,11 @@ jobs:
111130
112131 deploy-preview :
113132 runs-on : ubuntu-22.04
114- needs : build
115- # Only deploy if pull request
116- if : ${{ github.event.pull_request }}
133+ needs :
134+ - vars
135+ - build
136+ # Only deploy if non-closed/non-merged pull request
137+ if : github.event.pull_request && ! needs.vars.outputs.pr_closed_merged
117138 steps :
118139 - name : Download artifact
119140 uses : actions/download-artifact@v4
@@ -142,4 +163,28 @@ jobs:
142163 message : |
143164 Pull Request Live Preview
144165 :---:
145- | <p><br />:rocket: View preview at <br />https://${{ env.preview_subdomain }}.${{ env.domain }}/${{ env.preview_segment }}/<br /></p>
166+ | <p><br />:rocket: View preview at <br />https://${{ env.preview_subdomain }}.${{ env.domain }}/${{ env.preview_segment }}/<br /><br /></p>
167+
168+ remove-preview :
169+ runs-on : ubuntu-22.04
170+ needs :
171+ - vars
172+ # Only deploy if non-closed/non-merged pull request
173+ if : github.event.pull_request && needs.vars.outputs.pr_closed_merged
174+ steps :
175+ - name : Create deletion notice
176+ run : mkdir -p public && echo "This PR has been closed or merged. The preview has been removed." > public/index.html
177+
178+ - name : Copy empty folder to host
179+ uses : appleboy/scp-action@v0.1.7
180+ with :
181+ host : ${{ env.uberspace_host }}
182+ username : ${{ env.uberspace_user }}
183+ key : ${{ secrets.SSH_KEY }}
184+ port : 22
185+ timeout : 1m
186+ command_timeout : 2m
187+ target : ${{ env.uberspace_virtual_base }}/${{ env.uberspace_user }}/${{ env.preview_subdomain }}.${{ env.domain }}/${{ env.preview_segment }}
188+ source : " public/*"
189+ rm : true
190+ strip_components : 1
0 commit comments