-
Notifications
You must be signed in to change notification settings - Fork 1
129 lines (108 loc) · 4.5 KB
/
lf-autover.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
name: 'Linuxfabrik: Semantic Auto-Versioning'
on:
push:
branches:
- 'main'
jobs:
versioning:
runs-on: 'ubuntu-24.04'
outputs:
version: "${{ steps.version_update.outputs.new_version }}"
steps:
- name: 'Checkout Repository'
uses: 'actions/checkout@v4'
with:
fetch-depth: 0 # Required for full commit history
- name: 'Find and download latest version artifact'
run: |
GH_API_URL="https://api.github.com/repos/${{ github.repository_owner }}/${{ github.event.repository.name }}/actions/artifacts"
ARTIFACT_NAME="version"
echo "Fetching latest artifact..."
# Fetch all artifacts and filter by name
ARTIFACT_INFO=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" "$GH_API_URL" | \
jq -r ".artifacts | map(select(.name == \"$ARTIFACT_NAME\")) | sort_by(.created_at) | last // {}")
# Extract the download URL
ARTIFACT_URL=$(echo "$ARTIFACT_INFO" | jq -r ".archive_download_url // empty")
if [[ -z "$ARTIFACT_URL" ]]; then
echo "No previous version artifact found. Defaulting to 1.0.0.0"
echo "previous_version=1.0.0.0" >> $GITHUB_ENV
else
echo "Downloading artifact from: $ARTIFACT_URL"
curl -L -o version.zip -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "$ARTIFACT_URL"
mkdir -p version_artifact
unzip -o version.zip -d version_artifact
PREVIOUS_VERSION=$(cat version_artifact/version.txt)
echo "Previous version found: $PREVIOUS_VERSION"
echo "previous_version=$PREVIOUS_VERSION" >> $GITHUB_ENV
fi
- name: 'Create Version Bump Script'
run: |
cat > bump_version.py <<EOF
import os
import subprocess
import re
# Get previous version
previous_version = os.getenv("previous_version", "1.0.0.0").strip()
# Parse version into components
parts = previous_version.split(".")
while len(parts) < 4:
parts.append("0") # Ensure at least major.minor.patch.revision
major, minor, patch, revision = map(int, parts)
# Get second-to-last pushed commit (avoid failing on first commit)
try:
last_pushed_commit = subprocess.run(
["git", "rev-list", "--skip=1", "--max-count=1", "origin/main"],
capture_output=True,
text=True,
check=True
).stdout.strip()
except subprocess.CalledProcessError:
last_pushed_commit = None # No second-to-last commit exists
# Get commit messages since last push
commit_messages = []
if last_pushed_commit:
commit_messages = subprocess.run(
["git", "log", f"{last_pushed_commit}..HEAD", "--pretty=%s", "--no-merges"],
capture_output=True,
text=True
).stdout.splitlines()
# Determine version bump type
bump_type = "revision"
for msg in commit_messages:
if "BREAKING CHANGE" in msg or re.search(r"(feat|fix)!:", msg):
bump_type = "major"
break
elif msg.startswith("feat:"):
bump_type = "minor"
elif msg.startswith("fix:") and bump_type != "minor":
bump_type = "patch"
# Apply version bump
if bump_type == "major":
major += 1
minor, patch, revision = 0, 0, 0
elif bump_type == "minor":
minor += 1
patch, revision = 0, 0
elif bump_type == "patch":
patch += 1
revision = 0
else: # revision bump
revision += 1
new_version = f"{major}.{minor}.{patch}.{revision}"
# Output new version
with open("version.txt", "w") as f:
f.write(new_version)
print(new_version)
EOF
- name: 'Run Version Bump Script'
run: |
NEW_VERSION=$(python3 bump_version.py)
echo "new_version=$NEW_VERSION" >> $GITHUB_ENV
echo "$NEW_VERSION" > version.txt
echo "New version: $NEW_VERSION"
- name: 'Upload New Version as Artifact'
uses: 'actions/upload-artifact@v4'
with:
name: 'version'
path: 'version.txt'