|
| 1 | +name: Tag Major Version |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: [v*.*.*] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + update-major-version-tag: |
| 12 | + name: Update major version tag |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout repository |
| 16 | + uses: actions/checkout@v4 |
| 17 | + |
| 18 | + - name: Get major version |
| 19 | + id: major-version |
| 20 | + run: | |
| 21 | + VERSION=${GITHUB_REF#refs/tags/} |
| 22 | + MAJOR=${VERSION%%.*} |
| 23 | + echo "sha=$(git rev-list -n 1 $VERSION)" >> $GITHUB_OUTPUT |
| 24 | + echo "major=$MAJOR" >> $GITHUB_OUTPUT |
| 25 | +
|
| 26 | + - name: Find major tag |
| 27 | + id: major-tag |
| 28 | + uses: actions/github-script@v7 |
| 29 | + with: |
| 30 | + result-encoding: string |
| 31 | + script: | |
| 32 | + try { |
| 33 | + await github.rest.git.getRef({ |
| 34 | + ...context.repo, |
| 35 | + ref: "tags/${{ steps.major-version.outputs.major }}" |
| 36 | + }); |
| 37 | + return "1"; |
| 38 | + } |
| 39 | + catch (err) { |
| 40 | + if (err.status === 404) |
| 41 | + return "0"; |
| 42 | + throw err; |
| 43 | + } |
| 44 | +
|
| 45 | + - name: Create major version tag |
| 46 | + if: steps.major-tag.outputs.result == '0' |
| 47 | + uses: actions/github-script@v7 |
| 48 | + with: |
| 49 | + script: | |
| 50 | + github.rest.git.createRef({ |
| 51 | + ...context.repo, |
| 52 | + ref: "refs/tags/${{ steps.major-version.outputs.major }}", |
| 53 | + sha: "${{ steps.major-version.outputs.sha }}" |
| 54 | + }) |
| 55 | +
|
| 56 | + - name: Move major version tag |
| 57 | + if: steps.major-tag.outputs.result == '1' |
| 58 | + uses: actions/github-script@v7 |
| 59 | + with: |
| 60 | + script: | |
| 61 | + github.rest.git.updateRef({ |
| 62 | + ...context.repo, |
| 63 | + ref: "tags/${{ steps.major-version.outputs.major }}", |
| 64 | + sha: "${{ steps.major-version.outputs.sha }}", |
| 65 | + force: true |
| 66 | + }) |
0 commit comments