|
| 1 | +# |
| 2 | +# Copyright 2018 ABSA Group Limited |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | + |
| 16 | +name: Create a new release |
| 17 | + |
| 18 | +on: |
| 19 | + workflow_dispatch: |
| 20 | + inputs: |
| 21 | + release-version: |
| 22 | + description: "Define the release version (e.g., v0.5.2)" |
| 23 | + required: true |
| 24 | + default: "" |
| 25 | + development-version: |
| 26 | + description: "Define the snapshot version (e.g., v0.5.2)" |
| 27 | + required: true |
| 28 | + default: "" |
| 29 | + |
| 30 | +jobs: |
| 31 | + create-release: |
| 32 | + runs-on: ubuntu-latest |
| 33 | + steps: |
| 34 | + - name: Checkout repository |
| 35 | + uses: actions/checkout@v3 |
| 36 | + with: |
| 37 | + fetch-depth: 0 # Fetch all history for all tags and branches |
| 38 | + |
| 39 | + - name: Validate format of received tag |
| 40 | + uses: actions/github-script@v7 |
| 41 | + with: |
| 42 | + script: | |
| 43 | + const releaseVersion = core.getInput('release-version'); |
| 44 | + const developmentVersion = core.getInput('development-version'); |
| 45 | + const regex = /^v[0-9]+\.[0-9]+\.[0-9]+$/; |
| 46 | +
|
| 47 | + if (!regex.test(releaseVersion)) { |
| 48 | + core.setFailed('Release version does not match the required format "v[0-9]+.[0-9]+.[0-9]+". Valid example: v0.5.2'); |
| 49 | + return; |
| 50 | + } |
| 51 | + if (!regex.test(developmentVersion)) { |
| 52 | + core.setFailed('Development version does not match the required format "v[0-9]+.[0-9]+.[0-9]+". Valid example: v0.5.2'); |
| 53 | + return; |
| 54 | + } |
| 55 | + release-version: ${{ github.event.inputs.release-version }} |
| 56 | + development-version: ${{ github.event.inputs.development-version }} |
| 57 | + |
| 58 | + - name: Define semantic version number |
| 59 | + id: semantic_version |
| 60 | + run: | |
| 61 | + RELEASE_VERSION="${{ github.event.inputs.release-version }}" |
| 62 | + RELEASE_VERSION_WITHOUT_V=${RELEASE_VERSION#v} |
| 63 | + |
| 64 | + DEVELOPMENT_VERSION="${{ github.event.inputs.development-version }}" |
| 65 | + DEVELOPMENT_VERSION_WITHOUT_V=${DEVELOPMENT_VERSION#v} |
| 66 | + |
| 67 | + echo "RELEASE_VERSION_WITHOUT_V=${RELEASE_VERSION_WITHOUT_V}" >> "${GITHUB_ENV}" |
| 68 | + echo "DEVELOPMENT_VERSION_WITHOUT_V=${DEVELOPMENT_VERSION_WITHOUT_V}" >> "${GITHUB_ENV}" |
| 69 | +
|
| 70 | + - name: Configure Git |
| 71 | + run: | |
| 72 | + git config --global user.name "${{ github.actor }}" |
| 73 | + git config --global user.email "${{ github.actor }}@users.noreply.github.com" |
| 74 | +
|
| 75 | + - name: Create release branch |
| 76 | + run: | |
| 77 | + git checkout -b release/${RELEASE_VERSION_WITHOUT_V} |
| 78 | + git push origin release/${RELEASE_VERSION_WITHOUT_V} |
| 79 | +
|
| 80 | + - name: Set up JDK 1.8 |
| 81 | + uses: actions/setup-java@v1 |
| 82 | + with: |
| 83 | + java-version: '1.8' |
| 84 | + |
| 85 | + - name: Set Maven project version, create tag |
| 86 | + run: | |
| 87 | + git pull origin release/${RELEASE_VERSION_WITHOUT_V} |
| 88 | + mvn versions:set -DnewVersion=${RELEASE_VERSION_WITHOUT_V} --no-transfer-progress |
| 89 | + mvn versions:commit |
| 90 | + git commit -am "Set project version to ${RELEASE_VERSION_WITHOUT_V}" |
| 91 | + git push origin release/${RELEASE_VERSION_WITHOUT_V} |
| 92 | +
|
| 93 | + - name: Create tag |
| 94 | + run: | |
| 95 | + git tag ${{ github.event.inputs.release-version }} |
| 96 | + git push origin ${{ github.event.inputs.release-version }} |
| 97 | +
|
| 98 | + - name: Set next development version |
| 99 | + run: | |
| 100 | + # Assumes semantic versioning and increments the minor version. Adjust the awk command as needed for different versioning schemes. |
| 101 | + NEXT_VERSION=$(echo ${DEVELOPMENT_VERSION_WITHOUT_V} | awk -F. '{print $1"."$2"."$3"-SNAPSHOT"}') |
| 102 | + mvn versions:set -DnewVersion=$NEXT_VERSION --no-transfer-progress |
| 103 | + mvn versions:commit |
| 104 | + git commit -am "Set project version to $NEXT_VERSION" |
| 105 | + git push origin release/${RELEASE_VERSION_WITHOUT_V} |
| 106 | +
|
| 107 | + - name: Get Default Branch |
| 108 | + id: get_default_branch |
| 109 | + uses: actions/github-script@v7 |
| 110 | + with: |
| 111 | + script: | |
| 112 | + const response = await github.rest.repos.get({ |
| 113 | + owner: context.repo.owner, |
| 114 | + repo: context.repo.repo |
| 115 | + }); |
| 116 | + return response.data.default_branch; |
| 117 | +
|
| 118 | + - name: Create Pull Request |
| 119 | + uses: actions/github-script@v7 |
| 120 | + with: |
| 121 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 122 | + script: | |
| 123 | + const branch = `release/${process.env.RELEASE_VERSION_WITHOUT_V}`; |
| 124 | + await github.rest.pulls.create({ |
| 125 | + owner: context.repo.owner, |
| 126 | + repo: context.repo.repo, |
| 127 | + title: 'Release ${{ github.event.inputs.release-version }}', |
| 128 | + head: branch, |
| 129 | + base: ${{ steps.get_default_branch.outputs.result }}, |
| 130 | + body: 'Pull request to merge release ${{ github.event.inputs.release-version }} into default branch', |
| 131 | + draft: false |
| 132 | + }); |
| 133 | +
|
| 134 | + - name: Create Draft Release |
| 135 | + uses: softprops/action-gh-release@v1 |
| 136 | + env: |
| 137 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 138 | + with: |
| 139 | + name: ${{ github.event.inputs.release-version }} |
| 140 | + body: ${{ steps.generate_release_notes.outputs.releaseNotes }} |
| 141 | + tag_name: ${{ github.event.inputs.release-version }} |
| 142 | + draft: true |
| 143 | + prerelease: false |
0 commit comments