Skip to content

Release to Maven Central #5

Release to Maven Central

Release to Maven Central #5

# GitHub Action workflow for releasing artifacts to Maven Central
# This workflow is triggered manually or on tag creation and handles:
# - Version management from git tags
# - GPG signing of artifacts
# - Deployment to Maven Central via Sonatype
name: Release to Maven Central
on:
workflow_dispatch:
inputs:
release_version:
description: 'Release version (e.g., 0.2.0)'
required: true
type: string
jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'zulu'
cache: maven
server-id: central
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
gpg-private-key: ${{ secrets.GPG_SIGNING_KEY }}
gpg-passphrase: MAVEN_GPG_PASSPHRASE
- name: Determine version
id: version
run: |
VERSION="${{ github.event.inputs.release_version }}"
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
echo "Releasing version: $VERSION"
- name: Update POM versions
run: |
VERSION=${{ steps.version.outputs.VERSION }}
echo "Setting version to $VERSION"
# Update parent POM
mvn versions:set -DnewVersion=$VERSION -DgenerateBackupPoms=false
# Update module POMs
mvn versions:set -DnewVersion=$VERSION -DgenerateBackupPoms=false -pl core
mvn versions:set -DnewVersion=$VERSION -DgenerateBackupPoms=false -pl processor
- name: Commit version changes
id: commit
run: |
VERSION=${{ steps.version.outputs.VERSION }}
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git add pom.xml core/pom.xml processor/pom.xml
# Only commit if there are changes
if git diff --staged --quiet; then
echo "No version changes detected - version already set to $VERSION"
echo "has_changes=false" >> $GITHUB_OUTPUT
else
git commit -m "chore: release version $VERSION [skip ci]"
echo "has_changes=true" >> $GITHUB_OUTPUT
fi
- name: Create Git tag
run: |
VERSION=${{ steps.version.outputs.VERSION }}
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
# Only create tag if it doesn't exist
if git rev-parse "v$VERSION" >/dev/null 2>&1; then
echo "Tag v$VERSION already exists"
else
git tag -a "v$VERSION" -m "Release version $VERSION"
fi
- name: Build and verify
env:
MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_SIGNING_KEY_PASSWORD }}
run: mvn -B clean verify -Prelease
- name: Deploy to Maven Central
env:
MAVEN_USERNAME: ${{ secrets.CENTRAL_TOKEN_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.CENTRAL_TOKEN_PASSWORD }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_SIGNING_KEY_PASSWORD }}
run: |
mvn -B deploy -Prelease -pl core,processor -DskipTests=true -Dcentral.autoPublish=true
- name: Push changes to repository
if: success()
run: |
VERSION=${{ steps.version.outputs.VERSION }}
# Push commit if there were changes
if [ "${{ steps.commit.outputs.has_changes }}" = "true" ]; then
git push origin main
fi
# Push tag if it was newly created (check if tag exists remotely)
if ! git ls-remote --tags origin | grep -q "refs/tags/v$VERSION"; then
git push origin "v$VERSION"
else
echo "Tag v$VERSION already exists remotely"
fi
- name: Create GitHub Release
if: success()
env:
GH_TOKEN: ${{ github.token }}
run: |
VERSION=${{ steps.version.outputs.VERSION }}
# Check if release already exists
if gh release view "v$VERSION" >/dev/null 2>&1; then
echo "Release v$VERSION already exists"
else
PRERELEASE_FLAG=""
if [[ "$VERSION" == *"-"* ]]; then
PRERELEASE_FLAG="--prerelease"
fi
# Generate release notes and replace version placeholder
gh api repos/java-helpers/simple-builders/releases/generate-notes \
-f tag_name="v$VERSION" \
-f target_commitish="main" \
--jq '.body' > release_notes.md
# Replace version placeholder in generated notes
sed -i "s/\$RESOLVED_VERSION/$VERSION/g" release_notes.md
# Create draft release with custom notes and attach artifacts
gh release create "v$VERSION" \
--title "Release $VERSION" \
--draft \
--notes-file release_notes.md \
$PRERELEASE_FLAG \
core/target/simple-builders-core-${VERSION}.jar \
core/target/simple-builders-core-${VERSION}-sources.jar \
core/target/simple-builders-core-${VERSION}-javadoc.jar \
processor/target/simple-builders-processor-${VERSION}.jar \
processor/target/simple-builders-processor-${VERSION}-sources.jar \
processor/target/simple-builders-processor-${VERSION}-javadoc.jar
# Clean up
rm -f release_notes.md
fi