Skip to content

Commit cf65eab

Browse files
committed
use workflow_run for surge preview
1 parent ebee2a7 commit cf65eab

File tree

3 files changed

+83
-35
lines changed

3 files changed

+83
-35
lines changed

.github/workflows/main.yaml

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
name: CI
1+
name: Deploy Website to GitHub Pages
22

33
on:
44
push:
55
branches: main
6-
pull_request:
7-
branches: main
86

97
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
108
permissions:
@@ -18,7 +16,7 @@ concurrency:
1816
cancel-in-progress: true
1917

2018
jobs:
21-
build-and-deploy:
19+
build:
2220
runs-on: ubuntu-latest
2321

2422
steps:
@@ -50,19 +48,31 @@ jobs:
5048
run: |
5149
if (test -a _build/html/reports/*log); then cat _build/html/reports/*log ; fi
5250
53-
- name: Save Build
51+
- name: Save Build Folder
5452
if: always()
5553
uses: actions/upload-artifact@v4
5654
with:
5755
name: build
5856
path: _build/
5957

60-
- name: Publish to GitHub Pages
61-
if: github.ref == 'refs/heads/main'
62-
uses: peaceiris/actions-gh-pages@v4
58+
- name: Upload Pages Artifact
59+
uses: actions/upload-pages-artifact@v3
6360
with:
64-
github_token: ${{ secrets.GITHUB_TOKEN }}
65-
publish_dir: _build/html
66-
publish_branch: gh-pages
67-
cname: tutorial.xarray.dev
68-
enable_jekyll: false
61+
path: _build/html
62+
63+
# Publish Website to GitHub Pages if built successfully
64+
deploy:
65+
needs: build
66+
if: github.ref == 'refs/heads/main'
67+
runs-on: ubuntu-latest
68+
environment:
69+
name: github-pages
70+
url: ${{ steps.deployment.outputs.page_url }}
71+
72+
steps:
73+
- name: Setup Pages
74+
uses: actions/configure-pages@v5
75+
76+
- name: Deploy to GitHub Pages
77+
id: deployment
78+
uses: actions/deploy-pages@v4
Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,53 @@
1-
name: PR Preview
1+
name: Pull Request Build
22

3-
on: [pull_request_target]
4-
5-
permissions:
6-
pull-requests: write # allow surge-preview to create/update PR comments
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened, closed]
76

87
concurrency:
98
group: ${{ github.workflow }}-${{ github.ref }}
109
cancel-in-progress: true
1110

1211
jobs:
1312
preview:
14-
env:
15-
PREVIEW_URL: https://{{repository.owner}}-{{repository.name}}-{{job.name}}-pr-{{pr.number}}.surge.sh
1613
runs-on: ubuntu-latest
14+
defaults:
15+
run:
16+
shell: bash -el {0}
1717
steps:
1818
- name: Checkout repository
19+
if: github.event.action != 'closed'
1920
uses: actions/checkout@v4
2021

2122
- name: Setup JupyterBook Cache
23+
if: github.event.action != 'closed'
2224
uses: actions/cache@v4
2325
with:
2426
path: _build
2527
# NOTE: change key to "jupyterbook-DATE" to force rebuilding cache
2628
key: jupyterbook-20240517
2729

2830
- name: Install Conda environment with Micromamba
31+
if: github.event.action != 'closed'
2932
uses: mamba-org/setup-micromamba@v1
3033
with:
3134
environment-file: conda/conda-lock.yml
3235
environment-name: xarray-tutorial
3336
cache-environment: true
3437

3538
- name: Build JupyterBook
36-
# NOTE: login shell activates conda environment
37-
shell: bash -l {0}
39+
if: github.event.action != 'closed'
3840
run: |
3941
jupyter-book build ./ --warningiserror --keep-going
4042
4143
- name: Dump Build Logs
42-
if: always()
44+
if: github.event.action != 'closed'
4345
run: |
4446
if (test -a _build/html/reports/*log); then cat _build/html/reports/*log ; fi
4547
46-
- name: Install Surge.sh
47-
uses: actions/setup-node@v4
48-
49-
- run: npm install -g surge
50-
51-
- run: surge ./_build/html {{env.PREVIEW_URL}} --token $SURGE_TOKEN
52-
env:
53-
SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }}
54-
55-
- name: Add URL to Summary
56-
run: |
57-
echo "#### :rocket: Preview deployed at {{env.PREVIEW_URL}}" >> $GITHUB_STEP_SUMMARY
48+
- name: Upload artifact
49+
if: github.event.action != 'closed'
50+
uses: actions/upload-artifact@v4
51+
with:
52+
name: html
53+
path: book/_build/html

.github/workflows/surge_preview.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Pull Request Preview
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Pull Request Build"]
6+
types:
7+
- completed
8+
9+
permissions:
10+
pull-requests: write # allow surge-preview to create/update PR comments
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.event.workflow_run.id }}
14+
cancel-in-progress: true
15+
16+
jobs:
17+
# NOTE: match job name in pull_request.yaml
18+
preview:
19+
runs-on: ubuntu-latest
20+
if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }}
21+
22+
steps:
23+
# Ensure folder exists for PR 'closed' case
24+
- run: mkdir html
25+
26+
# Download built HTML from PR Build workflow
27+
- uses: actions/download-artifact@v4
28+
continue-on-error: true
29+
with:
30+
github-token: ${{ github.token }}
31+
run-id: ${{ github.event.workflow_run.id }}
32+
33+
- name: Manage Surge.sh Deployment
34+
id: preview_step
35+
uses: afc163/surge-preview@v1
36+
with:
37+
surge_token: ${{ secrets.SURGE_TOKEN }}
38+
github_token: ${{ secrets.GITHUB_TOKEN }}
39+
build: echo 'Uploading html/ folder contents to Surge.sh...'
40+
dist: html # NOTE: match upload_artifact name in pull_request.yaml
41+
failOnError: true
42+
teardown: true

0 commit comments

Comments
 (0)