|
| 1 | +name: 'Bump version' |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + bump-type: |
| 7 | + type: choice |
| 8 | + options: |
| 9 | + - 'minor' |
| 10 | + - 'major' |
| 11 | + description: 'Version bump type. Use ''minor'' to add features (keeps API files), ''major'' for breaking changes (resets API files).' |
| 12 | + required: true |
| 13 | + |
| 14 | +permissions: |
| 15 | + actions: read |
| 16 | + contents: write |
| 17 | + pull-requests: write |
| 18 | + administration: write |
| 19 | + |
| 20 | +concurrency: |
| 21 | + group: bump-version |
| 22 | + cancel-in-progress: false |
| 23 | + |
| 24 | +env: |
| 25 | + dotnet-sdk-version: '10.x' |
| 26 | + |
| 27 | +jobs: |
| 28 | + detect-version: |
| 29 | + name: 'Detect current version and calculate next' |
| 30 | + runs-on: ubuntu-latest |
| 31 | + outputs: |
| 32 | + current-version: ${{ steps.detect.outputs.current-version }} |
| 33 | + next-version: ${{ steps.calculate.outputs.next-version }} |
| 34 | + target-branch: ${{ steps.calculate.outputs.target-branch }} |
| 35 | + steps: |
| 36 | + - name: 'Checkout main' |
| 37 | + uses: actions/checkout@v6 |
| 38 | + with: |
| 39 | + ref: main |
| 40 | + fetch-depth: 0 |
| 41 | + |
| 42 | + - name: 'Detect highest release branch' |
| 43 | + id: detect |
| 44 | + run: | |
| 45 | + git fetch origin |
| 46 | + latest=$(git ls-remote --heads origin 'release/*' | grep -oP 'release/\K\d+\.\d+' | sort -V | tail -1) |
| 47 | + if [[ -z "$latest" ]]; then |
| 48 | + latest="0.0" |
| 49 | + fi |
| 50 | + echo "Detected current version: $latest" |
| 51 | + echo "current-version=$latest" >> $GITHUB_OUTPUT |
| 52 | +
|
| 53 | + - name: 'Calculate next version' |
| 54 | + id: calculate |
| 55 | + run: | |
| 56 | + current="${{ steps.detect.outputs.current-version }}" |
| 57 | + major="${current%%.*}" |
| 58 | + minor="${current##*.}" |
| 59 | + if [[ "${{ inputs.bump-type }}" == "major" ]]; then |
| 60 | + next_major=$((major + 1)) |
| 61 | + next_version="${next_major}.0" |
| 62 | + else |
| 63 | + next_minor=$((minor + 1)) |
| 64 | + next_version="${major}.${next_minor}" |
| 65 | + fi |
| 66 | + echo "Next version: $next_version" |
| 67 | + echo "next-version=$next_version" >> $GITHUB_OUTPUT |
| 68 | + echo "target-branch=develop/${next_version}" >> $GITHUB_OUTPUT |
| 69 | +
|
| 70 | + validate: |
| 71 | + name: 'Validate bump' |
| 72 | + needs: [detect-version] |
| 73 | + runs-on: ubuntu-latest |
| 74 | + steps: |
| 75 | + - name: 'Checkout main' |
| 76 | + uses: actions/checkout@v6 |
| 77 | + with: |
| 78 | + ref: main |
| 79 | + fetch-depth: 1 |
| 80 | + |
| 81 | + - name: 'Validate workflow is running from main' |
| 82 | + if: ${{ github.ref_name != 'main' }} |
| 83 | + run: | |
| 84 | + echo "This workflow must be run from the 'main' branch. Current branch: '${{ github.ref_name }}'." |
| 85 | + exit 1 |
| 86 | +
|
| 87 | + - name: 'Validate target branch does not exist' |
| 88 | + run: | |
| 89 | + set +e |
| 90 | + git ls-remote --exit-code --heads origin "${{ needs.detect-version.outputs.target-branch }}" |
| 91 | + if [[ $? -eq 0 ]]; then |
| 92 | + echo "Target branch '${{ needs.detect-version.outputs.target-branch }}' already exists." |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + set -e |
| 96 | +
|
| 97 | + create-branch: |
| 98 | + name: 'Create ${{ needs.detect-version.outputs.target-branch }}' |
| 99 | + needs: [detect-version, validate] |
| 100 | + runs-on: ubuntu-latest |
| 101 | + env: |
| 102 | + next-version: ${{ needs.detect-version.outputs.next-version }} |
| 103 | + target-branch: ${{ needs.detect-version.outputs.target-branch }} |
| 104 | + steps: |
| 105 | + - name: 'Checkout main' |
| 106 | + uses: actions/checkout@v6 |
| 107 | + with: |
| 108 | + ref: main |
| 109 | + fetch-depth: 0 |
| 110 | + |
| 111 | + - name: 'Setup .NET ${{ env.dotnet-sdk-version }}' |
| 112 | + uses: actions/setup-dotnet@v5 |
| 113 | + with: |
| 114 | + dotnet-version: ${{ env.dotnet-sdk-version }} |
| 115 | + |
| 116 | + - name: 'Configure git' |
| 117 | + run: | |
| 118 | + git config user.name "$(git log -n 1 --pretty=format:%an)" |
| 119 | + git config user.email "$(git log -n 1 --pretty=format:%ae)" |
| 120 | +
|
| 121 | + - name: 'Create develop branch from main' |
| 122 | + run: | |
| 123 | + git checkout -b ${{ env.target-branch }} |
| 124 | +
|
| 125 | + - name: 'Reset PublicAPI files for major bump' |
| 126 | + if: ${{ inputs.bump-type == 'major' }} |
| 127 | + run: | |
| 128 | + find . \( -name "PublicAPI.Shipped.txt" -o -name "PublicAPI.Unshipped.txt" \) | while read file; do |
| 129 | + printf '\xef\xbb\xbf#nullable enable\n' > "$file" |
| 130 | + echo "Reset: $file" |
| 131 | + done |
| 132 | +
|
| 133 | + - name: 'Commit API file reset' |
| 134 | + if: ${{ inputs.bump-type == 'major' }} |
| 135 | + run: | |
| 136 | + git add . |
| 137 | + git diff --staged --quiet || git commit -m "Reset PublicAPI files for major version ${{ env.next-version }}" |
| 138 | +
|
| 139 | + - name: 'Push develop branch' |
| 140 | + run: | |
| 141 | + git push --set-upstream origin ${{ env.target-branch }} |
| 142 | +
|
| 143 | + - name: 'Write summary' |
| 144 | + run: | |
| 145 | + echo "## ✅ Branch created: \`${{ env.target-branch }}\`" >> $GITHUB_STEP_SUMMARY |
| 146 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 147 | + echo "| | |" >> $GITHUB_STEP_SUMMARY |
| 148 | + echo "|---|---|" >> $GITHUB_STEP_SUMMARY |
| 149 | + echo "| **Bump type** | ${{ inputs.bump-type }} |" >> $GITHUB_STEP_SUMMARY |
| 150 | + echo "| **Previous version** | ${{ needs.detect-version.outputs.current-version }} |" >> $GITHUB_STEP_SUMMARY |
| 151 | + echo "| **New version** | ${{ env.next-version }} |" >> $GITHUB_STEP_SUMMARY |
| 152 | + echo "| **Target branch** | \`${{ env.target-branch }}\` |" >> $GITHUB_STEP_SUMMARY |
| 153 | + if [[ "${{ inputs.bump-type }}" == "major" ]]; then |
| 154 | + echo "| **API files** | Reset (major bump) |" >> $GITHUB_STEP_SUMMARY |
| 155 | + else |
| 156 | + echo "| **API files** | Preserved (minor bump) |" >> $GITHUB_STEP_SUMMARY |
| 157 | + fi |
0 commit comments