From 698e01ea1d0e2b59d7571a151405e527dd97540c Mon Sep 17 00:00:00 2001 From: mdvanes <4253562+mdvanes@users.noreply.github.com> Date: Fri, 3 Jul 2026 14:06:54 +0200 Subject: [PATCH] chore: refactor deploy script --- .github/actions/deploy-subdir/action.yml | 67 +------------------ .../actions/deploy-subdir/assemble-site.sh | 48 +++++++++++++ .github/actions/deploy-subdir/persist-site.sh | 17 +++++ 3 files changed, 67 insertions(+), 65 deletions(-) create mode 100755 .github/actions/deploy-subdir/assemble-site.sh create mode 100755 .github/actions/deploy-subdir/persist-site.sh diff --git a/.github/actions/deploy-subdir/action.yml b/.github/actions/deploy-subdir/action.yml index aa10ba3..86cbaa9 100644 --- a/.github/actions/deploy-subdir/action.yml +++ b/.github/actions/deploy-subdir/action.yml @@ -38,55 +38,7 @@ runs: TARGET_SUBDIR: ${{ inputs.target-subdir }} STORAGE_BRANCH: ${{ inputs.storage-branch }} PRESERVE: ${{ inputs.preserve }} - GH_TOKEN: ${{ inputs.github-token }} - run: | - set -euo pipefail - - if [ ! -d "$SOURCE_DIR" ]; then - echo "::error::source-dir '$SOURCE_DIR' does not exist" >&2 - exit 1 - fi - - REPO_URL="https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" - - rm -rf _site _storage - mkdir -p _site - - # Pull the current full site from the storage branch (if it exists). - if git ls-remote --exit-code --heads "$REPO_URL" "$STORAGE_BRANCH" >/dev/null 2>&1; then - git clone --quiet --depth 1 --branch "$STORAGE_BRANCH" "$REPO_URL" _storage - ( cd _storage && tar -cf - --exclude=.git . ) | ( cd _site && tar -xf - ) - rm -rf _storage - fi - - # Overlay the freshly built app into its slot. - if [ -z "$TARGET_SUBDIR" ] || [ "$TARGET_SUBDIR" = "." ]; then - # Site root (shell): replace root entries but keep sibling micro apps. - keep=".git .nojekyll $PRESERVE" - shopt -s dotglob nullglob - for entry in _site/*; do - name="$(basename "$entry")" - case " $keep " in - *" $name "*) continue ;; - esac - rm -rf "$entry" - done - shopt -u dotglob nullglob - cp -R "$SOURCE_DIR"/. _site/ - else - rm -rf "_site/${TARGET_SUBDIR}" - mkdir -p "_site/${TARGET_SUBDIR}" - cp -R "$SOURCE_DIR"/. "_site/${TARGET_SUBDIR}/" - fi - - # SPA fallback + disable Jekyll processing. - touch _site/.nojekyll - if [ -f _site/index.html ]; then - cp _site/index.html _site/404.html - fi - - echo "Assembled site tree:" - find _site -maxdepth 2 -mindepth 1 | sort + run: ${{ github.action_path }}/assemble-site.sh - name: Upload Pages artifact 📦 uses: actions/upload-pages-artifact@v5 @@ -99,19 +51,4 @@ runs: TARGET_SUBDIR: ${{ inputs.target-subdir }} STORAGE_BRANCH: ${{ inputs.storage-branch }} GH_TOKEN: ${{ inputs.github-token }} - run: | - set -euo pipefail - REPO_URL="https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" - SLOT="${TARGET_SUBDIR:-/ (shell)}" - - cd _site - rm -rf .git - git init -q - git add -A - git -c user.name="github-actions[bot]" \ - -c user.email="41898282+github-actions[bot]@users.noreply.github.com" \ - commit -q -m "deploy: update ${SLOT} (${GITHUB_SHA})" || { - echo "Nothing to persist."; exit 0; - } - git branch -M "$STORAGE_BRANCH" - git push -f "$REPO_URL" "$STORAGE_BRANCH" + run: ${{ github.action_path }}/persist-site.sh diff --git a/.github/actions/deploy-subdir/assemble-site.sh b/.github/actions/deploy-subdir/assemble-site.sh new file mode 100755 index 0000000..d2d19d8 --- /dev/null +++ b/.github/actions/deploy-subdir/assemble-site.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +set -euo pipefail + +if [ ! -d "$SOURCE_DIR" ]; then + echo "::error::source-dir '$SOURCE_DIR' does not exist" >&2 + exit 1 +fi + +REPO_URL="https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" + +rm -rf _site _storage +mkdir -p _site + +# Pull the current full site from the storage branch (if it exists). +if git ls-remote --exit-code --heads "$REPO_URL" "$STORAGE_BRANCH" >/dev/null 2>&1; then + git clone --quiet --depth 1 --branch "$STORAGE_BRANCH" "$REPO_URL" _storage + ( cd _storage && tar -cf - --exclude=.git . ) | ( cd _site && tar -xf - ) + rm -rf _storage +fi + +# Overlay the freshly built app into its slot. +if [ -z "$TARGET_SUBDIR" ] || [ "$TARGET_SUBDIR" = "." ]; then + # Site root (shell): replace root entries but keep sibling micro apps. + keep=".git .nojekyll $PRESERVE" + shopt -s dotglob nullglob + for entry in _site/*; do + name="$(basename "$entry")" + case " $keep " in + *" $name "*) continue ;; + esac + rm -rf "$entry" + done + shopt -u dotglob nullglob + cp -R "$SOURCE_DIR"/. _site/ +else + rm -rf "_site/${TARGET_SUBDIR}" + mkdir -p "_site/${TARGET_SUBDIR}" + cp -R "$SOURCE_DIR"/. "_site/${TARGET_SUBDIR}/" +fi + +# SPA fallback + disable Jekyll processing. +touch _site/.nojekyll +if [ -f _site/index.html ]; then + cp _site/index.html _site/404.html +fi + +echo "Assembled site tree:" +find _site -maxdepth 2 -mindepth 1 | sort diff --git a/.github/actions/deploy-subdir/persist-site.sh b/.github/actions/deploy-subdir/persist-site.sh new file mode 100755 index 0000000..b1daa91 --- /dev/null +++ b/.github/actions/deploy-subdir/persist-site.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -euo pipefail + +REPO_URL="https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" +SLOT="${TARGET_SUBDIR:-/ (shell)}" + +cd _site +rm -rf .git +git init -q +git add -A +git -c user.name="github-actions[bot]" \ + -c user.email="41898282+github-actions[bot]@users.noreply.github.com" \ + commit -q -m "deploy: update ${SLOT} (${GITHUB_SHA})" || { + echo "Nothing to persist."; exit 0; + } +git branch -M "$STORAGE_BRANCH" +git push -f "$REPO_URL" "$STORAGE_BRANCH"