diff --git a/.gitea/workflows/ci-cd.yaml b/.gitea/workflows/ci-cd.yaml index 65a7955..7a46993 100644 --- a/.gitea/workflows/ci-cd.yaml +++ b/.gitea/workflows/ci-cd.yaml @@ -89,56 +89,10 @@ jobs: COVERAGE=$(grep "total:" coverage.txt | grep -oP '\d+\.\d+' | head -1) echo "Coverage: ${COVERAGE}%" - # Determine badge color - if (( $(echo "$COVERAGE >= 80" | bc -l) )); then - COLOR="brightgreen" - elif (( $(echo "$COVERAGE >= 50" | bc -l) )); then - COLOR="yellow" - else - COLOR="red" - fi - - # Update README with new badge - BADGE_URL="https://img.shields.io/badge/coverage-${COVERAGE}%-${COLOR}?style=flat-square" - sed -i "s|https://img.shields.io/badge/coverage-.*-.*?style=flat-square|${BADGE_URL}|" README.md - - # Set up git authentication and commit - git config --global user.name "CI Bot" - git config --global user.email "ci@arcodange.fr" - - # Set up credentials using Gitea token - git config --global credential.helper store - echo "https://${{ secrets.PACKAGES_TOKEN }}@gitea.arcodange.lab" > ~/.git-credentials - - # Only update if coverage has actually changed - if grep -q "coverage-${COVERAGE}%" README.md; then - echo "Coverage badge already up to date at ${COVERAGE}%" - else - git add README.md - git commit -m "🤖 chore: update coverage badge to ${COVERAGE}% [skip ci]" || echo "No coverage change to commit" - - # Try push with retry logic for race conditions - for i in 1 2 3; do - if git push; then - echo "Successfully updated coverage badge to ${COVERAGE}%" - # Update local repo to the new HEAD after successful push - git fetch origin - git reset --hard origin/${{ github.ref_name }} - break - else - echo "Push attempt $i failed, retrying..." - if [ $i -eq 3 ]; then - echo "Final push attempt failed - another job may have updated the badge" - git pull --rebase || true - git push || echo "Recovery push also failed" - # Ensure we're on the latest commit even if push failed - git fetch origin - git reset --hard origin/${{ github.ref_name }} - fi - sleep 2 - fi - done - fi + # Update coverage badge using script + export PACKAGES_TOKEN="${{ secrets.PACKAGES_TOKEN }}" + export GITHUB_REF_NAME="${{ github.ref_name }}" + ./scripts/ci-update-coverage-badge.sh "$COVERAGE" - name: Run go fmt run: go fmt ./... @@ -188,34 +142,9 @@ jobs: NEW_VERSION="$MAJOR.$MINOR.$PATCH${PRERELEASE:+-$PRERELEASE}" sed -i "s|// @version [0-9.]*|// @version $NEW_VERSION|" cmd/server/main.go - # Commit version changes if bumped - if [ "$VERSION_BUMPED" = "true" ]; then - git config --global user.name "CI Bot" - git config --global user.email "ci@arcodange.fr" - - # Set up credentials using Gitea token - git config --global credential.helper store - echo "https://${{ secrets.PACKAGES_TOKEN }}@gitea.arcodange.lab" > ~/.git-credentials - - git add VERSION cmd/server/main.go README.md - git commit -m "chore: auto version bump [skip ci]" || echo "No changes to commit" - - # Try push with retry logic for race conditions - for i in 1 2 3; do - if git push; then - echo "Successfully bumped version" - break - else - echo "Version bump push attempt $i failed, retrying..." - if [ $i -eq 3 ]; then - echo "Final version bump push attempt failed - another job may have bumped version" - git pull --rebase || true - git push || echo "Version bump recovery push also failed" - fi - sleep 2 - fi - done - fi + # Handle version bump using script + export PACKAGES_TOKEN="${{ secrets.PACKAGES_TOKEN }}" + ./scripts/ci-version-bump.sh "$LAST_COMMIT" - name: Login to Gitea Container Registry if: github.ref == 'refs/heads/main' diff --git a/scripts/ci-update-coverage-badge.sh b/scripts/ci-update-coverage-badge.sh new file mode 100755 index 0000000..4d9ae7e --- /dev/null +++ b/scripts/ci-update-coverage-badge.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# CI script to update coverage badge in README.md +# Usage: scripts/ci-update-coverage-badge.sh + +set -e + +if [ -z "$1" ]; then + echo "Error: Coverage percentage not provided" + exit 1 +fi + +COVERAGE=$1 + +# Determine badge color +if (( $(echo "$COVERAGE >= 80" | bc -l) )); then + COLOR="brightgreen" +elif (( $(echo "$COVERAGE >= 50" | bc -l) )); then + COLOR="yellow" +else + COLOR="red" +fi + +BADGE_URL="https://img.shields.io/badge/coverage-${COVERAGE}%-${COLOR}?style=flat-square" + +# Only update if coverage has actually changed +if grep -q "coverage-${COVERAGE}%" README.md; then + echo "Coverage badge already up to date at ${COVERAGE}%" + exit 0 +fi + +# Update README +sed -i "s|https://img.shields.io/badge/coverage-.*-.*?style=flat-square|${BADGE_URL}|" README.md + +# Set up git +git config --global user.name "CI Bot" +git config --global user.email "ci@arcodange.fr" + +# Set up credentials using Gitea token +if [ -n "$PACKAGES_TOKEN" ]; then + git config --global credential.helper store + echo "https://${PACKAGES_TOKEN}@gitea.arcodange.lab" > ~/.git-credentials +fi + +git add README.md +if git commit -m "🤖 chore: update coverage badge to ${COVERAGE}% [skip ci]"; then + # Try push with retry logic for race conditions + for i in 1 2 3; do + if git push; then + echo "Successfully updated coverage badge to ${COVERAGE}%" + # Update local repo to the new HEAD after successful push + git fetch origin + git reset --hard origin/${GITHUB_REF_NAME:-${CI_COMMIT_REF_NAME:-main}} + exit 0 + else + echo "Push attempt $i failed, retrying..." + if [ $i -eq 3 ]; then + echo "Final push attempt failed - another job may have updated the badge" + git pull --rebase || true + git push || echo "Recovery push also failed" + # Ensure we're on the latest commit even if push failed + git fetch origin + git reset --hard origin/${GITHUB_REF_NAME:-${CI_COMMIT_REF_NAME:-main}} + fi + sleep 2 + fi + done +else + echo "No coverage change to commit" +fi \ No newline at end of file diff --git a/scripts/ci-version-bump.sh b/scripts/ci-version-bump.sh new file mode 100755 index 0000000..b900fc7 --- /dev/null +++ b/scripts/ci-version-bump.sh @@ -0,0 +1,68 @@ +#!/bin/bash +# CI script to handle automatic version bumping +# Usage: scripts/ci-version-bump.sh + +set -e + +if [ -z "$1" ]; then + echo "Error: Commit message not provided" + exit 1 +fi + +LAST_COMMIT=$1 +VERSION_BUMPED="false" + +# Automatic version bump based on commit type +if echo "$LAST_COMMIT" | grep -q "^✨ feat:"; then + echo "🎯 Feature commit detected - bumping MINOR version" + ./scripts/version-bump.sh minor + VERSION_BUMPED="true" +elif echo "$LAST_COMMIT" | grep -q "^🐛 fix:"; then + echo "🐛 Fix commit detected - bumping PATCH version" + ./scripts/version-bump.sh patch + VERSION_BUMPED="true" +elif echo "$LAST_COMMIT" | grep -q "BREAKING CHANGE"; then + echo "💥 Breaking change detected - bumping MAJOR version" + ./scripts/version-bump.sh major + VERSION_BUMPED="true" +else + echo "⏭️ No automatic version bump needed" +fi + +# Update swagger version regardless of bump +source VERSION +NEW_VERSION="$MAJOR.$MINOR.$PATCH${PRERELEASE:+-$PRERELEASE}" +sed -i "s|// @version [0-9.]*|// @version $NEW_VERSION|" cmd/server/main.go + +# Commit version changes if bumped +if [ "$VERSION_BUMPED" = "true" ]; then + git config --global user.name "CI Bot" + git config --global user.email "ci@arcodange.fr" + + # Set up credentials using Gitea token + if [ -n "$PACKAGES_TOKEN" ]; then + git config --global credential.helper store + echo "https://${PACKAGES_TOKEN}@gitea.arcodange.lab" > ~/.git-credentials + fi + + git add VERSION cmd/server/main.go README.md + if git commit -m "chore: auto version bump [skip ci]"; then + # Try push with retry logic for race conditions + for i in 1 2 3; do + if git push; then + echo "Successfully bumped version to $NEW_VERSION" + exit 0 + else + echo "Version bump push attempt $i failed, retrying..." + if [ $i -eq 3 ]; then + echo "Final version bump push attempt failed - another job may have bumped version" + git pull --rebase || true + git push || echo "Version bump recovery push also failed" + fi + sleep 2 + fi + done + else + echo "No version changes to commit" + fi +fi \ No newline at end of file