Skip to content

Commit

Permalink
Better documentation build workflow from 2021 GA Hackathon (#1987)
Browse files Browse the repository at this point in the history
* Remove old pipelines

* First version of new workflow

* Simplify for TARDIS use case

* Simplify for TARDIS use case (more)

* Use deploy branch variable again

* Add clean docs workflow

* Pre-merge changes

* Add bot comments with results

* Tweak triggers

* Final changes
  • Loading branch information
epassaro authored May 2, 2022
1 parent d1dffde commit 37a9769
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 105 deletions.
169 changes: 169 additions & 0 deletions .github/workflows/build-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# For more information about TARDIS pipelines, please refer to:
#
# https://tardis-sn.github.io/tardis/development/continuous_integration.html

name: build-docs

on:

push:
branches:
- master

pull_request_target:
branches:
- master

types:
- opened
- reopened
- synchronize
- labeled # requires the `build-docs` label

workflow_dispatch: # manual trigger

env:
DEPLOY_BRANCH: gh-pages # deployed docs branch
ROOT_REDIRECT: latest # https://tardis-sn.github.io/tardis/latest

concurrency:
group: build-docs-${{ github.ref }}
cancel-in-progress: true

defaults:
run:
shell: bash -l {0}

jobs:
build:

if: github.event_name == 'push' ||
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'pull_request_target' &&
contains(github.event.pull_request.labels.*.name, 'build-docs'))

runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2
if: github.event_name != 'pull_request_target'

- name: Checkout pull/${{ github.event.number }}
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
if: github.event_name == 'pull_request_target'

- name: Setup environment
uses: conda-incubator/setup-miniconda@v2
with:
miniforge-variant: Mambaforge
miniforge-version: latest
environment-file: tardis_env3.yml
activate-environment: tardis
use-mamba: true

- name: Install package
run: pip install -e .

- name: Build documentation
run: cd docs/ && make html

- name: Set destination directory
run: |
BRANCH=$(echo ${GITHUB_REF#refs/heads/})
if [[ $EVENT == push ]] || [[ $EVENT == workflow_dispatch ]]; then
if [[ $BRANCH == $DEFAULT ]]; then
echo "DEST_DIR=latest" >> $GITHUB_ENV
else
echo "DEST_DIR=branch/$BRANCH" >> $GITHUB_ENV
fi
elif [[ $EVENT == pull_request_target ]]; then
echo "DEST_DIR=pull/$PR" >> $GITHUB_ENV
else
echo "Unexpected event trigger $EVENT"
exit 1
fi
cat $GITHUB_ENV
env:
DEFAULT: ${{ github.event.repository.default_branch }}
EVENT: ${{ github.event_name }}
PR: ${{ github.event.number }}

- name: Deploy ${{ env.DEST_DIR }}
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.BOT_TOKEN }}
publish_branch: ${{ env.DEPLOY_BRANCH }}
publish_dir: ./docs/_build/html
destination_dir: ${{ env.DEST_DIR }}
user_name: 'TARDIS Bot'
user_email: '[email protected]'

- name: Redirect root
run: |
mkdir redirects && cd redirects
echo '<head>' >> index.html
echo ' <meta http-equiv="Refresh" content="0; url='/${{ github.event.repository.name }}/${{ env.ROOT_REDIRECT }}'"/>' >> index.html
echo '</head>' >> index.html
- name: Deploy redirect
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.BOT_TOKEN }}
publish_branch: ${{ env.DEPLOY_BRANCH }}
publish_dir: ./redirects
keep_files: true
user_name: 'TARDIS Bot'
user_email: '[email protected]'

- name: Find comment
uses: peter-evans/find-comment@v1
id: fc
with:
issue-number: ${{ github.event.number }}
body-includes: Hi, human.
if: always() && github.event_name == 'pull_request_target'

- name: Post comment (success)
uses: peter-evans/create-or-update-comment@v1
with:
token: ${{ secrets.BOT_TOKEN }}
issue-number: ${{ github.event.number }}
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace
body: |
*\*beep\* \*bop\**
Hi, human.
The **`${{ github.workflow }}`** workflow has **succeeded** :heavy_check_mark:
[**Click here**](${{ env.URL }}) to see your results.
env:
URL: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pull/${{ github.event.number }}/index.html
if: success() && github.event_name == 'pull_request_target'

- name: Post comment (failure)
uses: peter-evans/create-or-update-comment@v1
with:
token: ${{ secrets.BOT_TOKEN }}
issue-number: ${{ github.event.number }}
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace
body: |
*\*beep\* \*bop\**
Hi, human.
The **`${{ github.workflow }}`** workflow has **failed** :x:
[**Click here**](${{ env.URL }}) to see the build log.
env:
URL: https://github.com/${{ github.repository_owner }}/${{ github.event.repository.name }}/actions/runs/${{ github.run_id }}?check_suite_focus=true
if: failure() && github.event_name == 'pull_request_target'
64 changes: 64 additions & 0 deletions .github/workflows/clean-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# For more information about TARDIS pipelines, please refer to:
#
# https://tardis-sn.github.io/tardis/development/continuous_integration.html

name: clean-docs

on:

delete:
branches: # remove deleted branches
- '*'

pull_request_target: # remove closed or merged pull requests
branches:
- '*'
types:
- closed

env:
DEPLOY_BRANCH: gh-pages # deployed docs branch

jobs:
clean:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2

- name: Set folder to delete
run: |
if [[ $EVENT == delete ]]; then
echo "DEST_DIR=$EVENT_TYPE/$EVENT_REF" >> $GITHUB_ENV
elif [[ $EVENT == pull_request_target ]]; then
echo "DEST_DIR=pull/$PR" >> $GITHUB_ENV
else
echo "Unexpected event trigger $EVENT"
exit 1
fi
cat $GITHUB_ENV
env:
PR: ${{ github.event.number }}
EVENT: ${{ github.event_name }}
EVENT_REF: ${{ github.event.ref }}
EVENT_TYPE: ${{ github.event.ref_type }}

- name: Clean ${{ env.DEST_DIR }}
run: |
git fetch origin ${{ env.DEPLOY_BRANCH }}
git checkout ${{ env.DEPLOY_BRANCH }}
git config user.name "TARDIS Bot"
git config user.email [email protected]
if [[ -d $DEST_DIR ]]; then
git rm -rf $DEST_DIR
git commit -m "clean $DEST_DIR"
git push
else
echo "$DEST_DIR does not exist"
fi
45 changes: 0 additions & 45 deletions .github/workflows/documentation-build.yml

This file was deleted.

60 changes: 0 additions & 60 deletions .github/workflows/documentation-preview.yml

This file was deleted.

0 comments on commit 37a9769

Please sign in to comment.