From 2240ca38bd1b1447dc34ccbf78b5804b231aa1e2 Mon Sep 17 00:00:00 2001 From: bigjakk Date: Thu, 16 Apr 2026 08:10:14 -0700 Subject: [PATCH] ci: auto-detect patch vs full release in workflow --- .github/workflows/build-release.yml | 214 ++++++++++++++++++++++++---- 1 file changed, 184 insertions(+), 30 deletions(-) diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index 5769b58..ae2eaa1 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -4,6 +4,17 @@ on: push: branches: - main + workflow_dispatch: + inputs: + release_type: + description: 'Override release type (auto-detected by default)' + required: false + default: 'auto' + type: choice + options: + - auto + - patch + - full permissions: contents: write @@ -14,18 +25,24 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 + with: + fetch-depth: 0 - name: Check if version already released id: version-check env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITEA_TOKEN: ${{ secrets.DEST_GITEA_TOKEN }} run: | VERSION=$(grep '"version"' package.json | head -1 | sed 's/.*"version": *"\([^"]*\)".*/\1/') TAG="v$VERSION" echo "VERSION=$VERSION" >> "$GITHUB_OUTPUT" echo "TAG=$TAG" >> "$GITHUB_OUTPUT" - if gh release view "$TAG" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then + STATUS=$(curl -s -o /dev/null -w "%{http_code}" \ + "https://gitea.crjlab.net/api/v1/repos/bigjakk/Krunker-Civilian-Client-Test/releases/tags/$TAG" \ + -H "Authorization: token $GITEA_TOKEN") + + if [ "$STATUS" = "200" ]; then echo "Release $TAG already exists, skipping build" echo "SKIP=true" >> "$GITHUB_OUTPUT" else @@ -33,14 +50,64 @@ jobs: echo "SKIP=false" >> "$GITHUB_OUTPUT" fi + - name: Detect release type + if: steps.version-check.outputs.SKIP == 'false' + id: release-type + run: | + OVERRIDE="${{ github.event.inputs.release_type || 'auto' }}" + + if [ "$OVERRIDE" != "auto" ]; then + echo "Release type overridden to: $OVERRIDE" + echo "TYPE=$OVERRIDE" >> "$GITHUB_OUTPUT" + exit 0 + fi + + # Auto-detect: find what changed since the last release tag + PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || true) + + if [ -z "$PREV_TAG" ]; then + echo "No previous tag found — defaulting to full build" + echo "TYPE=full" >> "$GITHUB_OUTPUT" + exit 0 + fi + + echo "Comparing changes since $PREV_TAG" + CHANGED=$(git diff --name-only "$PREV_TAG"..HEAD) + echo "$CHANGED" + + # Files that require a full (installer) build + FULL_TRIGGERS="electron-builder.yml|scripts/download-electron.js" + + if echo "$CHANGED" | grep -qE "^($FULL_TRIGGERS)$"; then + echo "Full build trigger detected in changed files" + echo "TYPE=full" >> "$GITHUB_OUTPUT" + exit 0 + fi + + # Check if package.json deps changed (not just version field) + if echo "$CHANGED" | grep -q "^package.json$"; then + DEP_DIFF=$(git diff "$PREV_TAG"..HEAD -- package.json \ + | grep -E '^\+' | grep -vE '^\+\+\+' \ + | grep -E '"(dependencies|devDependencies|electron|electron-nightly)"' || true) + if [ -n "$DEP_DIFF" ]; then + echo "Dependency changes detected in package.json" + echo "TYPE=full" >> "$GITHUB_OUTPUT" + exit 0 + fi + fi + + echo "Only source changes detected — patch release" + echo "TYPE=patch" >> "$GITHUB_OUTPUT" + - name: Setup Node.js if: steps.version-check.outputs.SKIP == 'false' uses: actions/setup-node@v4 with: node-version: '22' + # Full build needs wine + system deps for electron-builder - name: Install system dependencies - if: steps.version-check.outputs.SKIP == 'false' + if: steps.version-check.outputs.SKIP == 'false' && steps.release-type.outputs.TYPE == 'full' run: | sudo dpkg --add-architecture i386 sudo apt-get update -qq @@ -56,60 +123,147 @@ jobs: if: steps.version-check.outputs.SKIP == 'false' run: npm ci --legacy-peer-deps + # ── Patch release: asar only ── + + - name: Build asar + if: steps.version-check.outputs.SKIP == 'false' && steps.release-type.outputs.TYPE == 'patch' + run: npm run build:asar + + # ── Full release: installer + asar ── + - name: Build source - if: steps.version-check.outputs.SKIP == 'false' + if: steps.version-check.outputs.SKIP == 'false' && steps.release-type.outputs.TYPE == 'full' run: npm run build - name: Build Windows distributables - if: steps.version-check.outputs.SKIP == 'false' + if: steps.version-check.outputs.SKIP == 'false' && steps.release-type.outputs.TYPE == 'full' env: WINEDEBUG: "-all" run: npx electron-builder --win -c.electronDist=node_modules/electron/dist-win --publish never - name: Build Linux distributables - if: steps.version-check.outputs.SKIP == 'false' + if: steps.version-check.outputs.SKIP == 'false' && steps.release-type.outputs.TYPE == 'full' run: npx electron-builder --linux -c.electronDist=node_modules/electron/dist-linux --publish never + - name: Build asar for full release + if: steps.version-check.outputs.SKIP == 'false' && steps.release-type.outputs.TYPE == 'full' + run: npm run build:asar + - name: Report build sizes if: steps.version-check.outputs.SKIP == 'false' run: | - echo "=== Build output sizes ===" - ls -lh out/*.exe out/*.AppImage out/*.deb 2>/dev/null || true + echo "=== Release type: ${{ steps.release-type.outputs.TYPE }} ===" + if [ "${{ steps.release-type.outputs.TYPE }}" = "full" ]; then + ls -lh out/*.exe out/*.AppImage out/*.deb 2>/dev/null || true + fi + ls -lh out/asar/* 2>/dev/null || true - name: Generate release notes if: steps.version-check.outputs.SKIP == 'false' + env: + GITEA_TOKEN: ${{ secrets.DEST_GITEA_TOKEN }} run: | - git fetch --unshallow 2>/dev/null || true chmod +x scripts/generate-release-notes.sh - scripts/generate-release-notes.sh "${{ steps.version-check.outputs.TAG }}" > /tmp/release-notes.md + + PREV_SHA=$(curl -s "https://gitea.crjlab.net/api/v1/repos/bigjakk/Krunker-Civilian-Client-Test/releases?limit=1" \ + -H "Authorization: token $GITEA_TOKEN" \ + | jq -r '.[0].target_commitish // empty' 2>/dev/null || true) + + echo "Previous release SHA: ${PREV_SHA:-none}" + scripts/generate-release-notes.sh "${{ steps.version-check.outputs.TAG }}" "$PREV_SHA" > /tmp/release-notes.md echo "--- Generated release notes ---" cat /tmp/release-notes.md - - name: Create GitHub release and upload assets + - name: Create release and upload assets if: steps.version-check.outputs.SKIP == 'false' env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITEA_TOKEN: ${{ secrets.DEST_GITEA_TOKEN }} + TAG: ${{ steps.version-check.outputs.TAG }} + RELEASE_TYPE: ${{ steps.release-type.outputs.TYPE }} run: | - # Collect built artifacts - ASSETS=() - for file in out/*.exe out/*.AppImage out/*.deb; do - [ -f "$file" ] || continue - ASSETS+=("$file") - done + GITEA_BASE="https://gitea.crjlab.net" + REPO="bigjakk/Krunker-Civilian-Client-Test" - if [ ${#ASSETS[@]} -eq 0 ]; then - echo "ERROR: No build artifacts found in out/" - ls -la out/ 2>/dev/null || echo "out/ directory does not exist" + # Create tag + curl -s -X POST "$GITEA_BASE/api/v1/repos/$REPO/tags" \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{\"tag_name\": \"$TAG\", \"message\": \"$TAG\", \"target\": \"$GITHUB_SHA\"}" + + # Read release notes + NOTES=$(cat /tmp/release-notes.md) + BODY=$(echo "$NOTES" | python3 -c 'import sys,json; print(json.dumps(sys.stdin.read()))') + + # Create release + RESPONSE=$(curl -s -X POST "$GITEA_BASE/api/v1/repos/$REPO/releases" \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{ + \"tag_name\": \"$TAG\", + \"name\": \"$TAG\", + \"body\": $BODY, + \"draft\": false, + \"prerelease\": false + }") + + RELEASE_ID=$(echo "$RESPONSE" | jq -r '.id') + echo "Created release ID: $RELEASE_ID" + + if [ "$RELEASE_ID" = "null" ] || [ -z "$RELEASE_ID" ]; then + echo "Failed to create release:" + echo "$RESPONSE" exit 1 fi - echo "Uploading ${#ASSETS[@]} assets:" - printf ' %s\n' "${ASSETS[@]}" + # Upload asar + checksums (both patch and full releases) + for file in out/asar/app.asar out/asar/checksums.sha256; do + [ -f "$file" ] || continue + FILENAME=$(basename "$file") + echo "Uploading: $FILENAME ($(du -h "$file" | cut -f1))" + curl -s -X POST \ + "$GITEA_BASE/api/v1/repos/$REPO/releases/$RELEASE_ID/assets?name=$FILENAME" \ + -H "Authorization: token $GITEA_TOKEN" \ + -F "attachment=@$file" \ + | jq -r '" -> \(.name) (\(.size) bytes)"' + done - gh release create "${{ steps.version-check.outputs.TAG }}" \ - --repo "$GITHUB_REPOSITORY" \ - --title "${{ steps.version-check.outputs.TAG }}" \ - --notes-file /tmp/release-notes.md \ - --draft=false \ - --latest \ - "${ASSETS[@]}" + # Upload installer artifacts (full release only) + if [ "$RELEASE_TYPE" = "full" ]; then + for file in out/*.exe out/*.AppImage out/*.deb; do + [ -f "$file" ] || continue + FILENAME=$(basename "$file") + SAFE_NAME=$(echo "$FILENAME" | tr ' ' '_') + echo "Uploading: $SAFE_NAME ($(du -h "$file" | cut -f1))" + curl -s -X POST \ + "$GITEA_BASE/api/v1/repos/$REPO/releases/$RELEASE_ID/assets?name=$SAFE_NAME" \ + -H "Authorization: token $GITEA_TOKEN" \ + -F "attachment=@$file" \ + | jq -r '" -> \(.name) (\(.size) bytes)"' + done + fi + + echo "All assets uploaded (release type: $RELEASE_TYPE)" + + - name: Prune old releases + if: steps.version-check.outputs.SKIP == 'false' + env: + GITEA_TOKEN: ${{ secrets.DEST_GITEA_TOKEN }} + KEEP: 5 + run: | + GITEA_BASE="https://gitea.crjlab.net" + REPO="bigjakk/Krunker-Civilian-Client-Test" + echo "=== Pruning $REPO (keeping last $KEEP) ===" + RELEASES=$(curl -s "$GITEA_BASE/api/v1/repos/$REPO/releases?limit=50" \ + -H "Authorization: token $GITEA_TOKEN") + + DELETE_IDS=$(echo "$RELEASES" | jq -r "sort_by(.created_at) | reverse | .[$KEEP:][] | .id") + + for ID in $DELETE_IDS; do + TAG_NAME=$(echo "$RELEASES" | jq -r ".[] | select(.id == $ID) | .tag_name") + echo "Deleting release $TAG_NAME (id=$ID)" + curl -s -X DELETE "$GITEA_BASE/api/v1/repos/$REPO/releases/$ID" \ + -H "Authorization: token $GITEA_TOKEN" + curl -s -X DELETE "$GITEA_BASE/api/v1/repos/$REPO/tags/$TAG_NAME" \ + -H "Authorization: token $GITEA_TOKEN" + done + echo "Prune complete"