Skip to content

Commit

Permalink
Merge pull request #56 from brian-rose/better-preview
Browse files Browse the repository at this point in the history
New and improved preview actions ala Radar Cookbook
  • Loading branch information
brian-rose authored Jun 8, 2022
2 parents f146f29 + 43c48cd commit 6be6340
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name: preview-book
name: build-book
on:
pull_request:
workflow_dispatch:

jobs:
build:
build-and-upload:
runs-on: ubuntu-latest
defaults:
run:
shell: bash -l {0}
steps:
- uses: actions/checkout@v3
- uses: conda-incubator/setup-miniconda@master
- name: Setup conda environment
uses: conda-incubator/setup-miniconda@master
with:
channels: conda-forge
channel-priority: strict
Expand All @@ -21,17 +21,19 @@ jobs:
environment-file: render-environment.yml
mamba-version: '*'
use-mamba: true

# Build the book
- name: Build the book
run: |
jupyter-book build .
# Push the book's HTML to github-pages
# This will be published at /_preview/PRnumber/ relative to the main site
- name: Deploy to GitHub pages
uses: peaceiris/[email protected]
- name: Zip the book
run: |
set -x
set -e
if [ -f book.zip ]; then
rm -rf book.zip
fi
zip -r book.zip _build/html
- name: Upload zipped book artifact
uses: actions/upload-artifact@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./_build/html
enable_jekyll: false
destination_dir: _preview/${{github.event.number}}
name: book-zip-${{github.event.number}}
path: ./book.zip
8 changes: 6 additions & 2 deletions .github/workflows/deploy-book.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ name: deploy-book
# Only run this when the main branch changes
on:
push:
branches:
- main
workflow_dispatch:

jobs:
build:
build-and-deploy-book:
runs-on: ubuntu-latest
defaults:
run:
Expand All @@ -17,7 +20,8 @@ jobs:
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v3
- uses: conda-incubator/setup-miniconda@master
- name: Set up conda environment
uses: conda-incubator/setup-miniconda@master
with:
channels: conda-forge
channel-priority: strict
Expand Down
137 changes: 137 additions & 0 deletions .github/workflows/deploy-preview.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
name: deploy-preview
on:
workflow_run:
workflows:
- build-book
types:
- requested
- completed

jobs:
deploy:
runs-on: ubuntu-latest
defaults:
run:
shell: bash -l {0}
steps:
- uses: actions/checkout@v3
- name: Fetch Repo Name
id: repo-name
run: echo "::set-output name=value::$(echo '${{ github.repository }}' | awk -F '/' '{print $2}')" # just the repo name, not owner

- name: Set message value
run: |
echo "comment_message=👋 Thanks for opening this PR! The book will be automatically built with [GitHub Actions](https://github.com/features/actions). To see the status of your deployment, click below." >> $GITHUB_ENV
- name: Find Pull Request
uses: actions/github-script@v6
id: find-pull-request
with:
script: |
let pullRequestNumber = ''
let pullRequestHeadSHA = ''
core.info('Finding pull request...')
const pullRequests = await github.rest.pulls.list({owner: context.repo.owner, repo: context.repo.repo})
for (let pullRequest of pullRequests.data) {
if(pullRequest.head.sha === context.payload.workflow_run.head_commit.id) {
pullRequestHeadSHA = pullRequest.head.sha
pullRequestNumber = pullRequest.number
break
}
}
core.setOutput('number', pullRequestNumber)
core.setOutput('sha', pullRequestHeadSHA)
if(pullRequestNumber === '') {
core.info(
`No pull request associated with git commit SHA: ${context.payload.workflow_run.head_commit.id}`
)
}
else{
core.info(`Found pull request ${pullRequestNumber}, with head sha: ${pullRequestHeadSHA}`)
}
- name: Find preview comment
uses: peter-evans/find-comment@v2
if: steps.find-pull-request.outputs.number != ''
id: fc
with:
issue-number: '${{ steps.find-pull-request.outputs.number }}'
comment-author: 'github-actions[bot]'
body-includes: '${{ env.comment_message }}'

- name: Create preview comment
if: |
github.event.workflow_run.conclusion != 'success'
&& steps.find-pull-request.outputs.number != ''
&& steps.fc.outputs.comment-id == ''
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ steps.find-pull-request.outputs.number }}
body: |
${{ env.comment_message }}
🔍 Git commit SHA: ${{ steps.find-pull-request.outputs.sha }}
✅ Deployment Preview URL: In Progress
- name: Update preview comment
if: |
github.event.workflow_run.conclusion != 'success'
&& steps.find-pull-request.outputs.number != ''
&& steps.fc.outputs.comment-id != ''
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace
body: |
${{ env.comment_message }}
🔍 Git commit SHA: ${{ steps.find-pull-request.outputs.sha }}
✅ Deployment Preview URL: In Progress
- name: Download Artifact Book
if: |
github.event.workflow_run.conclusion == 'success'
&& steps.find-pull-request.outputs.number != ''
&& steps.fc.outputs.comment-id != ''
uses: dawidd6/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
workflow: build-book.yaml
run_id: ${{ github.event.workflow_run.id }}
name: book-zip-${{ steps.find-pull-request.outputs.number }}

- name: Unzip the book
if: |
github.event.workflow_run.conclusion == 'success'
&& steps.find-pull-request.outputs.number != ''
&& steps.fc.outputs.comment-id != ''
run: |
rm -rf notebooks/_build/html
unzip book.zip
rm -f book.zip
# Push the book's HTML to github-pages
# This will be published at /_preview/PRnumber/ relative to the main site
- name: Deploy to GitHub pages
if: |
github.event.workflow_run.conclusion == 'success'
&& steps.find-pull-request.outputs.number != ''
&& steps.fc.outputs.comment-id != ''
uses: peaceiris/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: notebooks/_build/html
enable_jekyll: false
destination_dir: _preview/${{ steps.find-pull-request.outputs.number }}

- name: Finalize preview comment
if: |
github.event.workflow_run.conclusion == 'success'
&& steps.find-pull-request.outputs.number != ''
&& steps.fc.outputs.comment-id != ''
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace
body: |
${{ env.comment_message }}
🔍 Git commit SHA: ${{ steps.find-pull-request.outputs.sha }}
✅ Deployment Preview URL: https://${{ github.repository_owner }}.github.io/${{ steps.repo-name.outputs.value }}/_preview/${{ steps.find-pull-request.outputs.number }}
4 changes: 2 additions & 2 deletions .github/workflows/link-checker.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: link-checker

on:
push:
pull_request:
workflow_dispatch:
schedule:
Expand All @@ -19,7 +18,8 @@ jobs:
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v3
- uses: conda-incubator/setup-miniconda@master
- name: Set up conda environment
uses: conda-incubator/setup-miniconda@master
with:
channels: conda-forge
channel-priority: strict
Expand Down
36 changes: 0 additions & 36 deletions .github/workflows/make-comment.yaml

This file was deleted.

0 comments on commit 6be6340

Please sign in to comment.