|
| 1 | +name: 🏷️ Auto Versioning |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - labeled # Runs when labels are added to the PR |
| 7 | + branches: |
| 8 | + - dev # Only run for PRs targeting the dev branch |
| 9 | + if: github.event.pull_request.merged == false # Skip if PR is already merged |
| 10 | + |
| 11 | +jobs: |
| 12 | + versioning: |
| 13 | + name: Auto Versioning |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Checkout Repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 # Fetch full history to enable merging |
| 20 | + |
| 21 | + - name: Set up Git |
| 22 | + run: | |
| 23 | + git config --global user.name "github-actions[bot]" |
| 24 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 25 | +
|
| 26 | + - name: Get PR Labels |
| 27 | + id: labels |
| 28 | + uses: actions/github-script@v7 |
| 29 | + with: |
| 30 | + script: | |
| 31 | + const labels = context.payload.pull_request.labels.map(label => label.name); |
| 32 | + console.log("PR Labels:", labels); |
| 33 | + return labels; |
| 34 | +
|
| 35 | + - name: Check if Version Label Exists |
| 36 | + id: check_version_label |
| 37 | + run: | |
| 38 | + if echo "${{ steps.labels.outputs.result }}" | grep -Eq "version:(major|minor|patch)"; then |
| 39 | + echo "run_versioning=true" >> $GITHUB_ENV |
| 40 | + else |
| 41 | + echo "::notice::No versioning label found. Skipping workflow." |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | +
|
| 45 | + - name: Merge `dev` into PR Branch |
| 46 | + if: env.run_versioning == 'true' |
| 47 | + run: | |
| 48 | + git fetch origin dev |
| 49 | + git checkout ${{ github.head_ref }} |
| 50 | + if git merge --no-edit origin/dev; then |
| 51 | + echo "Merge successful." |
| 52 | + else |
| 53 | + echo "::error::Merge conflict detected! Resolve conflicts manually." |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + continue-on-error: false |
| 57 | + |
| 58 | + - name: Get Current Version from `dev` |
| 59 | + if: env.run_versioning == 'true' |
| 60 | + run: | |
| 61 | + VERSION=$(git show origin/dev:CMakeLists.txt | sed -nE 's/project\(atta VERSION ([0-9]+\.[0-9]+\.[0-9]+).*/\1/p') |
| 62 | +
|
| 63 | + if [[ -z "$VERSION" ]]; then |
| 64 | + echo "::error::Failed to extract version from CMakeLists.txt" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +
|
| 68 | + echo "::notice::Current Version (from dev): $VERSION" |
| 69 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 70 | +
|
| 71 | + - name: Determine Version Increment |
| 72 | + if: env.run_versioning == 'true' |
| 73 | + run: | |
| 74 | + echo "::notice::PR Labels Found: ${{ steps.labels.outputs.result }}" |
| 75 | + if echo "${{ steps.labels.outputs.result }}" | grep -q "version:major"; then |
| 76 | + echo "increment=major" >> $GITHUB_ENV |
| 77 | + echo "::notice::Version Increment: major" |
| 78 | + elif echo "${{ steps.labels.outputs.result }}" | grep -q "version:minor"; then |
| 79 | + echo "increment=minor" >> $GITHUB_ENV |
| 80 | + echo "::notice::Version Increment: minor" |
| 81 | + else |
| 82 | + echo "increment=patch" >> $GITHUB_ENV |
| 83 | + echo "::notice::Version Increment: patch" |
| 84 | + fi |
| 85 | +
|
| 86 | + - name: Increment Version |
| 87 | + if: env.run_versioning == 'true' |
| 88 | + run: | |
| 89 | + IFS='.' read -r major minor patch <<< "$VERSION" |
| 90 | +
|
| 91 | + case "$increment" in |
| 92 | + major) major=$((major + 1)); minor=0; patch=0 ;; |
| 93 | + minor) minor=$((minor + 1)); patch=0 ;; |
| 94 | + patch) patch=$((patch + 1)) ;; |
| 95 | + esac |
| 96 | +
|
| 97 | + NEW_VERSION="$major.$minor.$patch" |
| 98 | + echo "::notice::New Version: $NEW_VERSION" |
| 99 | +
|
| 100 | + sed -i -E "s/(project\(atta VERSION) [0-9]+\.[0-9]+\.[0-9]+/\1 $NEW_VERSION/" CMakeLists.txt |
| 101 | + echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV |
| 102 | +
|
| 103 | + - name: Commit Updated Version (If Changed) |
| 104 | + if: env.run_versioning == 'true' |
| 105 | + run: | |
| 106 | + git add CMakeLists.txt |
| 107 | + if git diff --staged --quiet; then |
| 108 | + echo "No changes to commit." |
| 109 | + else |
| 110 | + git commit -m "Chore: Bump version to $NEW_VERSION" |
| 111 | + git push origin ${{ github.head_ref }} |
| 112 | + fi |
| 113 | +
|
| 114 | + - name: Delete Old Tag (If Exists) |
| 115 | + if: env.run_versioning == 'true' |
| 116 | + run: | |
| 117 | + if git rev-parse "v$NEW_VERSION" >/dev/null 2>&1; then |
| 118 | + git push --delete origin "v$NEW_VERSION" |
| 119 | + git tag -d "v$NEW_VERSION" |
| 120 | + echo "Deleted existing tag v$NEW_VERSION." |
| 121 | + else |
| 122 | + echo "No existing tag v$NEW_VERSION found." |
| 123 | + fi |
| 124 | +
|
| 125 | + - name: Create and Push New Git Tag |
| 126 | + if: env.run_versioning == 'true' |
| 127 | + run: | |
| 128 | + # Fetch the PR title (single quotes to preserve special characters) |
| 129 | + PR_TITLE='${{ github.event.pull_request.title }}' |
| 130 | + # Fetch the PR number |
| 131 | + PR_NUMBER="${{ github.event.pull_request.number }}" |
| 132 | + echo "::notice::PR Title: $PR_TITLE" |
| 133 | + echo "::notice::PR Number: $PR_NUMBER" |
| 134 | +
|
| 135 | + # Create an annotated tag with the PR title as the description |
| 136 | + git tag -a "v$NEW_VERSION" -m "PR #$PR_NUMBER - $PR_TITLE" |
| 137 | +
|
| 138 | + # Push the tag to the remote repository |
| 139 | + git push origin "v$NEW_VERSION" |
0 commit comments