Skip to content
35 changes: 33 additions & 2 deletions .github/workflows/deploy-book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
# 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:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# `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'
Expand All @@ -35,14 +36,39 @@ jobs:
- id: set-branches
name: Set branches
run: |
# 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')
else
branches=$(echo "$BRANCHES_TO_DEPLOY" | tr ' ' '\n' | grep -E '\S')
fi

# Display raw/clean branch name versions in Actions Summary
echo "### Branches deployed" >> $GITHUB_STEP_SUMMARY
## Table headers
echo "| Branch :tanabata_tree: | Link :link: |" >> $GITHUB_STEP_SUMMARY
echo "| --- | :--- |" >> $GITHUB_STEP_SUMMARY

## Fetch the GH Pages URL
# https://stackoverflow.com/a/76354104
url=$(gh api "repos/$GITHUB_REPOSITORY/pages" --jq '.html_url')

# Clean branch names
echo "$branches" > raw.txt
echo "$branches" | tr '/":<>|*?\/\\' '-' > clean.txt
paste -d ' ' raw.txt clean.txt | sed 's/ / -> /g'
paste -d ' ' raw.txt clean.txt |
while IFS=' ' read -r raw clean; do
echo "| $raw | <$url$clean> |" >> $GITHUB_STEP_SUMMARY
done

# Primary branch
echo >> $GITHUB_STEP_SUMMARY
echo "Primary branch ($PRIMARY_BRANCH) is at:" >> $GITHUB_STEP_SUMMARY
echo "- <$url>" >> $GITHUB_STEP_SUMMARY
echo "- and <$url$PRIMARY_BRANCH>" >> $GITHUB_STEP_SUMMARY

# Convert to a JSON list
branches=$(echo "$branches" | jq -Rn '[inputs]')
echo "branches=$(echo $branches)" >> $GITHUB_OUTPUT

Expand Down Expand Up @@ -135,14 +161,19 @@ jobs:

- name: Symlink branch aliases
run: |
echo "### Aliases" >> $GITHUB_STEP_SUMMARY
# Summary table headers
echo "| Alias :arrow_right: | Target :dart: |" >> $GITHUB_STEP_SUMMARY
echo "| :--- | :--- |" >> $GITHUB_STEP_SUMMARY

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 "$value" | tr '/":<>|*?\/\\' '-')
echo link "$clean_key" "->" "$clean_value"
echo "| $clean_key | $clean_value |" >> $GITHUB_STEP_SUMMARY
ln -s "$clean_value" final/"$key"
fi
done
Expand Down