|
| 1 | +--- |
| 2 | +name: _version |
| 3 | +run-name: Calculate Version |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_call: |
| 7 | + inputs: |
| 8 | + is-release: |
| 9 | + type: boolean |
| 10 | + required: true |
| 11 | + outputs: |
| 12 | + version: |
| 13 | + description: "version to be built" |
| 14 | + value: ${{ jobs.version.outputs.version }} |
| 15 | + |
| 16 | +jobs: |
| 17 | + version: |
| 18 | + name: Calculate Version |
| 19 | + runs-on: ubuntu-22.04 |
| 20 | + outputs: |
| 21 | + version: ${{ steps.calculate.outputs.version }} |
| 22 | + steps: |
| 23 | + - name: Checkout Repo |
| 24 | + uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Get PR ID |
| 29 | + id: pr |
| 30 | + env: |
| 31 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 32 | + run: | |
| 33 | + commit_message=$( |
| 34 | + curl -s -L \ |
| 35 | + -H "Accept: application/vnd.github+json" \ |
| 36 | + -H "Authorization: Bearer $GH_TOKEN" \ |
| 37 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 38 | + https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }} | \ |
| 39 | + jq -r ".commit.message" |
| 40 | + ) |
| 41 | + ID=$(echo "$commit_message" | head -1 | grep -o "(#.*)" | grep -o "[0-9]*") |
| 42 | + echo "id=$ID" >> $GITHUB_OUTPUT |
| 43 | +
|
| 44 | + - name: Get version bump type |
| 45 | + if: ${{ inputs.is-release }} |
| 46 | + id: bump-type |
| 47 | + env: |
| 48 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 49 | + PR_NUMBER: ${{ steps.pr.outputs.id }} |
| 50 | + run: | |
| 51 | + version_tag=$( |
| 52 | + curl -s -L \ |
| 53 | + -H "Accept: application/vnd.github+json" \ |
| 54 | + -H "Authorization: Bearer $GH_TOKEN" \ |
| 55 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 56 | + https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/labels | \ |
| 57 | + jq -r ".[].name" | grep "version" |
| 58 | + ) |
| 59 | +
|
| 60 | + # Single Version label Enforcement (should go in CI...) |
| 61 | + if [[ $(echo $version_tag | wc -w) -gt 1 ]]; then |
| 62 | + echo "[!] multiple version labels found!" |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | +
|
| 66 | + version_type=$(echo $version_tag | cut -d ":" -f 2) |
| 67 | + echo "Version Bump Type: $version_type" |
| 68 | + echo "type=$version_type" >> $GITHUB_OUTPUT |
| 69 | +
|
| 70 | + - name: Calculate next version |
| 71 | + id: calculate |
| 72 | + env: |
| 73 | + VERSION_TYPE: ${{ steps.bump-type.outputs.type }} |
| 74 | + run: | |
| 75 | + echo -e "\nCalculating next version..." |
| 76 | +
|
| 77 | + latest_tag_version=$(git tag --sort=committerdate --list | tail -1) |
| 78 | + latest_version=${latest_tag_version:1} # remove 'v' from tag version |
| 79 | +
|
| 80 | + if [[ "${{ inputs.is-release }}" == "true" ]]; then |
| 81 | + latest_major_version=$(echo $latest_version | cut -d "." -f 1) |
| 82 | + latest_minor_version=$(echo $latest_version | cut -d "." -f 2) |
| 83 | + latest_patch_version=$(echo $latest_version | cut -d "." -f 3) |
| 84 | +
|
| 85 | + echo " latest_version: $latest_version" |
| 86 | + echo " latest_major_version: $latest_major_version" |
| 87 | + echo " latest_minor_version: $latest_minor_version" |
| 88 | + echo " latest_patch_version: $latest_patch_version" |
| 89 | +
|
| 90 | + if [[ "$VERSION_TYPE" == "major" ]]; then |
| 91 | + next_version="$(($latest_major_version + 1)).0.0" |
| 92 | + elif [[ "$VERSION_TYPE" == "minor" ]]; then |
| 93 | + next_version="${latest_major_version}.$(($latest_minor_version + 1)).0" |
| 94 | + elif [[ "$VERSION_TYPE" == "patch" ]]; then |
| 95 | + next_version="${latest_major_version}.${latest_minor_version}.$(($latest_patch_version + 1))" |
| 96 | + else |
| 97 | + next_version="$latest_version+${{ steps.pr.outputs.id }}" |
| 98 | + fi |
| 99 | +
|
| 100 | + echo "Next Version: $next_version" |
| 101 | + echo "version=$next_version" >> $GITHUB_OUTPUT |
| 102 | + else |
| 103 | + echo "version=$latest_version+${{ steps.pr.outputs.id }}" >> $GITHUB_OUTPUT |
| 104 | + fi |
0 commit comments