Skip to content

Commit 7523c1a

Browse files
authored
Merge branch 'main' into use-netlify-for-preview
2 parents de97a8a + 6a63901 commit 7523c1a

File tree

2 files changed

+152
-6
lines changed

2 files changed

+152
-6
lines changed

.github/workflows/ci.yaml

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
name: deploy-site
2-
32
on:
43
push:
5-
branches:
6-
- main
7-
4+
pull_request:
5+
workflow_dispatch:
86
jobs:
9-
deploy-site:
7+
build:
108
runs-on: ubuntu-latest
119
defaults:
1210
run:
1311
shell: bash -l {0}
12+
if: github.repository == 'ProjectPythia/projectpythia.github.io'
1413
steps:
14+
- name: Cancel previous runs
15+
uses: styfle/[email protected]
16+
with:
17+
access_token: ${{ github.token }}
1518
- uses: actions/checkout@v2
1619

1720
- uses: conda-incubator/setup-miniconda@v2
@@ -30,9 +33,22 @@ jobs:
3033
conda env list
3134
cd portal
3235
make -j4 html
36+
- name: Zip the site
37+
run: |
38+
set -x
39+
set -e
40+
if [ -f site.zip ]; then
41+
rm -rf site.zip
42+
fi
43+
zip -r site.zip ./portal/_build/html
44+
- uses: actions/upload-artifact@v2
45+
with:
46+
name: site-zip
47+
path: ./site.zip
3348

3449
- name: Deploy
35-
uses: peaceiris/[email protected]
50+
if: github.ref == 'refs/heads/main'
51+
uses: peaceiris/[email protected]
3652
with:
3753
github_token: ${{ secrets.GITHUB_TOKEN }}
3854
publish_dir: ./portal/_build/html

.github/workflows/preview.yaml

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
name: preview-site
2+
on:
3+
workflow_run:
4+
workflows:
5+
- deploy-site
6+
types:
7+
- requested
8+
- completed
9+
jobs:
10+
deploy:
11+
if: github.repository == 'ProjectPythia/projectpythia.github.io'
12+
runs-on: ubuntu-latest
13+
defaults:
14+
run:
15+
shell: bash
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Set message value
19+
run: |
20+
echo "comment_message=This pull request is being automatically built with [GitHub Actions](https://github.com/features/actions) and [Netlify](https://www.netlify.com/). To see the status of your deployment, click below." >> $GITHUB_ENV
21+
- name: Find Pull Request
22+
uses: actions/github-script@v4
23+
id: find-pull-request
24+
with:
25+
script: |
26+
let pullRequestNumber = ''
27+
let pullRequestHeadSHA = ''
28+
core.info('Finding pull request...')
29+
const pullRequests = await github.pulls.list({owner: context.repo.owner, repo: context.repo.repo})
30+
for (let pullRequest of pullRequests.data) {
31+
if(pullRequest.head.sha === context.payload.workflow_run.head_commit.id) {
32+
pullRequestHeadSHA = pullRequest.head.sha
33+
pullRequestNumber = pullRequest.number
34+
break
35+
}
36+
}
37+
core.setOutput('number', pullRequestNumber)
38+
core.setOutput('sha', pullRequestHeadSHA)
39+
if(pullRequestNumber === '') {
40+
core.info(
41+
`No pull request associated with git commit SHA: ${context.payload.workflow_run.head_commit.id}`
42+
)
43+
}
44+
else{
45+
core.info(`Found pull request ${pullRequestNumber}, with head sha: ${pullRequestHeadSHA}`)
46+
}
47+
- name: Find Comment
48+
uses: peter-evans/find-comment@v1
49+
if: steps.find-pull-request.outputs.number != ''
50+
id: fc
51+
with:
52+
issue-number: '${{ steps.find-pull-request.outputs.number }}'
53+
comment-author: 'github-actions[bot]'
54+
body-includes: '${{ env.comment_message }}'
55+
- name: Create comment
56+
if: |
57+
github.event.workflow_run.conclusion != 'success'
58+
&& steps.find-pull-request.outputs.number != ''
59+
&& steps.fc.outputs.comment-id == ''
60+
uses: peter-evans/create-or-update-comment@v1
61+
with:
62+
issue-number: ${{ steps.find-pull-request.outputs.number }}
63+
body: |
64+
${{ env.comment_message }}
65+
66+
🚧 Deployment in progress for git commit SHA: ${{ steps.find-pull-request.outputs.sha }}
67+
- name: Update comment
68+
if: |
69+
github.event.workflow_run.conclusion != 'success'
70+
&& steps.find-pull-request.outputs.number != ''
71+
&& steps.fc.outputs.comment-id != ''
72+
uses: peter-evans/create-or-update-comment@v1
73+
with:
74+
comment-id: ${{ steps.fc.outputs.comment-id }}
75+
edit-mode: replace
76+
body: |
77+
${{ env.comment_message }}
78+
79+
🚧 Deployment in progress for git commit SHA: ${{ steps.find-pull-request.outputs.sha }}
80+
- name: Download Artifact site
81+
if: |
82+
github.event.workflow_run.conclusion == 'success'
83+
&& steps.find-pull-request.outputs.number != ''
84+
&& steps.fc.outputs.comment-id != ''
85+
uses: dawidd6/[email protected]
86+
with:
87+
github_token: ${{ secrets.GITHUB_TOKEN }}
88+
workflow: ci.yaml
89+
run_id: ${{ github.event.workflow_run.id }}
90+
name: site-zip
91+
- name: Unzip site
92+
if: |
93+
github.event.workflow_run.conclusion == 'success'
94+
&& steps.find-pull-request.outputs.number != ''
95+
&& steps.fc.outputs.comment-id != ''
96+
run: |
97+
rm -rf ./portal/_build/html
98+
unzip site.zip
99+
rm -f site.zip
100+
# Push the site's HTML to Netlify and get the preview URL
101+
- name: Deploy to Netlify
102+
if: |
103+
github.event.workflow_run.conclusion == 'success'
104+
&& steps.find-pull-request.outputs.number != ''
105+
&& steps.fc.outputs.comment-id != ''
106+
id: netlify
107+
uses: nwtgck/[email protected]
108+
with:
109+
publish-dir: ./portal/_build/html
110+
production-deploy: false
111+
github-token: ${{ secrets.GITHUB_TOKEN }}
112+
enable-commit-comment: false
113+
env:
114+
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
115+
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
116+
timeout-minutes: 5
117+
- name: Update site Preview comment
118+
if: |
119+
github.event.workflow_run.conclusion == 'success'
120+
&& steps.find-pull-request.outputs.number != ''
121+
&& steps.fc.outputs.comment-id != ''
122+
uses: peter-evans/create-or-update-comment@v1
123+
with:
124+
comment-id: ${{ steps.fc.outputs.comment-id }}
125+
edit-mode: replace
126+
body: |
127+
${{ env.comment_message }}
128+
129+
🔍 Git commit SHA: ${{ steps.find-pull-request.outputs.sha }}
130+
✅ Deployment Preview URL: ${{ steps.netlify.outputs.deploy-url }}

0 commit comments

Comments
 (0)