|
| 1 | +name: Reusable gate |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + tag: |
| 7 | + required: false |
| 8 | + type: boolean |
| 9 | + default: false |
| 10 | + |
| 11 | +env: |
| 12 | + BRANCH_NAME: ${{ github.head_ref || github.ref_name }} |
| 13 | + |
| 14 | +jobs: |
| 15 | + validate_release: |
| 16 | + if: ${{ (github.head_ref || github.ref_name) == 'main' }} |
| 17 | + runs-on: ubuntu-latest |
| 18 | + outputs: |
| 19 | + next_tag: ${{ steps.validate.outputs.next_tag }} |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Retrieve repository |
| 23 | + uses: actions/checkout@v6 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Validate last RC tag |
| 28 | + id: validate |
| 29 | + run: | |
| 30 | + # validate last rc tag |
| 31 | + last_rc=$(git describe --tags --match "*rc*" --abbrev=0) |
| 32 | +
|
| 33 | + if [[ -z "${last_rc}" ]]; then |
| 34 | + echo "No RC tags found" |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | +
|
| 38 | + if [[ "${last_rc}" =~ ^([0-9]+\.[0-9]+\.[0-9]+)rc([0-9]+)$ ]]; then |
| 39 | + rc_base="${BASH_REMATCH[1]}" |
| 40 | + else |
| 41 | + echo "Could not parse RC tag: ${last_rc}" |
| 42 | + exit 2 |
| 43 | + fi |
| 44 | +
|
| 45 | + if [[ -n "$(git tag --list "${rc_base}")" ]]; then |
| 46 | + echo "Stable tag ${rc_base} already exists." |
| 47 | + exit 3 |
| 48 | + fi |
| 49 | +
|
| 50 | + echo "Last RC tag: ${last_rc}" |
| 51 | + echo "RC base version: ${rc_base}" |
| 52 | +
|
| 53 | + echo "next_tag=${rc_base}" >> "$GITHUB_OUTPUT" |
| 54 | +
|
| 55 | + validate_candidate: |
| 56 | + if: ${{ startsWith(github.head_ref || github.ref_name, 'release/') }} |
| 57 | + runs-on: ubuntu-latest |
| 58 | + outputs: |
| 59 | + next_tag: ${{ steps.validate.outputs.next_tag }} |
| 60 | + |
| 61 | + steps: |
| 62 | + - name: Retrieve repository |
| 63 | + uses: actions/checkout@v6 |
| 64 | + with: |
| 65 | + fetch-depth: 0 |
| 66 | + |
| 67 | + - name: Validate branch name |
| 68 | + id: validate |
| 69 | + run: | |
| 70 | + # strictly validating the format of branch name |
| 71 | + if [[ ! "${BRANCH_NAME}" =~ ^release/(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; then |
| 72 | + echo "Invalid branch '${BRANCH_NAME}'. Expected 'release/X.Y.Z' (e.g. release/1.2.3)" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | +
|
| 76 | + new_major="${BASH_REMATCH[1]}" |
| 77 | + new_minor="${BASH_REMATCH[2]}" |
| 78 | + new_patch="${BASH_REMATCH[3]}" |
| 79 | + new_version="${new_major}.${new_minor}.${new_patch}" |
| 80 | +
|
| 81 | + highest_tag=$(git tag --list | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n1) |
| 82 | +
|
| 83 | + if [[ -z "${highest_tag}" ]]; then |
| 84 | + echo "No valid semantic version tags found" |
| 85 | + exit 2 |
| 86 | + fi |
| 87 | +
|
| 88 | + IFS='.' read -r old_major old_minor old_patch <<< "${highest_tag}" |
| 89 | +
|
| 90 | + allowed_patch="${old_major}.${old_minor}.$((old_patch + 1))" |
| 91 | + allowed_minor="${old_major}.$((old_minor + 1)).0" |
| 92 | + allowed_major="$((old_major + 1)).0.0" |
| 93 | +
|
| 94 | + if [[ "${new_version}" != "${allowed_patch}" && \ |
| 95 | + "${new_version}" != "${allowed_minor}" && \ |
| 96 | + "${new_version}" != "${allowed_major}" ]]; then |
| 97 | + echo "Release version '${new_version}' is not exactly one valid increment from highest existing stable tag '${highest_tag}'." |
| 98 | + echo "Allowed next versions are: ${allowed_patch}, ${allowed_minor}, ${allowed_major}." |
| 99 | + exit 3 |
| 100 | + fi |
| 101 | +
|
| 102 | + echo "Highest existing stable tag: ${highest_tag}" |
| 103 | + echo "Validated new release version: ${new_version}" |
| 104 | +
|
| 105 | + last_rc=$(git tag --list "${new_version}rc*" | sort -V | tail -n1) |
| 106 | + if [[ -z "${last_rc}" ]]; then |
| 107 | + next_tag="${new_version}rc1" |
| 108 | + elif [[ "${last_rc}" =~ ^${new_version}rc([0-9]+)$ ]]; then |
| 109 | + rc="${BASH_REMATCH[1]}" |
| 110 | + next_tag="${new_version}rc$((rc+1))" |
| 111 | + else |
| 112 | + echo "Found matching tag candidate but could not parse RC number: ${last_rc}" |
| 113 | + exit 4 |
| 114 | + fi |
| 115 | +
|
| 116 | + echo "Next RC tag: ${next_tag}" |
| 117 | + echo "next_tag=${next_tag}" >> "$GITHUB_OUTPUT" |
| 118 | +
|
| 119 | + push_tag: |
| 120 | + needs: [validate_release, validate_candidate] |
| 121 | + if: | |
| 122 | + always() && |
| 123 | + inputs.tag && |
| 124 | + ( |
| 125 | + needs.validate_release.outputs.next_tag != '' || |
| 126 | + needs.validate_candidate.outputs.next_tag != '' |
| 127 | + ) |
| 128 | +
|
| 129 | + runs-on: ubuntu-latest |
| 130 | + |
| 131 | + env: |
| 132 | + NEXT_TAG: >- |
| 133 | + ${{ needs.validate_release.outputs.next_tag || |
| 134 | + needs.validate_candidate.outputs.next_tag }} |
| 135 | + |
| 136 | + steps: |
| 137 | + - name: Retrieve repository |
| 138 | + uses: actions/checkout@v6 |
| 139 | + with: |
| 140 | + fetch-depth: 0 |
| 141 | + |
| 142 | + - name: Push tag |
| 143 | + run: | |
| 144 | + # pushing the tag |
| 145 | + git config user.name "github-actions" |
| 146 | + git config user.email "github-actions@github.com" |
| 147 | +
|
| 148 | + git tag -a "${NEXT_TAG}" -m "tag ${NEXT_TAG}" |
| 149 | + git push origin "${NEXT_TAG}" |
0 commit comments