Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run on pull requests #73

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6d02a19
Run on pull requests
Tom-van-Woudenberg Jan 23, 2025
960b7e9
Includes 'branches' from pull requests
Tom-van-Woudenberg Jan 23, 2025
6af6398
only include pull requests from forks
Tom-van-Woudenberg Jan 23, 2025
a5bcfc1
Correct reference format for checkout
Tom-van-Woudenberg Jan 23, 2025
94d22e8
debug
Tom-van-Woudenberg Jan 23, 2025
3bdd4f7
Revert "debug"
Tom-van-Woudenberg Jan 23, 2025
39ac600
clean branch name for PR
Tom-van-Woudenberg Jan 23, 2025
525f212
Clean name for summary too
Tom-van-Woudenberg Jan 23, 2025
73fe456
add repository variable pull requests to deploy
Tom-van-Woudenberg Jan 23, 2025
a38a855
Allow for no pull requests
Tom-van-Woudenberg Jan 23, 2025
eb01e60
debug
Tom-van-Woudenberg Jan 23, 2025
2b76ef1
fix issue of double hits when syncing pull requests
Tom-van-Woudenberg Jan 23, 2025
c9c94ac
Revert "fix issue of double hits when syncing pull requests"
Tom-van-Woudenberg Jan 23, 2025
4fe2eda
Trimming leading/trailing spaces and remove duplicates
Tom-van-Woudenberg Jan 23, 2025
9c6192c
more debug
Tom-van-Woudenberg Jan 23, 2025
935b7a9
filter PR to emerge in branch step
Tom-van-Woudenberg Jan 23, 2025
81faf4a
Revert "Trimming leading/trailing spaces and remove duplicates"
Tom-van-Woudenberg Jan 23, 2025
d235c56
Revert "debug"
Tom-van-Woudenberg Jan 23, 2025
9e65fab
Revert "more debug"
Tom-van-Woudenberg Jan 23, 2025
3908cdd
Do not publish PR branch
Tom-van-Woudenberg Jan 23, 2025
73ebcd7
typo
Tom-van-Woudenberg Jan 23, 2025
9bbe7fd
now also do not update github pages
Tom-van-Woudenberg Jan 23, 2025
58a112c
clarified
Tom-van-Woudenberg Jan 23, 2025
89e3cec
debug
Tom-van-Woudenberg Jan 23, 2025
7026ab7
clarified
Tom-van-Woudenberg Jan 23, 2025
ef3269e
fix?
Tom-van-Woudenberg Jan 23, 2025
e188659
Revert "debug"
Tom-van-Woudenberg Jan 23, 2025
f037df1
better if statement
Tom-van-Woudenberg Jan 23, 2025
1688081
clarified readme
Tom-van-Woudenberg Jan 23, 2025
da3c7e2
NOT
Tom-van-Woudenberg Jan 23, 2025
457a9a2
or --> and
Tom-van-Woudenberg Jan 23, 2025
f3a5374
cleaning
Tom-van-Woudenberg Jan 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/call-deploy-book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ on:
- book/**
- requirements.txt
- .github/workflows/call-deploy-book.yml
pull_request:
branches:
- '**'
paths:
- book/**
- requirements.txt
- .github/workflows/call-deploy-book.yml
workflow_dispatch:

jobs:
Expand Down
57 changes: 48 additions & 9 deletions .github/workflows/deploy-book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ env:
# Space-separated list of branch names, e.g. 'main second third'.
# By default, deploy all branches. This is indicated by '*'.
BRANCHES_TO_DEPLOY: ${{ vars.BRANCHES_TO_DEPLOY != '' && vars.BRANCHES_TO_DEPLOY || '*' }}

# Which pull requests to release
# Space-separated list of pull request numbers, e.g. '1 2 3'.
# By default, deploy all pull requests. This is indicated by '*'.
# If building of pull request is unwanted, PULL_REQUESTS_TO_DEPLOY may be set to ' ' (space).
PULL_REQUESTS_TO_DEPLOY: ${{ vars.PULL_REQUESTS_TO_DEPLOY != '' && vars.PULL_REQUESTS_TO_DEPLOY || '*' }}

# Preprocessing is described here: (as described here: https://teachbooks.github.io/TeachBooks/cli/cli.html#teachbooks-build)
# Space-separated list of branch names, e.g. 'main second third'.
Expand Down Expand Up @@ -73,11 +79,22 @@ jobs:
# If all (*), query all remote branches
# If not, read from env var
if [ "$BRANCHES_TO_DEPLOY" == '*' ]; then
branches=$(git branch -r | sed 's,\s*origin/,,g' | grep -v '\->')
branches=$(git branch -r | sed 's,\s*origin/,,g' | grep -v '\->' | grep -v 'pull/')
else
branches=$(echo "$BRANCHES_TO_DEPLOY" | tr ' ' '\n' | grep -E '\S')
fi

# Fetch all open pull requests from forks and include their branches
if [ "$PULL_REQUESTS_TO_DEPLOY" == '*' ]; then
pr_branches=$(gh pr list --state open --json number,headRefName,isCrossRepository --jq '.[] | select(.isCrossRepository) | "refs/pull/" + (.number|tostring) + "/merge"')
elif [ "$PULL_REQUESTS_TO_DEPLOY" == ' ' ]; then
pr_branches=""
else
pr_branches=$(echo "$PULL_REQUESTS_TO_DEPLOY" | tr ' ' '\n' | xargs -I {} gh pr view {} --json number,headRefName,isCrossRepository --jq 'select(.isCrossRepository) | "refs/pull/" + (.number|tostring) + "/merge"')
fi

branches=$(echo -e "$branches\n$pr_branches" | grep -E '\S')

# Record branch names (one per line) for alias validation
echo "$branches" | tr ' ' '\n' > branches.txt

Expand Down Expand Up @@ -248,7 +265,7 @@ jobs:
- name: Record build success
if: success()
run: |
echo ":white_check_mark: \\\`Released\\\`" >> buildstatus.txt
echo ":white_check_mark: \\\`Build successful\\\`" >> buildstatus.txt

- name: Make summary of errors, if any
# run even in case of failure
Expand All @@ -268,7 +285,12 @@ jobs:
- name: Clean branch name of disallowed characters
if: always()
run: |
echo "MATRIX_BRANCH_NAME_CLEAN=$(echo ${{ matrix.branch }} | tr '/":<>|*?\/\\' '-')" >> $GITHUB_ENV
if [[ "${{ matrix.branch }}" =~ refs/pull/([0-9]+)/merge ]]; then
CLEAN_BRANCH_NAME="PR-${BASH_REMATCH[1]}"
else
CLEAN_BRANCH_NAME=$(echo "${{ matrix.branch }}" | tr '/":<>|*?\\' '-')
fi
echo "MATRIX_BRANCH_NAME_CLEAN=${CLEAN_BRANCH_NAME}" >> $GITHUB_ENV

- name: Upload summary as an artifact
if: always()
Expand Down Expand Up @@ -362,10 +384,23 @@ jobs:
# https://stackoverflow.com/a/76354104
url=$(gh api "repos/$GITHUB_REPOSITORY/pages" --jq '.html_url')

# Clean branch names
cat raw.txt | tr '/":<>|*?\/\\' '-' > clean.txt
cat clean.txt | xargs -I{} cat _buildstatus/_buildstatus-{}/buildstatus.txt > status.txt
# Clean normal branch names
grep -v '^refs/pull/' raw.txt | tr '/":<>|*?\\' '-' > clean.txt

# Include cleaned PR branches
grep '^refs/pull/' raw.txt | sed 's|refs/pull/\([0-9]\+\)/merge|PR-\1|' >> clean.txt

# Generate status.txt
if [[ "${{ github.event_name }}" != 'push' && "${{ github.event_name }}" != 'workflow_dispatch' ]]; then
cat clean.txt | xargs -I{} sh -c 'echo ":no_entry_sign: \\\`Not updated\\\` $(cat _buildstatus/_buildstatus-{}/buildstatus.txt)" >> status.txt'
else
cat clean.txt | xargs -I{} cat _buildstatus/_buildstatus-{}/buildstatus.txt > status.txt
fi

# Combine raw, clean, and status into pasted.txt
paste -d ';' raw.txt clean.txt status.txt > pasted.txt

# Generate summary.md
cat pasted.txt |
while IFS=';' read -r raw clean status; do
echo "| $raw | <$url$clean> | $status |" >> summary.md
Expand All @@ -376,9 +411,11 @@ jobs:
{
echo
echo "#### Legend for build status"
echo ":white_check_mark: \\\`Released\\\` - build success, new version released."
echo ":red_circle: \\\`Build failed [1]\\\` - build failure, previous version of the book reused."
echo ":o: \\\`Build failed [2]\\\` - build failure, no previous version reused."
echo ":white_check_mark: \\\`Build successful\\\` - build is successful, new version used."
echo ":red_circle: \\\`Build failed [1]\\\` - build has failed, previous version of the book reused."
echo ":o: \\\`Build failed [2]\\\` - build has failed, no previous version reused."
echo ":no_entry_sign: \\\`Not updated\\\` - deployment is skipped because it's triggered from a forked repository."


echo
echo '#### Primary book at root'
Expand Down Expand Up @@ -473,11 +510,13 @@ jobs:
echo "$HTML_TEMPLATE_404_REDIRECT" | envsubst | tee final/404.html

- name: Upload final Pages artifact
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
uses: actions/upload-pages-artifact@v3
with:
path: "final/"

- name: Deploy to GitHub Pages
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
id: deployment
uses: actions/deploy-pages@v4

Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The workflow has the following features:
- Ability to release both private (GitHub Pro, GitHub Team, GitHub Enterprise Cloud, or GitHub Enterprise Server required) and public (GitHub Free is enough) repositories.
GitHub Teams is free for teachers as described in the [GitHub documentation](https://docs.github.com/en/education/explore-the-benefits-of-teaching-and-learning-with-github-education/github-education-for-teachers/about-github-education-for-teachers#github-education-features-for-teachers).
If you have an organization for your TeachBook on GitHub, link your GitHub team rights to your organization as described on the [GitHub website](https://github.com/team#organizations).
- Releasing all or a selection of branches, allowing to build a draft version of the TeachBook online which reduces the need for local builds of the book
- Releasing all or a selection of branches and pull requests, allowing to build a draft version of the TeachBook online which reduces the need for local builds of the book
- Provides a summary describing where the TeachBook is released, errors in the build process per branch and how the release step is configured
- Caching of already built books so that it can be partially reused when another branch is released or the next build contains critical errors
- Caching of python environment to speed up the workflow
Expand Down Expand Up @@ -47,6 +47,10 @@ You can adapt the behaviour by setting repository variables as explained [here](
- `BRANCHES_TO_DEPLOY` which is set to `*` (all branches) whenever it's not defined in the repository variables.
- This defines the branches to deploy.
- It should be a space-separated list of branch names, e.g. 'main second third'.
- `PULL_REQUESTS_TO_DEPLOY` which is set to `*` (all branches) whenver it's not defined in the repository ariables.
- This defines the pull requests to deploy
- It should be a space-seperated list of pull request numbers, e.g. '1 2 3'
- If building of pull request is unwanted, PULL_REQUESTS_TO_DEPLOY may be set to ' ' (space).
- `BRANCHES_TO_PREPROCESS` which is to to ` ` (just a space = no branch) whenever it's not defined in the repository variables
- This defines the branches to preprocess with the [`TeachBooks` package](https://teachbooks.io/TeachBooks/cli/cli.html#cmdoption-teachbooks-build), which removed book-pages and config lines defined with `# START REMOVE FROM RELEASE` and `# END REMOVE FROM RELEASE`
- It should be a space-separated list of branch names, e.g. 'main second third'.
Expand All @@ -70,24 +74,26 @@ Here's an example for a summary for the template book:
> ### Branches deployed
> | Branch 🎋 | Link 🔗 | Build status ☑️ |
> | :--- | :--- | :--- |
> | main | [https://teachbooks.github.io/template/main](https://teachbooks.github.io/template/main) | ✅ `Released` |
> | main | [https://teachbooks.github.io/template/main](https://teachbooks.github.io/template/main) | ✅ `Build successful` |
> | version2 | [https://teachbooks.github.io/template/version2](https://teachbooks.github.io/template/version2) | 🔴 `Build failed [1]` |
> | version3 | [https://teachbooks.github.io/template/version3](https://teachbooks.github.io/template/version3) | ⭕ `Build failed [2]` |
>
> #### Legend for build status
> ✅ `Released` - build success, new version released.
> ✅ `Build successful` - build success, new version used.
>
> 🔴 `Build failed [1]` - build failure, previous version of the book reused.
>
> ⭕ `Build failed [2]` - build failure, no previous version reused.
>
> 🚫 `Not updated` - deployment skipped because triggered from forked repository.
>
> #### Primary book at root
> The book at the website root <https://teachbooks.github.io/template/> redirects to the primary branch `main` (status: ✅ `Released`).
> The book at the website root <https://teachbooks.github.io/template/> redirects to the primary branch `main` (status: ✅ `Build successful`).
>
> ### Aliases
> | Alias ➡️ | Target 🎯 | Link 🔗 | Build status ☑️ |
> | :--- | :--- | :--- | :---- |
> | draft | main | [https://teachbooks.github.io/template/draft](https://teachbooks.github.io/template/draft) | ✅ `Released` |
> | draft | main | [https://teachbooks.github.io/template/draft](https://teachbooks.github.io/template/draft) | ✅ `Build successful` |
>
> ### Preview of build errors & warnings
> For more details please see the corresponding `build-books` jobs in the left pane.
Expand Down