brian-rose / ClimateLaboratoryBook

JupyterBook source for The Climate Laboratory
https://brian-rose.github.io/ClimateLaboratoryBook
Other
106 stars 44 forks source link

Merging a PR deletes ALL previews #74

Open brian-rose opened 2 years ago

brian-rose commented 2 years ago

Currently when you merge a PR it triggers a build-and-deploy action that overwrites the entire contents of the gh-pages branch, which effectively deletes the previews not just of the merged PR but also any other previews that are currently open.

We need to preserve previews for open PRs and explicitly delete them upon merge.

I think the way to do the first part is to set keep_files: true in the actions-gh-pages step.

The second part... I already have code in place for this but can't really test it until we fix the first part.

brian-rose commented 2 years ago

76 implements this but it doesn't work because the "delete preview" push to gh-pages gets cancelled before it does its thing.

brian-rose commented 2 years ago

Maybe the best approach is to set up a new action that runs after the rendered book is already pushed to gh-pages and simply checks out the gh-pages branch, deletes the appropriate _preview/[PR number] directory, and pushes the changes.

This action may be useful: https://github.com/marketplace/actions/add-commit

brian-rose commented 2 years ago

Or maybe a simpler one: https://github.com/stefanzweifel/git-auto-commit-action

brian-rose commented 2 years ago

Rather than running a delete action immediately upon merge, we could just run a daily or weekly "clean up" action that looks for anything in the _preview/ directory of the gh-pages branch, and removes anything not associated with a currently open PR.

For this, we would just need to be able to test if a given PR number is open.

brian-rose commented 2 years ago

Nevermind, I found a better / simpler solution.

In #77 We introduce a new delete_preview.yaml workflow that runs in the context

on:
  pull_request_target:
    types: closed

This means that it is called on merge OR on PR close, and either way it runs from the main branch so it has the credentials to push file changes to the gh_pages branch.

From there, it's just a matter of

Then the gh-pages automation takes over.

If you watch the action reports, it looks like this action gets skipped when the main deploy-book.yaml action pushes to gh-pages. But that's just the part where GitHub takes the files in the gh-pages repo and deploys them to the pages website. The important thing is that we get the file changes pushed into the repo.

I tested this all out, and found that:

So basically all the functionality is there!

Some more candy could be added, like:

But I don't think any of that is necessary right now.

brian-rose commented 1 year ago

I should finish migrating this repo to use the reusable Pythia Cookbook actions (following the Cookbook Template). Will leave this issue open until that work is done.