-
Notifications
You must be signed in to change notification settings - Fork 0
153 lines (137 loc) · 5.75 KB
/
auto-version.yml
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
name: Auto Version
on:
push:
branches:
- main
- develop
- 'chore/**'
- 'hotfix/**'
- 'fix/**'
- 'feature/**'
- 'release/**'
pull_request:
types: [ closed ]
jobs:
auto-version:
runs-on: ubuntu-latest
timeout-minutes: 15
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.merged == true)
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
token: ${{ secrets.GITHUB_TOKEN }}
- name: Get version info
id: get_version_info
run: |
git config --local user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git config --local user.name "${GITHUB_ACTOR}"
# Handle both PR and push events for branch name
EVENT_NAME="${{ github.event_name }}"
if [[ "$EVENT_NAME" == "pull_request" ]]; then
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
else
BRANCH_NAME="${GITHUB_REF#refs/heads/}"
fi
# Determine branch type prefix first
if [[ $BRANCH_NAME == "develop" ]]; then
PREFIX="dev"
elif [[ $BRANCH_NAME == "main" ]]; then
PREFIX="main"
elif [[ $BRANCH_NAME == feature/* ]]; then
PREFIX="feat"
elif [[ $BRANCH_NAME == hotfix/* ]]; then
PREFIX="fix"
elif [[ $BRANCH_NAME == release/* ]]; then
PREFIX="rel"
else
PREFIX="build"
fi
# Get latest version tag globally, ignoring branch prefixes
echo "Available tags:"
if ! git tag --list | grep -q "^v"; then
echo "No version tags found, creating initial tag v0.0.1"
git tag -a "v0.0.1" -m "Initial version"
git push origin v0.0.1
LATEST_TAG="v0.0.1"
else
git tag --sort=-v:refname
echo "Filtered tags:"
git tag --sort=-v:refname | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+" || echo "No matching tags"
LATEST_TAG=$(git tag --sort=-v:refname | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+" | head -n1 || echo "v0.0.1")
fi
COMMIT_SHA=$(git rev-parse --short HEAD)
PR_NUMBER=${{ github.event.pull_request.number }}
COMMIT_MSG=$(git log -1 --pretty=%B)
# Version bump detection
if [[ $COMMIT_MSG == *"#major"* ]] || [[ $COMMIT_MSG == "BREAKING CHANGE:"* ]] || [[ $COMMIT_MSG == *"!"* ]]; then
echo "bump_type=major" >> $GITHUB_OUTPUT
elif [[ $COMMIT_MSG == *"#minor"* ]] || [[ $COMMIT_MSG == "feat:"* ]]; then
echo "bump_type=minor" >> $GITHUB_OUTPUT
elif [[ $COMMIT_MSG == *"#patch"* ]] || [[ $COMMIT_MSG == "fix:"* ]]; then
echo "bump_type=patch" >> $GITHUB_OUTPUT
else
echo "bump_type=auto" >> $GITHUB_OUTPUT
fi
echo "latest_tag=${LATEST_TAG}" >> $GITHUB_OUTPUT
echo "commit_sha=${COMMIT_SHA}" >> $GITHUB_OUTPUT
echo "prefix=${PREFIX}" >> $GITHUB_OUTPUT
echo "pr_number=${PR_NUMBER}" >> $GITHUB_OUTPUT
- name: Bump version
id: bump_version
run: |
LATEST_TAG=${{ steps.get_version_info.outputs.latest_tag }}
COMMIT_SHA=${{ steps.get_version_info.outputs.commit_sha }}
PREFIX=${{ steps.get_version_info.outputs.prefix }}
PR_NUMBER=${{ steps.get_version_info.outputs.pr_number }}
BUMP_TYPE=${{ steps.get_version_info.outputs.bump_type }}
# Remove 'v' prefix and split into array
VERSION=${LATEST_TAG#v}
IFS='.' read -ra VERSION_PARTS <<< "$VERSION"
echo "current_version=${VERSION}" >> $GITHUB_OUTPUT
# Version increment logic
case $BUMP_TYPE in
"major")
((VERSION_PARTS[0]++)) # Increment major
VERSION_PARTS[1]=0 # Reset minor
VERSION_PARTS[2]=0 # Reset patch
;;
"minor")
((VERSION_PARTS[1]++)) # Increment minor
VERSION_PARTS[2]=0 # Reset patch
;;
"patch")
((VERSION_PARTS[2]++)) # Increment patch
;;
"auto")
# Default behavior based on branch type
if [[ $PREFIX == "rel" ]]; then
((VERSION_PARTS[1]++)) # Increment minor for release
VERSION_PARTS[2]=0 # Reset patch
else
((VERSION_PARTS[2]++)) # Increment patch for everything else
fi
;;
esac
# Create new version string
if [[ -n $PR_NUMBER ]]; then
NEW_VERSION="v${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${VERSION_PARTS[2]}-${PREFIX}.pr${PR_NUMBER}.${COMMIT_SHA}"
else
NEW_VERSION="v${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${VERSION_PARTS[2]}-${PREFIX}.${COMMIT_SHA}"
fi
echo "new_version=${NEW_VERSION}" >> $GITHUB_OUTPUT
# Update version in build.gradle.kts
VERSION_WITHOUT_V=${NEW_VERSION#v}
BASE_VERSION=$(echo $VERSION_WITHOUT_V | cut -d'-' -f1)
sed -i "s/version = \".*\"/version = \"$BASE_VERSION\"/" build.gradle.kts
# Stage the version update
git add build.gradle.kts
# Create commit and tag
git commit -m "chore: release version ${NEW_VERSION} [skip ci]"
git tag -a ${NEW_VERSION} -m "Release ${NEW_VERSION}"
# Push both the commit and tag
git push origin HEAD ${NEW_VERSION}