From b46a30b33a6fed2bbd60188aeb28cc12d8474f5b Mon Sep 17 00:00:00 2001 From: Severin Date: Sun, 14 Apr 2024 20:26:26 +0200 Subject: [PATCH 1/5] Use reusable workflow --- .github/workflows/call-deploy-book.yml | 23 +++++++++++++++++++++++ .github/workflows/deploy-book-ghpages.yml | 17 ++++++----------- 2 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/call-deploy-book.yml diff --git a/.github/workflows/call-deploy-book.yml b/.github/workflows/call-deploy-book.yml new file mode 100644 index 0000000..97903c7 --- /dev/null +++ b/.github/workflows/call-deploy-book.yml @@ -0,0 +1,23 @@ +name: call-deploy-book + +on: + push: + branches: + - '**' + # If your git repository has the Jupyter Book within some-subfolder next to + # unrelated files, you can make this run only if a file within that specific + # folder has been modified. + # + # paths: + # - some-subfolder/** + workflow_dispatch: + +jobs: + call-workflow: + uses: TeachBooks/template/.github/workflows/deploy-book-ghpages.yml@main + secrets: inherit + permissions: + contents: read + pages: write + id-token: write + diff --git a/.github/workflows/deploy-book-ghpages.yml b/.github/workflows/deploy-book-ghpages.yml index b6b96bf..682ab42 100644 --- a/.github/workflows/deploy-book-ghpages.yml +++ b/.github/workflows/deploy-book-ghpages.yml @@ -3,18 +3,13 @@ name: deploy-book # Based on: # https://jupyterbook.org/en/stable/publish/gh-pages.html -# Run on a push to any branch, then deploy books from all branches. +# NOTE to template users: +# You should not need to edit this file. This is not the workflow file that is being run on every push. +# Instead, on every push workflow `call-deploy-book` calls the `deploy-book` workflow at the original template repository: +# https://github.com/TeachBooks/template + on: - push: - branches: - - '**' - # If your git repository has the Jupyter Book within some-subfolder next to - # unrelated files, you can make this run only if a file within that specific - # folder has been modified. - # - # paths: - # - some-subfolder/** - workflow_dispatch: + workflow_call: # Inherit configuration variables in the environment, or assign default values. # Configuration variables may be set as explained here: From 8103e5145177da0cfca8a6f5ed14aa081c7f941e Mon Sep 17 00:00:00 2001 From: Severin Date: Sun, 14 Apr 2024 20:27:38 +0200 Subject: [PATCH 2/5] Log cleaning branch names --- .github/workflows/deploy-book-ghpages.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy-book-ghpages.yml b/.github/workflows/deploy-book-ghpages.yml index 682ab42..2c71465 100644 --- a/.github/workflows/deploy-book-ghpages.yml +++ b/.github/workflows/deploy-book-ghpages.yml @@ -41,10 +41,14 @@ jobs: name: Set branches run: | if [ "$BRANCHES_TO_DEPLOY" == '*' ]; then - branches=$(git branch -r | sed 's,\s*origin/,,g' | jq -Rn '[inputs]') + branches=$(git branch -r | sed 's,\s*origin/,,g') else - branches=$(echo "$BRANCHES_TO_DEPLOY" | tr ' ' '\n' | grep -E '\S' | jq -Rn '[inputs]') + branches=$(echo "$BRANCHES_TO_DEPLOY" | tr ' ' '\n' | grep -E '\S') fi + echo "$branches" > raw.txt + echo "$branches" | tr '/":<>|*?\/\\' '-' > clean.txt + paste -d ' ' raw.txt clean.txt | sed 's/ / -> /g' + branches=$(echo "$branches" | jq -Rn '[inputs]') echo "branches=$(echo $branches)" >> $GITHUB_OUTPUT build-books: From dc351c02efafbb54a9e33e73bf4e4542c4ed3eeb Mon Sep 17 00:00:00 2001 From: Severin Date: Sun, 14 Apr 2024 20:28:12 +0200 Subject: [PATCH 3/5] Clean branch names in symlinking --- .github/workflows/deploy-book-ghpages.yml | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/.github/workflows/deploy-book-ghpages.yml b/.github/workflows/deploy-book-ghpages.yml index 2c71465..74d51fe 100644 --- a/.github/workflows/deploy-book-ghpages.yml +++ b/.github/workflows/deploy-book-ghpages.yml @@ -133,19 +133,22 @@ jobs: run: | GLOBIGNORE=".:.." # -i leads to a prompt in case of conflict, => a timeout - if [ -d final/$PRIMARY_BRANCH ]; then - cp -irv final/$PRIMARY_BRANCH/* final/ + if [ -d final/"$PRIMARY_BRANCH" ]; then + cp -irv final/"$PRIMARY_BRANCH"/* final/ fi ls -a final/ - name: Symlink branch aliases run: | - echo $BRANCH_ALIASES | tr ' ' '\n' | grep -E '\S' | + echo "$BRANCH_ALIASES" | tr ' ' '\n' | grep -E '\S' | while IFS=':' read -r key value; do # If the target branch is to be deployed, make symlink to it. - if [ "$BRANCHES_TO_DEPLOY" == "*" ] || echo $BRANCHES_TO_DEPLOY | tr ' ' '\n' | grep "^$value$"; then - echo link $key "->" $value - ln -s $value final/$key + if [ "$BRANCHES_TO_DEPLOY" == "*" ] || echo "$BRANCHES_TO_DEPLOY" | tr ' ' '\n' | grep "^$value$"; then + # Clean branch names of special characters + clean_key=$(echo "$key" | tr '/":<>|*?\/\\' '-') + clean_value=$(echo "$key" | tr '/":<>|*?\/\\' '-') + echo link $clean_key "->" $clean_value + ln -s "$clean_value" final/"$clean_key" fi done @@ -163,4 +166,3 @@ permissions: contents: read pages: write id-token: write - From 40782d87e767ffb5595ad14e9c6dc8f2319b580c Mon Sep 17 00:00:00 2001 From: Severin Date: Mon, 15 Apr 2024 13:21:50 +0200 Subject: [PATCH 4/5] Add note on paths --- .github/workflows/call-deploy-book.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/call-deploy-book.yml b/.github/workflows/call-deploy-book.yml index 97903c7..f2448d2 100644 --- a/.github/workflows/call-deploy-book.yml +++ b/.github/workflows/call-deploy-book.yml @@ -6,10 +6,12 @@ on: - '**' # If your git repository has the Jupyter Book within some-subfolder next to # unrelated files, you can make this run only if a file within that specific - # folder has been modified. + # folder (or the workflow file itself) has been modified. # # paths: # - some-subfolder/** + # - .github/workflows/call-deploy-book.yml + # workflow_dispatch: jobs: From 6a2f75b1b9320420ef08b796e0cc48caaf3c2bca Mon Sep 17 00:00:00 2001 From: Severin Date: Mon, 15 Apr 2024 13:22:59 +0200 Subject: [PATCH 5/5] Use workflow in a different repo --- .github/workflows/call-deploy-book.yml | 2 +- .github/workflows/deploy-book-ghpages.yml | 168 ---------------------- 2 files changed, 1 insertion(+), 169 deletions(-) delete mode 100644 .github/workflows/deploy-book-ghpages.yml diff --git a/.github/workflows/call-deploy-book.yml b/.github/workflows/call-deploy-book.yml index f2448d2..2c924b5 100644 --- a/.github/workflows/call-deploy-book.yml +++ b/.github/workflows/call-deploy-book.yml @@ -16,7 +16,7 @@ on: jobs: call-workflow: - uses: TeachBooks/template/.github/workflows/deploy-book-ghpages.yml@main + uses: TeachBooks/deploy-book-workflow/.github/workflows/deploy-book.yml@main secrets: inherit permissions: contents: read diff --git a/.github/workflows/deploy-book-ghpages.yml b/.github/workflows/deploy-book-ghpages.yml deleted file mode 100644 index 74d51fe..0000000 --- a/.github/workflows/deploy-book-ghpages.yml +++ /dev/null @@ -1,168 +0,0 @@ -name: deploy-book - -# Based on: -# https://jupyterbook.org/en/stable/publish/gh-pages.html - -# NOTE to template users: -# You should not need to edit this file. This is not the workflow file that is being run on every push. -# Instead, on every push workflow `call-deploy-book` calls the `deploy-book` workflow at the original template repository: -# https://github.com/TeachBooks/template - -on: - workflow_call: - -# Inherit configuration variables in the environment, or assign default values. -# Configuration variables may be set as explained here: -# https://docs.github.com/en/actions/learn-github-actions/variables#creating-configuration-variables-for-a-repository -env: - # `main` as primary by default, advised to make it `published` to start using draft-publish-workflow - PRIMARY_BRANCH: ${{ vars.PRIMARY_BRANCH != '' && vars.PRIMARY_BRANCH || 'main' }} - # Space-separated list of alias-rules, e.g. 'draft:main alias:really-long-branch-name' - # By default, `draft` links to `main`. Advised to link `book` to `publish` - # If no aliases are wanted, BRANCH_ALIASES may be set to ' ' (space). - BRANCH_ALIASES: ${{ vars.BRANCH_ALIASES != '' && vars.BRANCH_ALIASES || 'draft:main' }} - # 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 || '*' }} - -jobs: - get-branches: - runs-on: ubuntu-latest - outputs: - branches: ${{ steps.set-branches.outputs.branches }} - permissions: - contents: read - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - id: set-branches - name: Set branches - run: | - if [ "$BRANCHES_TO_DEPLOY" == '*' ]; then - branches=$(git branch -r | sed 's,\s*origin/,,g') - else - branches=$(echo "$BRANCHES_TO_DEPLOY" | tr ' ' '\n' | grep -E '\S') - fi - echo "$branches" > raw.txt - echo "$branches" | tr '/":<>|*?\/\\' '-' > clean.txt - paste -d ' ' raw.txt clean.txt | sed 's/ / -> /g' - branches=$(echo "$branches" | jq -Rn '[inputs]') - echo "branches=$(echo $branches)" >> $GITHUB_OUTPUT - - build-books: - runs-on: ubuntu-latest - needs: get-branches - if: ${{ needs.get-branches.outputs.branches != '[]' }} - permissions: - pages: write - id-token: write - strategy: - # If one branch fails, we may still want to deploy the other - fail-fast: false - matrix: - branch: ${{ fromJson(needs.get-branches.outputs.branches) }} - steps: - - name: Checkout to branch - uses: actions/checkout@v4 - with: - ref: ${{ matrix.branch }} - - - name: Cache page build - id: cache-html - uses: actions/cache@v4 - with: - path: "book/_build/html" - key: html-build-${{ hashFiles('book/**', 'figures/**', 'requirements.txt') }} - - - if: ${{ steps.cache-html.outputs.cache-hit != 'true' }} - name: Set up Python 3.11 - uses: actions/setup-python@v5 - with: - python-version: 3.11 - cache: 'pip' - - - if: ${{ steps.cache-html.outputs.cache-hit != 'true' }} - name: Install dependencies - run: | - pip install -r requirements.txt - - - if: ${{ steps.cache-html.outputs.cache-hit != 'true' }} - name: Preprocess & build the book from branch - run: | - echo $PATH - if [ ${{matrix.branch}} == $PRIMARY_BRANCH ]; then - teachbooks build --publish book/ - else - teachbooks build book/ - fi - - - - name: Clean branch name of disallowed characters - run: | - echo "MATRIX_BRANCH_NAME_CLEAN=$(echo ${{ matrix.branch }} | tr '/":<>|*?\/\\' '-')" >> $GITHUB_ENV - - - name: Upload the built book HTML as an artifact - uses: actions/upload-artifact@v4 - with: - name: ${{ env.MATRIX_BRANCH_NAME_CLEAN }} - path: "book/_build/html" - - deploy-books: - # Run after build-books, even if it failed - if: always() - needs: build-books - runs-on: ubuntu-latest - permissions: - pages: write - id-token: write - steps: - - uses: actions/checkout@v4 - - - run: | - mkdir final/ - - - name: Download all book artifacts - uses: actions/download-artifact@v4 - with: - path: "final/" - - - name: Copy primary book to root, fail if names conflict - timeout-minutes: 1 - run: | - GLOBIGNORE=".:.." - # -i leads to a prompt in case of conflict, => a timeout - if [ -d final/"$PRIMARY_BRANCH" ]; then - cp -irv final/"$PRIMARY_BRANCH"/* final/ - fi - ls -a final/ - - - name: Symlink branch aliases - run: | - echo "$BRANCH_ALIASES" | tr ' ' '\n' | grep -E '\S' | - while IFS=':' read -r key value; do - # If the target branch is to be deployed, make symlink to it. - if [ "$BRANCHES_TO_DEPLOY" == "*" ] || echo "$BRANCHES_TO_DEPLOY" | tr ' ' '\n' | grep "^$value$"; then - # Clean branch names of special characters - clean_key=$(echo "$key" | tr '/":<>|*?\/\\' '-') - clean_value=$(echo "$key" | tr '/":<>|*?\/\\' '-') - echo link $clean_key "->" $clean_value - ln -s "$clean_value" final/"$clean_key" - fi - done - - - name: Upload final Pages artifact - uses: actions/upload-pages-artifact@v3 - with: - path: "final/" - - - name: Deploy to GitHub Pages - id: deployment - uses: actions/deploy-pages@v4 - - -permissions: - contents: read - pages: write - id-token: write