Files
grimmory/.github/workflows/master-pipeline.yml
T
2026-01-21 23:51:34 -07:00

339 lines
12 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Master Tag – Build, Push, and Publish
on:
push:
tags:
- 'v*'
jobs:
validate-tag:
name: Validate Release Tag
runs-on: ubuntu-latest
outputs:
version: ${{ steps.parse.outputs.version }}
is_prerelease: ${{ steps.parse.outputs.is_prerelease }}
steps:
- name: Checkout Repository
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Parse and Validate Tag
id: parse
run: |
tag="${{ github.ref_name }}"
echo "Processing tag: $tag"
# Validate semver format (v1.2.3 or v1.2.3-rc.1, v1.2.3-beta.2, etc.)
if ! echo "$tag" | grep -qE '^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z]+\.[0-9]+)?$'; then
echo "::error::Invalid tag format. Expected 'vX.Y.Z' or 'vX.Y.Z-prerelease.N', got '$tag'"
exit 1
fi
# Check if this is a prerelease (contains hyphen after version)
if echo "$tag" | grep -qE '^v[0-9]+\.[0-9]+\.[0-9]+-'; then
is_prerelease="true"
else
is_prerelease="false"
fi
# ----------------------------------------
# Version sanity check against latest tag
# ----------------------------------------
# Get latest stable tag (excluding prereleases and current tag)
latest_tag=$(git tag --list "v*" --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | grep -v "^${tag}$" | head -n 1)
latest_tag=${latest_tag:-"v0.0.0"}
echo "Latest stable tag: $latest_tag"
# Extract base version (strip prerelease suffix if present)
base_version=$(echo "$tag" | sed 's/^v//' | sed 's/-.*//')
# Parse latest version
latest_semver=$(echo "$latest_tag" | sed 's/^v//')
latest_major=$(echo "$latest_semver" | cut -d. -f1)
latest_minor=$(echo "$latest_semver" | cut -d. -f2)
latest_patch=$(echo "$latest_semver" | cut -d. -f3)
# Parse target version
target_major=$(echo "$base_version" | cut -d. -f1)
target_minor=$(echo "$base_version" | cut -d. -f2)
target_patch=$(echo "$base_version" | cut -d. -f3)
echo "Latest: v${latest_major}.${latest_minor}.${latest_patch}"
echo "Target: v${target_major}.${target_minor}.${target_patch}"
# Check for backwards version (target < latest)
if [ "$target_major" -lt "$latest_major" ]; then
echo "::error::Target version v${base_version} is older than latest tag ${latest_tag}"
exit 1
elif [ "$target_major" -eq "$latest_major" ] && [ "$target_minor" -lt "$latest_minor" ]; then
echo "::error::Target version v${base_version} is older than latest tag ${latest_tag}"
exit 1
elif [ "$target_major" -eq "$latest_major" ] && [ "$target_minor" -eq "$latest_minor" ] && [ "$target_patch" -lt "$latest_patch" ]; then
echo "::error::Target version v${base_version} is older than latest tag ${latest_tag}"
exit 1
fi
# For stable releases, ensure version is strictly newer
if [ "$is_prerelease" = "false" ]; then
if [ "$target_major" -eq "$latest_major" ] && [ "$target_minor" -eq "$latest_minor" ] && [ "$target_patch" -eq "$latest_patch" ]; then
echo "::error::Stable release v${base_version} already exists as ${latest_tag}"
exit 1
fi
fi
# For prereleases, check if a stable version already exists for this base version
if [ "$is_prerelease" = "true" ]; then
existing_stable=$(git tag --list "v${base_version}" | head -n 1)
if [ -n "$existing_stable" ]; then
echo "::error::Cannot create prerelease ${tag} - stable version v${base_version} already exists"
exit 1
fi
fi
# Check for suspicious jumps (likely typos)
major_jump=$((target_major - latest_major))
minor_jump=$((target_minor - latest_minor))
patch_jump=$((target_patch - latest_patch))
# Flag suspicious patterns
if [ "$major_jump" -gt 1 ]; then
echo "::error::Suspicious major version jump: ${latest_tag} → ${tag} (jumping ${major_jump} major versions). Typo?"
exit 1
elif [ "$major_jump" -eq 0 ] && [ "$minor_jump" -gt 5 ]; then
echo "::error::Suspicious minor version jump: ${latest_tag} → ${tag} (jumping ${minor_jump} minor versions). Typo?"
exit 1
elif [ "$major_jump" -eq 0 ] && [ "$minor_jump" -eq 0 ] && [ "$patch_jump" -gt 10 ]; then
echo "::error::Suspicious patch version jump: ${latest_tag} → ${tag} (jumping ${patch_jump} patch versions). Typo?"
exit 1
fi
echo "✅ Version sanity check passed: ${latest_tag} → ${tag}"
echo "version=$tag" >> $GITHUB_OUTPUT
echo "is_prerelease=$is_prerelease" >> $GITHUB_OUTPUT
echo "Version: $tag, Prerelease: $is_prerelease"
migration-check:
name: Flyway Migration Check
needs: [ validate-tag ]
uses: ./.github/workflows/migrations-check.yml
with:
base_ref: 'master~1'
head_ref: 'master'
backend-tests:
name: Backend Tests
needs: [ migration-check ]
if: needs.migration-check.result == 'success' || needs.migration-check.result == 'skipped'
runs-on: ubuntu-latest
permissions:
contents: read
checks: write
pull-requests: write
steps:
- name: Checkout Repository
uses: actions/checkout@v6
- name: Set Up JDK 21
uses: actions/setup-java@v5
with:
java-version: '21'
distribution: 'temurin'
cache: gradle
- name: Execute Backend Tests
id: backend_tests
working-directory: ./booklore-api
run: |
echo "Running backend tests..."
./gradlew test --no-daemon --parallel --build-cache
continue-on-error: true
- name: Publish Backend Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: booklore-api/build/test-results/**/*.xml
check_name: Backend Test Results
- name: Upload Backend Test Reports
uses: actions/upload-artifact@v6
if: always()
with:
name: backend-test-reports
path: |
booklore-api/build/reports/tests/
booklore-api/build/test-results/
retention-days: 30
- name: Validate Backend Test Results
if: steps.backend_tests.outcome == 'failure'
run: |
echo "❌ Backend tests failed"
exit 1
frontend-tests:
name: Frontend Tests
needs: [ migration-check ]
if: needs.migration-check.result == 'success' || needs.migration-check.result == 'skipped'
runs-on: ubuntu-latest
permissions:
contents: read
checks: write
pull-requests: write
steps:
- name: Checkout Repository
uses: actions/checkout@v6
- name: Set Up Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'npm'
cache-dependency-path: booklore-ui/package-lock.json
- name: Install Frontend Dependencies
working-directory: ./booklore-ui
run: npm ci --force
- name: Execute Frontend Tests
id: frontend_tests
working-directory: ./booklore-ui
run: |
echo "Running frontend tests..."
npx ng test
continue-on-error: true
- name: Publish Frontend Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: booklore-ui/test-results/vitest-results.xml
check_name: Frontend Test Results
- name: Upload Frontend Test Reports
uses: actions/upload-artifact@v6
if: always()
with:
name: frontend-test-reports
path: |
booklore-ui/test-results/vitest-results.xml
retention-days: 30
- name: Validate Frontend Test Results
if: steps.frontend_tests.outcome == 'failure'
run: |
echo "❌ Frontend tests failed"
exit 1
build-and-release:
needs: [ validate-tag, backend-tests, frontend-tests ]
if: needs.backend-tests.result == 'success' && needs.frontend-tests.result == 'success'
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Checkout Repository
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Authenticate to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Authenticate to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Set Up QEMU for Multi-Architecture Builds
uses: docker/setup-qemu-action@v3
- name: Set Up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Compute Docker Tags
id: docker_tags
run: |
version="${{ needs.validate-tag.outputs.version }}"
is_prerelease="${{ needs.validate-tag.outputs.is_prerelease }}"
# Always include the version tag
tags="booklore/booklore:${version}"
tags="${tags},ghcr.io/booklore-app/booklore:${version}"
# Only add 'latest' tag for stable releases (not prereleases)
if [ "$is_prerelease" = "false" ]; then
tags="${tags},booklore/booklore:latest"
tags="${tags},ghcr.io/booklore-app/booklore:latest"
fi
echo "tags=$tags" >> $GITHUB_OUTPUT
echo "Docker tags: $tags"
- name: Build and Push Docker Image
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.docker_tags.outputs.tags }}
build-args: |
APP_VERSION=${{ needs.validate-tag.outputs.version }}
APP_REVISION=${{ github.sha }}
cache-from: |
type=gha
type=registry,ref=ghcr.io/booklore-app/booklore:buildcache
cache-to: |
type=gha,mode=max
type=registry,ref=ghcr.io/booklore-app/booklore:buildcache,mode=max
- name: Create GitHub Release
uses: release-drafter/release-drafter@v6
with:
tag: ${{ needs.validate-tag.outputs.version }}
name: "Release ${{ needs.validate-tag.outputs.version }}"
prerelease: ${{ needs.validate-tag.outputs.is_prerelease }}
publish: true
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Release Summary
run: |
version="${{ needs.validate-tag.outputs.version }}"
is_prerelease="${{ needs.validate-tag.outputs.is_prerelease }}"
echo "## Release Published" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Version:** \`${version}\`" >> $GITHUB_STEP_SUMMARY
if [ "$is_prerelease" = "true" ]; then
echo "**Type:** Prerelease" >> $GITHUB_STEP_SUMMARY
else
echo "**Type:** Stable Release" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Docker Images:**" >> $GITHUB_STEP_SUMMARY
echo "- \`booklore/booklore:${version}\`" >> $GITHUB_STEP_SUMMARY
echo "- \`ghcr.io/booklore-app/booklore:${version}\`" >> $GITHUB_STEP_SUMMARY
if [ "$is_prerelease" = "false" ]; then
echo "- \`booklore/booklore:latest\`" >> $GITHUB_STEP_SUMMARY
echo "- \`ghcr.io/booklore-app/booklore:latest\`" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Install:**" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
echo "docker pull booklore/booklore:${version}" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY