From 943ed05bf4323ec467f4c801e1ccbffe109815d7 Mon Sep 17 00:00:00 2001 From: Severin Date: Tue, 2 Apr 2024 20:00:05 +0200 Subject: [PATCH] Add rules for branches 1. User can set the "primary" branch to live at page root. This is `publish` by default. 2. User can set a list of aliases (symbolic links). The default setting maps `draft` to `main`, and `book` to `publish`. 3. User can set a list of branch to be deployed. By default: deploy all branches (indicated by '*'). NOTE: If user wants no branches deployed, or no aliases, they can set the corresponding variables to ' '. --- .github/workflows/deploy-book-ghpages.yml | 35 ++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deploy-book-ghpages.yml b/.github/workflows/deploy-book-ghpages.yml index a2a9209..7544985 100644 --- a/.github/workflows/deploy-book-ghpages.yml +++ b/.github/workflows/deploy-book-ghpages.yml @@ -16,6 +16,20 @@ on: # - some-subfolder/** workflow_dispatch: +# 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: + # `publish` as primary by default + PRIMARY_BRANCH: ${{ vars.PRIMARY_BRANCH != '' && vars.PRIMARY_BRANCH || 'publish' }} + # Space-separated list of alias-rules, e.g. 'draft:main alias:really-long-branch-name' + # By default, `draft` links to `main`. + # If no aliases are wanted, BRANCH_ALIASES may be set to ' ' (space). + BRANCH_ALIASES: ${{ vars.BRANCH_ALIASES != '' && vars.BRANCH_ALIASES || 'draft:main book:publish' }} + # 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 @@ -27,15 +41,21 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 + - id: set-branches - name: Collect all remote branch names for the matrix + name: Set branches run: | - branches=$(git branch -r | sed 's,\s*origin/,,g' | jq -Rn '[inputs]') + if [ "$BRANCHES_TO_DEPLOY" == '*' ]; then + branches=$(git branch -r | sed 's,\s*origin/,,g' | jq -Rn '[inputs]') + else + branches=$(echo $BRANCHES_TO_DEPLOY | tr ' ' '\n' | jq -Rn '[inputs]') + fi 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 @@ -115,9 +135,16 @@ jobs: fi ls -a final/ - - name: Load root page to redirect to `book/` + - name: Symlink branch aliases run: | - cp redirect.html final/index.html + 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 + fi + done - name: Upload final Pages artifact uses: actions/upload-pages-artifact@v3