#!/usr/bin/env bash set -e VERSION="$1" if [ -z "$VERSION" ]; then echo "Usage: ./release.sh " echo "Example: ./release.sh 1.4.0" exit 1 fi # ----------------------------- # SemVer validation # ----------------------------- if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then echo "Version must be SemVer: X.Y.Z" exit 1 fi TAG="v$VERSION" echo "=== Release $TAG ===" # ----------------------------- # Branch check # ----------------------------- BRANCH=$(git rev-parse --abbrev-ref HEAD) if [ "$BRANCH" != "main" ]; then echo "Not on main branch" exit 1 fi # ----------------------------- # Working tree clean? # ----------------------------- if ! git diff --quiet; then echo "Working tree not clean — commit first" exit 1 fi # ----------------------------- # Sync with remote # ----------------------------- git fetch origin LOCAL=$(git rev-parse main) REMOTE=$(git rev-parse origin/main) if [ "$LOCAL" != "$REMOTE" ]; then echo "Local main not in sync with origin/main" exit 1 fi # ----------------------------- # Tag already exists? # ----------------------------- if git rev-parse "$TAG" >/dev/null 2>&1; then echo "Tag already exists locally" exit 1 fi if git ls-remote --tags origin | grep -q "$TAG"; then echo "Tag already exists on remote" exit 1 fi # ----------------------------- # Update versions.env # ----------------------------- echo "Updating versions.env" if grep -q "^APP_VERSION=" versions.env; then sed -i "s/^APP_VERSION=.*/APP_VERSION=${VERSION}/" versions.env else echo "APP_VERSION=${VERSION}" >> versions.env fi # ----------------------------- # Optional changelog entry # ----------------------------- if [ -f CHANGELOG.md ]; then echo "Updating CHANGELOG.md" DATE=$(date +%Y-%m-%d) sed -i "1i ## $VERSION - $DATE\n- release\n" CHANGELOG.md git add CHANGELOG.md fi LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") if [ -n "$LAST_TAG" ]; then echo "Generating release notes from $LAST_TAG → HEAD" git log $LAST_TAG..HEAD --pretty=format:"- %s" > RELEASE_NOTES.tmp else git log --pretty=format:"- %s" > RELEASE_NOTES.tmp fi echo -e "\n## $VERSION\n" | cat - RELEASE_NOTES.tmp >> CHANGELOG.md rm RELEASE_NOTES.tmp # ----------------------------- # Commit version bump # ----------------------------- git add versions.env git commit -m "release: $VERSION" # ----------------------------- # Push main → triggers edge build # ----------------------------- git push origin main echo "Main pushed — edge build will run" # ----------------------------- # Create annotated tag # ----------------------------- git tag -a "$TAG" -m "Release $TAG" # ----------------------------- # Push tag → triggers release build # ----------------------------- git push origin "$TAG" echo "Tag pushed — release build triggered" # ----------------------------- # Optional: wait for CI success # ----------------------------- if [ -n "$GITEA_TOKEN" ]; then echo "Checking CI status..." REPO=$(git config --get remote.origin.url | sed 's#.*/##; s/.git$//') OWNER=$(git config --get remote.origin.url | awk -F'[:/]' '{print $(NF-1)}') for i in {1..30}; do STATUS=$(curl -s \ -H "Authorization: token $GITEA_TOKEN" \ "https://git.pi-farm.de/api/v1/repos/$OWNER/$REPO/actions/runs?event=push" \ | jq -r '.workflow_runs[0].status') echo "CI status: $STATUS" [ "$STATUS" = "success" ] && break sleep 5 done fi echo "=== Release complete ==="