Skip to content

Commit

Permalink
Update actions to preview on GitHub Pages (#51)
Browse files Browse the repository at this point in the history
* Update actions to preview on GitHub Pages

* Tweak paths
  • Loading branch information
brian-rose authored Jul 8, 2022
1 parent 9b19ac3 commit e10662a
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 92 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/build-site.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: build-site

on:
workflow_call:
inputs:
environment_name:
description: 'Name of conda environment to activate'
required: false
default: 'pythia'
type: string
environment_file:
description: 'Name of conda environment file'
required: false
default: 'environment.yml'
type: string
path_to_source:
description: 'Location of the Sphinx source relative to repo root'
required: false
default: './'
type: string
use_cached_environment:
description: 'Flag for whether we should attempt to retrieve a previously cached environment.'
required: false
default: 'true'
type: string # had a lot of trouble with boolean types, see https://github.com/actions/runner/issues/1483

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
shell: bash -l {0}
steps:
- uses: actions/checkout@v3

- name: Setup Mambaforge
uses: conda-incubator/setup-miniconda@v2
with:
miniforge-variant: Mambaforge
miniforge-version: latest
activate-environment: ${{ inputs.environment_name }}
use-mamba: true

- name: Set cache date
if: ${{ inputs.use_cached_environment == 'true' }}
run: echo "DATE=$(date +'%Y%m%d')" >> $GITHUB_ENV

- uses: actions/cache@v3
if: inputs.use_cached_environment == 'true'
with:
path: /usr/share/miniconda3/envs/${{ inputs.environment_name }}
# This should create a key that looks like 'linux-64-conda-environment.yml-[HASH]-DATE'
# Logic inspired by https://dev.to/epassaro/caching-anaconda-environments-on-github-actions-2d08
key: ${{ format('linux-64-conda-{0}-{1}-{2}', inputs.environment_file, hashFiles(format('{0}', inputs.environment_file)), env.DATE )}}
id: cache

- name: Update environment
if: |
inputs.use_cached_environment != 'true'
|| steps.cache.outputs.cache-hit != 'true'
run: mamba env update -n ${{ inputs.environment_name }} -f ${{ inputs.environment_file }}

- name: Build the site
run: |
cd ${{ inputs.path_to_source }}
make -j4 html
- name: Zip the site
run: |
set -x
set -e
if [ -f site.zip ]; then
rm -rf site.zip
fi
zip -r site.zip ${{ inputs.path_to_source }}/_build/html
- name: Upload zipped site artifact
uses: actions/upload-artifact@v3
with:
name: site-zip-${{github.event.number}}
path: ./site.zip
55 changes: 0 additions & 55 deletions .github/workflows/ci.yaml

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/delete-preview.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: delete-preview

# Only run this when the main branch changes
on:
pull_request_target:
types: closed

jobs:
delete:
runs-on: ubuntu-latest
defaults:
run:
shell: bash -l {0}
steps:
- name: Checkout gh-pages branch
uses: actions/checkout@v3
with:
ref: gh-pages
- name: Delete preview files
run: |
rm -rf _preview/${{ github.event.pull_request.number }}
- name: Commit the changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
branch: gh-pages
commit_message: Delete preview for pull request \#${{ github.event.pull_request.number }}
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
name: preview-site
name: deploy-preview
on:
workflow_run:
workflows:
- deploy-site
- trigger-site-build
types:
- requested
- completed

jobs:
deploy:
if: github.repository == 'ProjectPythiaCookbooks/projectpythiacookbooks.github.io'
runs-on: ubuntu-latest
defaults:
run:
shell: bash
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=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
echo "comment_message=👋 Thanks for opening this PR! The site 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
Expand All @@ -26,7 +31,7 @@ jobs:
let pullRequestNumber = ''
let pullRequestHeadSHA = ''
core.info('Finding pull request...')
const pullRequests = await github.pulls.list({owner: context.repo.owner, repo: context.repo.repo})
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
Expand All @@ -44,15 +49,17 @@ jobs:
else{
core.info(`Found pull request ${pullRequestNumber}, with head sha: ${pullRequestHeadSHA}`)
}
- name: Find Comment
- 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 comment

- name: Create preview comment
if: |
github.event.workflow_run.conclusion != 'success'
&& steps.find-pull-request.outputs.number != ''
Expand All @@ -62,9 +69,10 @@ jobs:
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
🚧 Deployment in progress for git commit SHA: ${{ steps.find-pull-request.outputs.sha }}
- name: Update comment
- name: Update preview comment
if: |
github.event.workflow_run.conclusion != 'success'
&& steps.find-pull-request.outputs.number != ''
Expand All @@ -75,46 +83,46 @@ jobs:
edit-mode: replace
body: |
${{ env.comment_message }}
🔍 Git commit SHA: ${{ steps.find-pull-request.outputs.sha }}
✅ Deployment Preview URL: In Progress
🚧 Deployment in progress for git commit SHA: ${{ steps.find-pull-request.outputs.sha }}
- name: Download Artifact site
- name: Download Artifact Site
if: |
github.event.workflow_run.conclusion == 'success'
&& steps.find-pull-request.outputs.number != ''
&& steps.fc.outputs.comment-id != ''
uses: dawidd6/action-download-artifact@v2.21.1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
workflow: ci.yaml
workflow: build-site.yaml
run_id: ${{ github.event.workflow_run.id }}
name: site-zip
- name: Unzip site
if: |
name: site-zip-${{ steps.find-pull-request.outputs.number }}

- name: Unzip the site
if: |
github.event.workflow_run.conclusion == 'success'
&& steps.find-pull-request.outputs.number != ''
&& steps.fc.outputs.comment-id != ''
run: |
rm -rf ./site/_build/html
rm -rf site/_build/html
unzip site.zip
rm -f site.zip
# Push the site's HTML to Netlify and get the preview URL
- name: Deploy to Netlify
# Push the site'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 != ''
id: netlify
uses: nwtgck/actions-netlify@v1.2
uses: peaceiris/actions-gh-pages@v3.8.0
with:
publish-dir: ./site/_build/html
production-deploy: false
github-token: ${{ secrets.GITHUB_TOKEN }}
enable-commit-comment: false
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
timeout-minutes: 5
- name: Update site Preview comment
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: site/_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 != ''
Expand All @@ -125,6 +133,5 @@ jobs:
edit-mode: replace
body: |
${{ env.comment_message }}
🔍 Git commit SHA: ${{ steps.find-pull-request.outputs.sha }}
✅ Deployment Preview URL: ${{ steps.netlify.outputs.deploy-url }}
🔍 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 }}
42 changes: 42 additions & 0 deletions .github/workflows/deploy-site.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: deploy-site

on:
# Trigger the workflow on push to main branch
push:
branches:
- main
workflow_dispatch:

jobs:
build:
uses: ./.github/workflows/build-site.yaml
with:
environment_name: pythia-tutorial-dev
environment_file: environment.yml
path_to_source: ./site

deploy:
needs: build
runs-on: ubuntu-latest
defaults:
run:
shell: bash -l {0}
steps:
- name: Download Artifact Site
uses: actions/download-artifact@v3
with:
name: site-zip-${{ github.event.number }}

- name: Unzip the site
run: |
rm -rf site/_build/html
unzip site.zip
rm -f site.zip
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3.8.0
if: github.ref == 'refs/heads/main'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: site/_build/html
keep_files: true # This preserves existing previews from open PRs
13 changes: 13 additions & 0 deletions .github/workflows/trigger-site-build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: trigger-site-build
on:
pull_request:
workflow_dispatch:

jobs:
build:
uses: ./.github/workflows/build-site.yaml
with:
environment_name: pythia-tutorial-dev
environment_file: environment.yml
path_to_source: ./site
use_cached_environment: 'true' # This is default, not strickly needed. Set to 'false' to always build a new environment
4 changes: 0 additions & 4 deletions .readthedocs.yaml

This file was deleted.

File renamed without changes.

0 comments on commit e10662a

Please sign in to comment.