From e20d2818bed3a5b81c02a601abc648d808f6e25b Mon Sep 17 00:00:00 2001 From: Staninna <26458805+Staninna@users.noreply.github.com> Date: Wed, 27 Aug 2025 16:55:03 +0200 Subject: [PATCH] update cd.yml to be better??? test run --- .github/workflows/cd.yml | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index ebc3c81..1c26237 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -20,6 +20,7 @@ jobs: environment: name: github-pages url: ${{ steps.deploy.outputs.page_url }} + steps: - name: Checkout (shallow) uses: actions/checkout@v4 @@ -34,6 +35,12 @@ jobs: run: | set -euo pipefail + # Ensure jq is available (usually preinstalled) + if ! command -v jq >/dev/null 2>&1; then + sudo apt-get update -y + sudo apt-get install -y jq + fi + OWNER="${GITHUB_REPOSITORY%/*}" REPO="${GITHUB_REPOSITORY#*/}" SITE_ROOT="/${REPO}/" @@ -41,13 +48,14 @@ jobs: mkdir -p public : > public/.nojekyll - # List remote branches + # List remote branches (exclude symbolic HEAD) mapfile -t BRANCHES < <( - git ls-remote --heads origin | awk '{print $2}' \ + git ls-remote --heads origin \ + | awk '{print $2}' \ | sed 's@refs/heads/@@' ) - # Top-level landing page + # Landing page shell cat > public/index.html <<'HTML' @@ -66,17 +74,16 @@ jobs:
HTML - # Helper to create an index.html for every directory + # Function to emit an index.html for a directory gen_index_dir() { local DIR="$1" ROOT="$2" BR="$3" SAFE="$4" local rel="${DIR#$ROOT}" # "" or "/sub/dir" local title="Branch: ${BR}${rel:-/}" - # Collect entries mapfile -t SUBDIRS < <(find "$DIR" -mindepth 1 -maxdepth 1 -type d \ -printf '%f\n' | sort) mapfile -t FILES < <(find "$DIR" -mindepth 1 -maxdepth 1 -type f \ @@ -108,16 +115,16 @@ jobs: } > "$DIR/index.html" } - # Process each branch + # Build trees and index entries per branch for BR in "${BRANCHES[@]}"; do SAFE="$(echo "$BR" | tr '/:@ ' '----' | sed 's/[^A-Za-z0-9._-]/-/g')" mkdir -p "public/$SAFE" - # Fetch and extract content without checkout + # Export branch contents without checkout git fetch --depth=1 origin "$BR" git archive --format=tar FETCH_HEAD | tar -x -C "public/$SAFE" - # Keep only static assets, then remove empty dirs + # Keep only static assets, then prune empties rm -rf "public/$SAFE/.git" "public/$SAFE/.github" "public/$SAFE/node_modules" find "public/$SAFE" -type f ! \ \( -name '*.html' -o -name '*.js' -o -name '*.css' -o -name '*.json' -o \ @@ -125,17 +132,20 @@ jobs: -name '*.svg' \) -delete find "public/$SAFE" -type d -empty -delete - # Generate index.html for every directory (recursive) + # Generate index.html for every directory in this branch ROOT_DIR="public/$SAFE" while IFS= read -r -d '' d; do gen_index_dir "$d" "$ROOT_DIR" "$BR" "$SAFE" done < <(find "$ROOT_DIR" -type d -print0) - # Add branch to landing page list - echo "" >> public/index.html + # Append to landing page using JSON-escaped values + SAFE_JSON=$(printf '%s' "$SAFE" | jq -Rr @json) + BR_JSON=$(printf '%s' "$BR" | jq -Rr @json) + echo "" \ + >> public/index.html done - # Finish landing page with JS to render branch cards + # Finish landing page cat >> public/index.html <<'HTML'