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