|
| 1 | +name: Assemble Pages subdirectory |
| 2 | +description: >- |
| 3 | + Overlays a freshly built app onto the assembled-site storage branch, adds the |
| 4 | + SPA fallback (.nojekyll + 404.html), uploads the merged site as a Pages |
| 5 | + artifact, and persists the merged tree back to the storage branch. This lets |
| 6 | + each micro app deploy independently while GitHub Pages still serves a single |
| 7 | + site (see README "Hosting"). |
| 8 | +
|
| 9 | +inputs: |
| 10 | + source-dir: |
| 11 | + description: Freshly built directory to publish (e.g. dist/apps/portfolio/browser). |
| 12 | + required: true |
| 13 | + target-subdir: |
| 14 | + description: Subdirectory within the site. Empty for the site root (the shell). |
| 15 | + required: false |
| 16 | + default: '' |
| 17 | + storage-branch: |
| 18 | + description: Branch that stores the full assembled site between deploys. |
| 19 | + required: false |
| 20 | + default: pages-content |
| 21 | + preserve: |
| 22 | + description: >- |
| 23 | + Space-separated top-level entries to keep when publishing the site root, |
| 24 | + so independent micro-app subdirectories survive a shell redeploy. |
| 25 | + required: false |
| 26 | + default: 'portfolio storybook' |
| 27 | + github-token: |
| 28 | + description: Token used to read and update the storage branch. |
| 29 | + required: true |
| 30 | + |
| 31 | +runs: |
| 32 | + using: composite |
| 33 | + steps: |
| 34 | + - name: Assemble merged site π§© |
| 35 | + shell: bash |
| 36 | + env: |
| 37 | + SOURCE_DIR: ${{ inputs.source-dir }} |
| 38 | + TARGET_SUBDIR: ${{ inputs.target-subdir }} |
| 39 | + STORAGE_BRANCH: ${{ inputs.storage-branch }} |
| 40 | + PRESERVE: ${{ inputs.preserve }} |
| 41 | + GH_TOKEN: ${{ inputs.github-token }} |
| 42 | + run: | |
| 43 | + set -euo pipefail |
| 44 | +
|
| 45 | + if [ ! -d "$SOURCE_DIR" ]; then |
| 46 | + echo "::error::source-dir '$SOURCE_DIR' does not exist" >&2 |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | +
|
| 50 | + REPO_URL="https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" |
| 51 | +
|
| 52 | + rm -rf _site _storage |
| 53 | + mkdir -p _site |
| 54 | +
|
| 55 | + # Pull the current full site from the storage branch (if it exists). |
| 56 | + if git ls-remote --exit-code --heads "$REPO_URL" "$STORAGE_BRANCH" >/dev/null 2>&1; then |
| 57 | + git clone --quiet --depth 1 --branch "$STORAGE_BRANCH" "$REPO_URL" _storage |
| 58 | + ( cd _storage && tar -cf - --exclude=.git . ) | ( cd _site && tar -xf - ) |
| 59 | + rm -rf _storage |
| 60 | + fi |
| 61 | +
|
| 62 | + # Overlay the freshly built app into its slot. |
| 63 | + if [ -z "$TARGET_SUBDIR" ] || [ "$TARGET_SUBDIR" = "." ]; then |
| 64 | + # Site root (shell): replace root entries but keep sibling micro apps. |
| 65 | + keep=".git .nojekyll $PRESERVE" |
| 66 | + shopt -s dotglob nullglob |
| 67 | + for entry in _site/*; do |
| 68 | + name="$(basename "$entry")" |
| 69 | + case " $keep " in |
| 70 | + *" $name "*) continue ;; |
| 71 | + esac |
| 72 | + rm -rf "$entry" |
| 73 | + done |
| 74 | + shopt -u dotglob nullglob |
| 75 | + cp -R "$SOURCE_DIR"/. _site/ |
| 76 | + else |
| 77 | + rm -rf "_site/${TARGET_SUBDIR}" |
| 78 | + mkdir -p "_site/${TARGET_SUBDIR}" |
| 79 | + cp -R "$SOURCE_DIR"/. "_site/${TARGET_SUBDIR}/" |
| 80 | + fi |
| 81 | +
|
| 82 | + # SPA fallback + disable Jekyll processing. |
| 83 | + touch _site/.nojekyll |
| 84 | + if [ -f _site/index.html ]; then |
| 85 | + cp _site/index.html _site/404.html |
| 86 | + fi |
| 87 | +
|
| 88 | + echo "Assembled site tree:" |
| 89 | + find _site -maxdepth 2 -mindepth 1 | sort |
| 90 | +
|
| 91 | + - name: Upload Pages artifact π¦ |
| 92 | + uses: actions/upload-pages-artifact@v5 |
| 93 | + with: |
| 94 | + path: _site |
| 95 | + |
| 96 | + - name: Persist assembled site to storage branch πΎ |
| 97 | + shell: bash |
| 98 | + env: |
| 99 | + TARGET_SUBDIR: ${{ inputs.target-subdir }} |
| 100 | + STORAGE_BRANCH: ${{ inputs.storage-branch }} |
| 101 | + GH_TOKEN: ${{ inputs.github-token }} |
| 102 | + run: | |
| 103 | + set -euo pipefail |
| 104 | + REPO_URL="https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" |
| 105 | + SLOT="${TARGET_SUBDIR:-/ (shell)}" |
| 106 | +
|
| 107 | + cd _site |
| 108 | + rm -rf .git |
| 109 | + git init -q |
| 110 | + git add -A |
| 111 | + git -c user.name="github-actions[bot]" \ |
| 112 | + -c user.email="41898282+github-actions[bot]@users.noreply.github.com" \ |
| 113 | + commit -q -m "deploy: update ${SLOT} (${GITHUB_SHA})" || { |
| 114 | + echo "Nothing to persist."; exit 0; |
| 115 | + } |
| 116 | + git branch -M "$STORAGE_BRANCH" |
| 117 | + git push -f "$REPO_URL" "$STORAGE_BRANCH" |
0 commit comments