Skip to content

Merge pull request #29 from openpatch/changeset #30

Merge pull request #29 from openpatch/changeset

Merge pull request #29 from openpatch/changeset #30

Workflow file for this run

name: Version Bump
on:
push:
branches:
- main
paths:
- ".changeset/**"
- '!.changeset/README.md'
jobs:
bump-version:
runs-on: ubuntu-latest
permissions:
contents: write # needed to commit the version bump and push the branch
pull-requests: write # needed to open the release PR
steps:
- name: Checkout repository
uses: actions/checkout@v7
- name: Set up Java
uses: actions/setup-java@v5
with:
distribution: temurin
java-version: 17
- name: Detect change types and build PR body
id: detect
run: |
FILES=$(find .changeset -maxdepth 1 -type f ! -name README.md)
if [ -z "$FILES" ]; then
echo "No changeset files found"
echo "found=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "found=true" >> "$GITHUB_OUTPUT"
# Initialize variables
TYPE_PRIORITY=0
BODY=""
for FILE in $FILES; do
echo "Processing $FILE"
FILE_TYPE=$(grep '^type:' "$FILE" | awk '{print $2}')
case "$FILE_TYPE" in
patch) PRIORITY=1 ;;
minor) PRIORITY=2 ;;
major) PRIORITY=3 ;;
*) echo "Unknown type: $FILE_TYPE"; exit 1 ;;
esac
if [ $PRIORITY -gt $TYPE_PRIORITY ]; then
TYPE_PRIORITY=$PRIORITY
TYPE=$FILE_TYPE
fi
CONTENT=$(sed '/^type:/d' "$FILE" | sed '/^---$/d')
BODY="$BODY\n\n$CONTENT"
done
echo "TYPE=$TYPE" >> $GITHUB_ENV
echo "BODY<<EOF" >> $GITHUB_ENV
echo -e "$BODY" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "Final detected type: $TYPE"
echo "PR body:"
echo -e "$BODY"
VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
# Split the version by dots
IFS='.' read -r major minor patch <<< "$VERSION"
case "$TYPE" in
patch)
new_version="$major.$minor.$((patch+1))"
mvn versions:set -DnewVersion="$new_version" -DgenerateBackupPoms=false
;;
minor)
new_version="$major.$((minor+1)).0"
mvn versions:set -DnewVersion="$new_version" -DgenerateBackupPoms=false
;;
major)
new_version="$((major+1)).0.0"
mvn versions:set -DnewVersion="$new_version" -DgenerateBackupPoms=false
;;
*)
echo "Unknown type: $TYPE"; exit 1
;;
esac
mvn versions:commit
- name: Get new project version
id: get-version
if: steps.detect.outputs.found == 'true'
run: |
VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "New version: $VERSION"
- name: Update CHANGELOG.md
if: steps.detect.outputs.found == 'true'
run: |
ENTRY_FILE=$(mktemp)
printf "## %s\n%s\n\n" "$VERSION" "$BODY" > "$ENTRY_FILE"
awk -v newfile="$ENTRY_FILE" '
BEGIN { fm=0; done=0 }
{
print
if ($0 == "---") {
fm++
if (fm == 2 && !done) {
print ""
while ((getline line < newfile) > 0) print line
done=1
}
}
}
' CHANGELOG.md > /tmp/CHANGELOG.new
mv /tmp/CHANGELOG.new CHANGELOG.md
# bump the index in the frontmatter to match the new entry count
sed -i "0,/^index: [0-9]*/s//index: $(( $(grep -c '^## ' CHANGELOG.md) ))/" CHANGELOG.md
- name: Delete changeset files
if: steps.detect.outputs.found == 'true'
run: |
find .changeset -type f ! -name 'README.md' -delete
echo "Removed processed changeset files."
- name: Create Pull Request
if: steps.detect.outputs.found == 'true'
uses: peter-evans/create-pull-request@v8
with:
base: main
commit-message: "chore: release v${{ env.VERSION }}"
branch: changeset
title: "Release v${{ env.VERSION }}"
body: ${{ env.BODY }}
labels: version