Pre-process example notebooks #645
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Pre-process example notebooks | |
on: | |
workflow_dispatch: | |
inputs: | |
pr_number: | |
description: "Update the cache for PR#: (leave blank if not a PR)" | |
required: false | |
default: "" | |
type: string | |
schedule: | |
- cron: 0 0 * * * # 1/day at midnight UTC | |
push: | |
branches: | |
- main | |
concurrency: | |
group: ${{ github.workflow }}-${{ inputs.pr_number || github.ref || github.run_id }} | |
cancel-in-progress: true | |
jobs: | |
preprocess: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Choose deployment branch | |
shell: bash -l {0} | |
run: | | |
if [[ ${{ github.event_name }} != "workflow_dispatch" ]]; then | |
echo "DEPLOY_BRANCH=_cookbook_data_$GITHUB_REF_NAME" >> "$GITHUB_ENV" | |
elif [[ "${{ inputs.pr_number }}" == "" ]]; then | |
echo "DEPLOY_BRANCH=_cookbook_data_$GITHUB_REF_NAME" >> "$GITHUB_ENV" | |
else | |
echo "DEPLOY_BRANCH=_cookbook_data_PR${{ inputs.pr_number }}" >> "$GITHUB_ENV" | |
fi | |
- name: Report dispatch to PR | |
if: github.event_name == 'workflow_dispatch' && inputs.pr_number != '' | |
uses: thollander/actions-comment-pull-request@v2 | |
with: | |
pr_number: ${{ inputs.pr_number }} | |
message: > | |
A workflow has been dispatched to regenerate the cookbook cache for this PR. | |
- Run ID: [${{ github.run_id }}] | |
- Triggering actor: ${{ github.triggering_actor }} | |
- Target branch: ${{ github.ref_name }} | |
- Deployment branch: ${{ env.DEPLOY_BRANCH }} | |
[${{ github.run_id }}]: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
- name: Create check | |
if: github.event_name == 'workflow_dispatch' && inputs.pr_number != '' | |
id: create-check | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const created_run = await github.request( | |
'POST /repos/{owner}/{repo}/check-runs', | |
{ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
name: '${{ github.job }}', | |
head_sha: '${{ github.sha }}', | |
details_url: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}', | |
status: 'in_progress', | |
started_at: new Date().toISOString(), | |
output: { | |
title: 'Regenerate example notebook cache', | |
summary: 'Results and progress: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}', | |
text: '' | |
}, | |
headers: { | |
'X-GitHub-Api-Version': '2022-11-28' | |
} | |
} | |
) | |
return created_run.data.id | |
- uses: actions/checkout@v3 | |
- name: Install Conda environment | |
uses: mamba-org/setup-micromamba@v1 | |
with: | |
environment-file: devtools/conda-envs/examples_env.yml | |
# default - will pull down 2.0 which we don't want! | |
# micromamba-version: latest | |
# pin to latest 1.x release | |
micromamba-version: "1.5.10-0" | |
- name: Pre-process and execute notebooks | |
shell: bash -l {0} | |
run: | | |
set -e | |
python source/_ext/proc_examples.py --prefix=deploy/ --cache-branch=${DEPLOY_BRANCH} --log-failures=notebooks_log.json | |
- name: Read notebooks log | |
if: always() | |
shell: bash -l {0} | |
run: echo "NOTEBOOKS_LOG=$(cat 'notebooks_log.json')" >> "$GITHUB_ENV" | |
- name: Deploy cache | |
shell: bash -l {0} | |
run: | | |
cd deploy | |
git config --global user.name github-actions | |
git config --global user.email [email protected] | |
git config --global init.defaultBranch main | |
# Create an empty repository so we don't have to fetch (guaranteeing atomicity) | |
git init . | |
git remote add origin "https://github-actions:${{ github.token }}@github.com/${{ github.repository }}.git" | |
# Commit the deployment and force push to the deploy branch | |
git add . | |
git commit -m "Deploy pre-processed notebook cache" | |
git push --force origin HEAD:$DEPLOY_BRANCH | |
- name: Trigger RTD build | |
# RTD doesn't offer a way to manually trigger builds in PRs, so we'll just do this for other builds | |
if: github.event_name == 'schedule' || github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && inputs.pr_number == '') | |
shell: bash -l {0} | |
run: | | |
curl -X POST -d "branches=$GITHUB_REF_NAME" -d "token=${{ secrets.RTD_WEBHOOK_TOKEN }}" https://readthedocs.org/api/v2/webhook/openff-docs/243876/ | |
- name: Report status to PR | |
id: reportStatusToPr | |
if: always() && github.event_name == 'workflow_dispatch' && inputs.pr_number != '' | |
uses: thollander/actions-comment-pull-request@v2 | |
with: | |
pr_number: ${{ inputs.pr_number }} | |
message: > | |
A workflow dispatched to regenerate the cookbook cache for this PR has just finished. | |
- Run ID: [${{ github.run_id }}] | |
- Triggering actor: ${{ github.triggering_actor }} | |
- Target branch: ${{ github.ref_name }} | |
- Deployment branch: ${{ env.DEPLOY_BRANCH }} | |
- Job status: **${{ job.status }}** | |
- Notebooks status: ${{fromJSON(env.NOTEBOOKS_LOG).n_successful}} / ${{fromJSON(env.NOTEBOOKS_LOG).n_total}} notebooks successfully executed (${{fromJSON(env.NOTEBOOKS_LOG).n_ignored}} failures ignored) | |
${{(fromJSON(env.NOTEBOOKS_LOG).failed || fromJSON(env.NOTEBOOKS_LOG).ignored) && '- Failing notebooks: | |
- ' || ''}}${{join(fromJSON(env.NOTEBOOKS_LOG).failed, ' | |
- ')}}${{fromJSON(env.NOTEBOOKS_LOG).ignored && ' | |
- [ignored] ' || ''}}${{join(fromJSON(env.NOTEBOOKS_LOG).ignored, ' | |
- [ignored] ')}} | |
Changes will only be visible in the ReadTheDocs | |
preview after it has been [rebuilt]. | |
[${{ github.run_id }}]: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
[rebuilt]: https://readthedocs.org/projects/openff-docs/builds/ | |
- name: Report status to PR on templating failure | |
if: always() && steps.reportStatusToPr.outcome == 'failure' | |
uses: thollander/actions-comment-pull-request@v2 | |
with: | |
pr_number: ${{ inputs.pr_number }} | |
message: > | |
A workflow dispatched to regenerate the cookbook cache for this PR has just finished. | |
- Run ID: [${{ github.run_id }}] | |
- Triggering actor: ${{ github.triggering_actor }} | |
- Target branch: ${{ github.ref_name }} | |
- Deployment branch: ${{ env.DEPLOY_BRANCH }} | |
- Job status: **${{ job.status }}** | |
- Notebooks status: N/A | |
Changes will only be visible in the ReadTheDocs | |
preview after it has been [rebuilt]. | |
[${{ github.run_id }}]: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
[rebuilt]: https://readthedocs.org/projects/openff-docs/builds/ | |
- name: Update check | |
if: always() && github.event_name == 'workflow_dispatch' && inputs.pr_number != '' | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
await github.request( | |
'PATCH /repos/{owner}/{repo}/check-runs/{check_run_id}', | |
{ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
check_run_id: ${{ steps.create-check.outputs.result }}, | |
status: 'completed', | |
conclusion: '${{ job.status }}', | |
completed_at: new Date().toISOString(), | |
headers: { | |
'X-GitHub-Api-Version': '2022-11-28' | |
} | |
} | |
) |