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 ' '.
This commit is contained in:
Severin 2024-04-02 20:00:05 +02:00
parent 01d0f3943f
commit 943ed05bf4

View file

@ -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