|
| 1 | +# Release-affecting changes under src/main/ or pom.xml require pom.xml + changelog.md bumps |
| 2 | +# aligned with the latest tag. Skips when only tests, .github, skills, or docs change. |
| 3 | + |
| 4 | +name: Check Version Bump |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + |
| 9 | +jobs: |
| 10 | + version-bump: |
| 11 | + name: Version & changelog bump |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 |
| 18 | + |
| 19 | + - name: Detect changed files |
| 20 | + id: detect |
| 21 | + run: | |
| 22 | + FILES=$(git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}") |
| 23 | + echo "Changed files:" |
| 24 | + echo "$FILES" |
| 25 | +
|
| 26 | + CODE_CHANGED=false |
| 27 | + while IFS= read -r f; do |
| 28 | + [ -z "$f" ] && continue |
| 29 | + if [[ "$f" == src/main/* ]] || [[ "$f" == "pom.xml" ]]; then |
| 30 | + CODE_CHANGED=true |
| 31 | + break |
| 32 | + fi |
| 33 | + done <<< "$FILES" |
| 34 | +
|
| 35 | + POM_CHANGED=false |
| 36 | + CHANGELOG_CHANGED=false |
| 37 | + echo "$FILES" | grep -qx 'pom.xml' && POM_CHANGED=true |
| 38 | + echo "$FILES" | grep -qx 'changelog.md' && CHANGELOG_CHANGED=true |
| 39 | +
|
| 40 | + VERSION_FILES_OK=false |
| 41 | + if [ "$POM_CHANGED" = true ] && [ "$CHANGELOG_CHANGED" = true ]; then |
| 42 | + VERSION_FILES_OK=true |
| 43 | + fi |
| 44 | +
|
| 45 | + echo "code_changed=$CODE_CHANGED" >> "$GITHUB_OUTPUT" |
| 46 | + echo "version_files_ok=$VERSION_FILES_OK" >> "$GITHUB_OUTPUT" |
| 47 | +
|
| 48 | + - name: Skip when no release-affecting code changed |
| 49 | + if: steps.detect.outputs.code_changed != 'true' |
| 50 | + run: | |
| 51 | + echo "No src/main or pom-only release path triggered (e.g. tests/docs/.github only). Skipping version-bump check." |
| 52 | + exit 0 |
| 53 | +
|
| 54 | + - name: Fail when version bump files were not both updated |
| 55 | + if: steps.detect.outputs.code_changed == 'true' && steps.detect.outputs.version_files_ok != 'true' |
| 56 | + run: | |
| 57 | + echo "::error::This PR changes release-affecting code but pom.xml and/or changelog.md were not both updated. Bump <version> in pom.xml and add a ## vX.Y.Z section in changelog.md." |
| 58 | + exit 1 |
| 59 | +
|
| 60 | + - name: Validate version vs latest tag and changelog header |
| 61 | + if: steps.detect.outputs.code_changed == 'true' && steps.detect.outputs.version_files_ok == 'true' |
| 62 | + run: | |
| 63 | + set -euo pipefail |
| 64 | + POM_VERSION=$(python3 <<'PY' |
| 65 | + import xml.etree.ElementTree as ET |
| 66 | + root = ET.parse("pom.xml").getroot() |
| 67 | + ns = {"m": "http://maven.apache.org/POM/4.0.0"} |
| 68 | + el = root.find("m:version", ns) |
| 69 | + if el is None or not (el.text or "").strip(): |
| 70 | + raise SystemExit("Could not read project version from pom.xml") |
| 71 | + print(el.text.strip()) |
| 72 | + PY |
| 73 | + ) |
| 74 | +
|
| 75 | + git fetch --tags --force 2>/dev/null || true |
| 76 | + LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || true) |
| 77 | + if [ -z "$LATEST_TAG" ]; then |
| 78 | + echo "No existing tags found. Skipping semver vs tag check (first release)." |
| 79 | + CHANGELOG_HEAD=$(sed -nE 's/^## v?([0-9]+\.[0-9]+\.[0-9]+).*/\1/p' changelog.md | head -1) |
| 80 | + if [ -z "$CHANGELOG_HEAD" ]; then |
| 81 | + echo "::error::Could not find a ## vX.Y.Z entry at the top of changelog.md." |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + if [ "$CHANGELOG_HEAD" != "$POM_VERSION" ]; then |
| 85 | + echo "::error::changelog.md top version ($CHANGELOG_HEAD) does not match pom.xml version ($POM_VERSION)." |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | + exit 0 |
| 89 | + fi |
| 90 | +
|
| 91 | + LATEST_VERSION="${LATEST_TAG#v}" |
| 92 | + LATEST_VERSION="${LATEST_VERSION%%-*}" |
| 93 | + if [ "$(printf '%s\n' "$LATEST_VERSION" "$POM_VERSION" | sort -V | tail -1)" != "$POM_VERSION" ]; then |
| 94 | + echo "::error::pom.xml version ($POM_VERSION) must be greater than latest tag ($LATEST_TAG)." |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | + if [ "$POM_VERSION" = "$LATEST_VERSION" ]; then |
| 98 | + echo "::error::pom.xml version ($POM_VERSION) must be strictly greater than latest tag version ($LATEST_VERSION)." |
| 99 | + exit 1 |
| 100 | + fi |
| 101 | +
|
| 102 | + CHANGELOG_HEAD=$(sed -nE 's/^## v?([0-9]+\.[0-9]+\.[0-9]+).*/\1/p' changelog.md | head -1) |
| 103 | + if [ -z "$CHANGELOG_HEAD" ]; then |
| 104 | + echo "::error::Could not find a ## vX.Y.Z entry at the top of changelog.md." |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | + if [ "$CHANGELOG_HEAD" != "$POM_VERSION" ]; then |
| 108 | + echo "::error::changelog.md top version ($CHANGELOG_HEAD) does not match pom.xml version ($POM_VERSION)." |
| 109 | + exit 1 |
| 110 | + fi |
| 111 | + echo "Version bump check passed: pom.xml and changelog.md at $POM_VERSION (latest tag: $LATEST_TAG)." |
0 commit comments