256 lines
8.5 KiB
YAML
256 lines
8.5 KiB
YAML
name: Build, Tag, Push, and Release to GitHub Container Registry
|
||
|
||
on:
|
||
push:
|
||
branches:
|
||
- '**'
|
||
|
||
jobs:
|
||
build-and-push:
|
||
runs-on: ubuntu-latest
|
||
|
||
permissions:
|
||
contents: write
|
||
packages: write
|
||
issues: read
|
||
checks: write
|
||
pull-requests: write
|
||
|
||
steps:
|
||
- name: Checkout Repository
|
||
uses: actions/checkout@v4
|
||
with:
|
||
fetch-depth: 0
|
||
|
||
- name: Log in to Docker Hub
|
||
uses: docker/login-action@v2
|
||
with:
|
||
username: ${{ secrets.DOCKER_USERNAME }}
|
||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||
|
||
- name: Log in to GitHub Container Registry
|
||
uses: docker/login-action@v2
|
||
with:
|
||
registry: ghcr.io
|
||
username: ${{ github.actor }}
|
||
password: ${{ secrets.GITHUB_TOKEN }}
|
||
|
||
- name: Set up QEMU for multi-arch builds
|
||
uses: docker/setup-qemu-action@v3
|
||
|
||
- name: Set up Docker Buildx
|
||
uses: docker/setup-buildx-action@v3
|
||
|
||
# Set up Java for backend tests
|
||
- name: Set up JDK 21
|
||
uses: actions/setup-java@v4
|
||
with:
|
||
java-version: '21'
|
||
distribution: 'temurin'
|
||
|
||
# Run backend tests with testcontainers
|
||
- name: Run Backend Tests
|
||
id: backend_tests
|
||
working-directory: ./booklore-api
|
||
run: |
|
||
echo "Running backend tests with testcontainers..."
|
||
./gradlew test
|
||
continue-on-error: true
|
||
|
||
# Publish test results
|
||
- name: Publish 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
|
||
comment_title: Backend Test Results
|
||
report_individual_runs: true
|
||
report_suite_logs: 'any'
|
||
|
||
# Upload test reports as artifacts
|
||
- name: Upload Test Reports
|
||
uses: actions/upload-artifact@v4
|
||
if: always()
|
||
with:
|
||
name: test-reports
|
||
path: |
|
||
booklore-api/build/reports/tests/
|
||
booklore-api/build/test-results/
|
||
retention-days: 30
|
||
|
||
# Fail the workflow if tests failed
|
||
- name: Check Test Results
|
||
if: steps.backend_tests.outcome == 'failure'
|
||
run: |
|
||
echo "❌ Backend tests failed! Check the test results above."
|
||
exit 1
|
||
|
||
- name: Get Latest Master Version
|
||
id: get_version
|
||
run: |
|
||
latest_tag=$(git tag --list "v*" --sort=-v:refname | head -n 1)
|
||
latest_tag=${latest_tag:-"v0.0.0"}
|
||
echo "latest_tag=$latest_tag" >> $GITHUB_ENV
|
||
echo "Latest master tag: $latest_tag"
|
||
|
||
- name: Determine Version Bump (Only for Master)
|
||
if: github.ref == 'refs/heads/master'
|
||
id: determine_bump
|
||
run: |
|
||
# Try to parse the PR number from the merge commit message
|
||
pr_number=$(git log -1 --pretty=%B \
|
||
| grep -oE 'Merge pull request #[0-9]+' \
|
||
| grep -oE '[0-9]+') || true
|
||
# Fallback to latest merged PR if parsing didn’t work
|
||
if [ -z "$pr_number" ]; then
|
||
pr_number=$(gh pr list --state merged --base master --limit 1 \
|
||
--json number --jq '.[0].number')
|
||
fi
|
||
echo "PR number: $pr_number"
|
||
|
||
labels=$(gh pr view "$pr_number" --json labels --jq '.labels[].name' || echo "")
|
||
if echo "$labels" | grep -q 'major'; then
|
||
bump="major"
|
||
elif echo "$labels" | grep -q 'minor'; then
|
||
bump="minor"
|
||
elif echo "$labels" | grep -q 'patch'; then
|
||
bump="patch"
|
||
fi
|
||
|
||
if [ -z "$bump" ]; then
|
||
last_commit_msg=$(git log -1 --pretty=%B)
|
||
if echo "$last_commit_msg" | grep -iq '#major'; then
|
||
bump="major"
|
||
gh pr edit "$pr_number" --add-label "major" || echo "Could not add major label"
|
||
elif echo "$last_commit_msg" | grep -iq '#minor'; then
|
||
bump="minor"
|
||
gh pr edit "$pr_number" --add-label "minor" || echo "Could not add minor label"
|
||
elif echo "$last_commit_msg" | grep -iq '#patch'; then
|
||
bump="patch"
|
||
gh pr edit "$pr_number" --add-label "patch" || echo "Could not add patch label"
|
||
else
|
||
bump="patch"
|
||
gh pr edit "$pr_number" --add-label "patch" || echo "Could not add patch label"
|
||
fi
|
||
fi
|
||
|
||
echo "bump=$bump" >> $GITHUB_ENV
|
||
echo "Version bump type: $bump"
|
||
env:
|
||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||
|
||
- name: Update Release Draft (Only for Master)
|
||
if: github.ref == 'refs/heads/master'
|
||
uses: release-drafter/release-drafter@v6
|
||
id: release_drafter
|
||
env:
|
||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||
|
||
- name: Get Release Version
|
||
if: github.ref == 'refs/heads/master'
|
||
run: |
|
||
new_tag="${{ steps.release_drafter.outputs.tag_name }}"
|
||
echo "new_tag=$new_tag" >> $GITHUB_ENV
|
||
echo "New version: $new_tag"
|
||
|
||
- name: Generate Image Tag
|
||
id: set_image_tag
|
||
run: |
|
||
branch="${GITHUB_REF#refs/heads/}"
|
||
if [[ "$branch" == "master" ]]; then
|
||
image_tag="${{ env.new_tag }}"
|
||
elif [[ "$branch" == "develop" ]]; then
|
||
short_sha=$(git rev-parse --short HEAD)
|
||
image_tag="${{ env.latest_tag }}-develop-${short_sha}"
|
||
else
|
||
short_sha=$(git rev-parse --short HEAD)
|
||
image_tag="${short_sha}"
|
||
fi
|
||
echo "image_tag=$image_tag" >> $GITHUB_ENV
|
||
echo "Image tag: $image_tag"
|
||
|
||
- name: Build and Push Docker Image (Docker Hub & GHCR)
|
||
run: |
|
||
docker buildx create --use
|
||
docker buildx build \
|
||
--platform linux/amd64,linux/arm64 \
|
||
--build-arg APP_VERSION=${{ env.image_tag }} \
|
||
--tag booklore/booklore:${{ env.image_tag }} \
|
||
--tag ghcr.io/booklore-app/booklore:${{ env.image_tag }} \
|
||
--push .
|
||
|
||
- name: Push Latest Tag (Only for Master)
|
||
if: github.ref == 'refs/heads/master'
|
||
run: |
|
||
docker buildx build \
|
||
--platform linux/amd64,linux/arm64 \
|
||
--build-arg APP_VERSION=${{ env.new_tag }} \
|
||
--tag booklore/booklore:latest \
|
||
--tag ghcr.io/booklore-app/booklore:latest \
|
||
--push .
|
||
|
||
- name: Create Git Tag (Only for Master)
|
||
if: github.ref == 'refs/heads/master'
|
||
run: |
|
||
git config --global user.name "github-actions"
|
||
git config --global user.email "github-actions@github.com"
|
||
git tag ${{ env.new_tag }}
|
||
git push origin ${{ env.new_tag }}
|
||
|
||
|
||
- name: Publish Draft Release (Only for Master)
|
||
if: github.ref == 'refs/heads/master'
|
||
env:
|
||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||
run: |
|
||
gh release edit ${{ env.new_tag }} --draft=false
|
||
|
||
- name: Notify Discord of New Release
|
||
if: github.ref == 'refs/heads/master'
|
||
continue-on-error: true
|
||
shell: bash
|
||
env:
|
||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
|
||
NEW_TAG: ${{ env.new_tag }}
|
||
run: |
|
||
set -euo pipefail
|
||
if [ -z "${DISCORD_WEBHOOK_URL:-}" ]; then
|
||
echo "DISCORD_WEBHOOK_URL not set, skipping Discord notification."
|
||
exit 0
|
||
fi
|
||
echo "Preparing Discord notification for release $NEW_TAG"
|
||
|
||
release_json=$(gh release view "$NEW_TAG" --json name,body,url)
|
||
release_name=$(jq -r '.name' <<< "$release_json")
|
||
release_body=$(jq -r '.body' <<< "$release_json")
|
||
release_url=$(jq -r '.url' <<< "$release_json")
|
||
|
||
clean_body=$(echo "$release_body" | tr -d '\r')
|
||
max_length=1800
|
||
if [ ${#clean_body} -gt $max_length ]; then
|
||
clean_body="${clean_body:0:$((max_length-12))}… [truncated]"
|
||
fi
|
||
|
||
payload=$(jq -n \
|
||
--arg title "New Release: $release_name" \
|
||
--arg url "$release_url" \
|
||
--arg desc "$clean_body" \
|
||
'{
|
||
content: null,
|
||
embeds: [{
|
||
title: $title,
|
||
url: $url,
|
||
description: $desc,
|
||
color: 3066993
|
||
}]
|
||
}')
|
||
|
||
echo "=== Discord payload ==="
|
||
echo "$payload"
|
||
echo "======================="
|
||
|
||
curl -H "Content-Type: application/json" -d "$payload" "$DISCORD_WEBHOOK_URL"
|