|
| 1 | +# This workflow automates the release process when a release is created on GitHub |
| 2 | +# It creates a release branch, updates versions, and deploys to Maven Central |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# 1. Create a new release in GitHub UI with tag format: vX.Y.Z (e.g., v1.2.0) |
| 6 | +# 2. This workflow will automatically: |
| 7 | +# - Create a release-X.Y.Z branch |
| 8 | +# - Update pom.xml versions to release version |
| 9 | +# - Build and deploy artifacts to Maven Central |
| 10 | +# - Update pom.xml to next development version |
| 11 | +# - Push version commits back to the release branch |
| 12 | + |
| 13 | +name: Automated Release |
| 14 | + |
| 15 | +on: |
| 16 | + release: |
| 17 | + types: [created] # Trigger when release is created (before it's published) |
| 18 | + |
| 19 | +permissions: |
| 20 | + contents: write # Required to create branches and push commits |
| 21 | + |
| 22 | +jobs: |
| 23 | + release: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Validate release tag format |
| 28 | + id: validate_tag |
| 29 | + run: | |
| 30 | + TAG_NAME="${{ github.event.release.tag_name }}" |
| 31 | + echo "Release tag: $TAG_NAME" |
| 32 | + |
| 33 | + # Validate tag format (should be vX.Y.Z) |
| 34 | + if [[ ! "$TAG_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 35 | + echo "::error::Invalid tag format. Expected format: vX.Y.Z (e.g., v1.2.0)" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + |
| 39 | + # Remove 'v' prefix to get the version number |
| 40 | + VERSION="${TAG_NAME#v}" |
| 41 | + echo "version=${VERSION}" >> $GITHUB_OUTPUT |
| 42 | + echo "Extracted version: $VERSION" |
| 43 | + |
| 44 | + # Create release branch name |
| 45 | + RELEASE_BRANCH="release-${VERSION}" |
| 46 | + echo "release_branch=${RELEASE_BRANCH}" >> $GITHUB_OUTPUT |
| 47 | + echo "Release branch: $RELEASE_BRANCH" |
| 48 | + |
| 49 | + # Calculate next development version (increment patch version) |
| 50 | + IFS='.' read -ra VERSION_PARTS <<< "$VERSION" |
| 51 | + MAJOR="${VERSION_PARTS[0]}" |
| 52 | + MINOR="${VERSION_PARTS[1]}" |
| 53 | + PATCH="${VERSION_PARTS[2]}" |
| 54 | + NEXT_PATCH=$((PATCH + 1)) |
| 55 | + NEXT_VERSION="${MAJOR}.${MINOR}.${NEXT_PATCH}-SNAPSHOT" |
| 56 | + echo "next_version=${NEXT_VERSION}" >> $GITHUB_OUTPUT |
| 57 | + echo "Next development version: $NEXT_VERSION" |
| 58 | + |
| 59 | + - name: Checkout repository at release tag |
| 60 | + uses: actions/checkout@v3 |
| 61 | + with: |
| 62 | + ref: ${{ github.event.release.tag_name }} |
| 63 | + fetch-depth: 0 |
| 64 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 65 | + |
| 66 | + - name: Create release branch |
| 67 | + run: | |
| 68 | + RELEASE_BRANCH="${{ steps.validate_tag.outputs.release_branch }}" |
| 69 | + |
| 70 | + # Create new branch from the release tag |
| 71 | + git checkout -b "$RELEASE_BRANCH" |
| 72 | + echo "Created branch $RELEASE_BRANCH from tag ${{ github.event.release.tag_name }}" |
| 73 | + |
| 74 | + - name: Set up JDK 17 |
| 75 | + uses: actions/setup-java@v5 |
| 76 | + with: |
| 77 | + java-version: "17" |
| 78 | + distribution: "temurin" |
| 79 | + cache: maven |
| 80 | + server-id: central |
| 81 | + server-username: MAVEN_USERNAME |
| 82 | + server-password: MAVEN_PASSWORD |
| 83 | + gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} |
| 84 | + gpg-passphrase: MAVEN_GPG_PASSPHRASE |
| 85 | + |
| 86 | + - name: Configure Git |
| 87 | + run: | |
| 88 | + git config user.name "github-actions[bot]" |
| 89 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 90 | + |
| 91 | + - name: Set release version in POM |
| 92 | + run: | |
| 93 | + VERSION="${{ steps.validate_tag.outputs.version }}" |
| 94 | + echo "Setting version to: $VERSION" |
| 95 | + |
| 96 | + # Use Maven versions plugin to set the version |
| 97 | + ./mvnw -B versions:set -DnewVersion="${VERSION}" -DgenerateBackupPoms=false |
| 98 | + |
| 99 | + # Commit the version change |
| 100 | + git add . |
| 101 | + git commit -m "[maven-release-plugin] prepare release v${VERSION}" |
| 102 | + |
| 103 | + - name: Build and deploy release |
| 104 | + run: | |
| 105 | + VERSION="${{ steps.validate_tag.outputs.version }}" |
| 106 | + echo "Building and deploying version: $VERSION" |
| 107 | + |
| 108 | + # Build and deploy with release profile (includes signing, sources, javadocs) |
| 109 | + ./mvnw -B clean deploy -Prelease \ |
| 110 | + -pl xapi-model,xapi-client,xapi-model-spring-boot-starter -am \ |
| 111 | + -DskipTests |
| 112 | + env: |
| 113 | + MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} |
| 114 | + MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} |
| 115 | + MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} |
| 116 | + |
| 117 | + - name: Update to next development version |
| 118 | + run: | |
| 119 | + NEXT_VERSION="${{ steps.validate_tag.outputs.next_version }}" |
| 120 | + echo "Setting next development version to: $NEXT_VERSION" |
| 121 | + |
| 122 | + # Update to next SNAPSHOT version |
| 123 | + ./mvnw -B versions:set -DnewVersion="${NEXT_VERSION}" -DgenerateBackupPoms=false |
| 124 | + |
| 125 | + # Commit the version change |
| 126 | + git add . |
| 127 | + git commit -m "[maven-release-plugin] prepare for next development iteration" |
| 128 | + |
| 129 | + - name: Update release tag to point to release commit |
| 130 | + run: | |
| 131 | + TAG_NAME="${{ github.event.release.tag_name }}" |
| 132 | + |
| 133 | + # Delete the existing tag locally |
| 134 | + git tag -d "$TAG_NAME" |
| 135 | + |
| 136 | + # Get the commit hash of the release preparation commit (one before current) |
| 137 | + RELEASE_COMMIT=$(git rev-parse HEAD~1) |
| 138 | + |
| 139 | + # Create new tag pointing to the release commit |
| 140 | + git tag -a "$TAG_NAME" "$RELEASE_COMMIT" -m "Release $TAG_NAME" |
| 141 | + |
| 142 | + # Force push the updated tag |
| 143 | + git push -f origin "$TAG_NAME" |
| 144 | + |
| 145 | + echo "Updated tag $TAG_NAME to point to release commit: $RELEASE_COMMIT" |
| 146 | + |
| 147 | + - name: Push release branch |
| 148 | + run: | |
| 149 | + RELEASE_BRANCH="${{ steps.validate_tag.outputs.release_branch }}" |
| 150 | + |
| 151 | + # Push the release branch with all commits |
| 152 | + git push -u origin "$RELEASE_BRANCH" |
| 153 | + |
| 154 | + echo "Pushed release branch: $RELEASE_BRANCH" |
| 155 | + |
| 156 | + - name: Clean up |
| 157 | + if: always() |
| 158 | + run: | |
| 159 | + # Clean up any Maven artifacts |
| 160 | + rm -f release.properties |
| 161 | + find . -name "pom.xml.versionsBackup" -delete || true |
0 commit comments