Skip to content

Commit 60c45d3

Browse files
committed
build: improve version tag detection robustness
Makefile: - Add determine-last-tag function to check if expected previous tag exists - Fall back to latest stable tag if expected tag not found - Pass current version as expected previous tag to bump-version targets build-rpm.yml: - Extract base version from pre-release tags (e.g., v0.5.0-alpha -> v0.5.0) - Check if stable version tag exists before finding previous tag - Add informative logging for tag detection process - Fall back to most recent stable tag when needed
1 parent d0bb28f commit 60c45d3

2 files changed

Lines changed: 44 additions & 12 deletions

File tree

.github/workflows/build-rpm.yml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,33 @@ jobs:
219219
id: generate-notes
220220
run: |
221221
CURRENT_TAG="${{ github.ref_name }}"
222-
# Get the previous stable tag (excluding alpha/beta/rc suffixes)
223-
# Filter tags matching vX.Y.Z pattern (no suffix like -alpha, -beta, -rc)
224-
PREV_TAG=$(git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | grep -A1 "^${CURRENT_TAG}$" | tail -n1)
225-
if [ -z "$PREV_TAG" ] || [ "$PREV_TAG" = "$CURRENT_TAG" ]; then
226-
PREV_TAG=$(git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -2 | tail -n1)
222+
echo "Current tag: $CURRENT_TAG"
223+
224+
# Extract version number from current tag (e.g., v0.5.0-alpha -> v0.5.0)
225+
CURRENT_VERSION=$(echo "$CURRENT_TAG" | grep -oE '^v[0-9]+\.[0-9]+\.[0-9]+')
226+
echo "Current version: $CURRENT_VERSION"
227+
228+
# Get all stable tags sorted by version
229+
STABLE_TAGS=$(git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$')
230+
echo "Stable tags:"
231+
echo "$STABLE_TAGS" | head -5
232+
233+
# Check if the stable version of current tag exists
234+
if echo "$STABLE_TAGS" | grep -q "^${CURRENT_VERSION}$"; then
235+
echo "Found stable version $CURRENT_VERSION, finding previous tag"
236+
# Use the stable version as reference, find the one before it
237+
PREV_TAG=$(echo "$STABLE_TAGS" | grep -A1 "^${CURRENT_VERSION}$" | tail -n1)
238+
else
239+
echo "Stable version not found, using most recent stable tag"
240+
# Current tag is pre-release, find the most recent stable tag
241+
PREV_TAG=$(echo "$STABLE_TAGS" | head -1)
227242
fi
228243
229-
echo "Current tag: $CURRENT_TAG"
244+
# Fallback: if still no prev tag, use the most recent stable tag
245+
if [ -z "$PREV_TAG" ] || [ "$PREV_TAG" = "$CURRENT_VERSION" ]; then
246+
echo "Fallback: using most recent stable tag"
247+
PREV_TAG=$(echo "$STABLE_TAGS" | head -1)
248+
fi
230249
echo "Previous tag: $PREV_TAG"
231250
232251
# Generate changelog with commit hashes

Makefile

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,20 @@ define update-cargo-lock
210210
endef
211211

212212
# Function to get git info
213+
# $(1) = expected previous version tag (e.g., v0.4.0)
213214
define get-git-info
214-
$(eval LAST_TAG := $(shell git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$$' | head -1))
215+
$(eval EXPECTED_PREV_TAG := v$(shell echo $(1) | awk -F. '{print $$1"."$$2".0"}'))
215216
$(eval AUTHOR := $(shell git log -1 --pretty=format:"%an <%ae>"))
216217
endef
217218

219+
# Function to determine last tag
220+
# $(1) = expected previous version tag (e.g., v0.4.0)
221+
define determine-last-tag
222+
$(eval EXPECTED_TAG := $(1))
223+
$(eval TAG_EXISTS := $(shell git tag -l "$(EXPECTED_TAG)"))
224+
$(eval LAST_TAG := $(if $(TAG_EXISTS),$(EXPECTED_TAG),$(shell git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$$' | head -1)))
225+
endef
226+
218227
# Function to write commits to file for RPM (uses -)
219228
define write-commits-to-file
220229
@git log $(LAST_TAG)..HEAD --pretty=format:"- %s" --no-merges > $(1) 2>/dev/null || echo "- Version bump to $(2)" > $(1)
@@ -255,13 +264,17 @@ define update-debian-changelog
255264
endef
256265

257266
# Main bump version function
267+
# $(1) = version type (major/minor/patch)
268+
# $(2) = new version number
269+
# $(3) = expected previous version tag (e.g., v0.4.0)
258270
define bump-version-internal
259271
@echo "Bumping $(1) version: $(CURRENT_VERSION) -> $(2)"
260272
$(call update-cargo-toml,$(2))
261273
@echo "New version: $(2)"
262274
$(call update-cargo-lock)
263275
$(call get-git-info,$(2))
264-
@echo "Last tag: $(LAST_TAG)"
276+
$(call determine-last-tag,$(3))
277+
@echo "Using last tag: $(LAST_TAG)"
265278
@echo "Updating RPM spec version and changelog..."
266279
$(call update-rpm-spec,$(2))
267280
@echo "Updating Debian changelog..."
@@ -270,7 +283,7 @@ define bump-version-internal
270283
@echo "Changes made:"
271284
@echo " - Updated Cargo.toml"
272285
@echo " - Updated Cargo.lock"
273-
@echo " - Updated RPM spec changelog"
286+
@echo " - Updated RPM spec version and changelog"
274287
@echo " - Updated Debian changelog"
275288
@echo ""
276289
@echo "If it is ok to commit, run the following commands:"
@@ -283,15 +296,15 @@ endef
283296
# Bump major version (0.4.1 -> 1.0.0)
284297
.PHONY: bump-version-major
285298
bump-version-major:
286-
$(call bump-version-internal,major,$(NEW_VERSION_MAJOR))
299+
$(call bump-version-internal,major,$(NEW_VERSION_MAJOR),v$(CURRENT_VERSION))
287300

288301
# Bump minor version (0.4.1 -> 0.5.0)
289302
.PHONY: bump-version-minor
290303
bump-version-minor:
291-
$(call bump-version-internal,minor,$(NEW_VERSION_MINOR))
304+
$(call bump-version-internal,minor,$(NEW_VERSION_MINOR),v$(CURRENT_VERSION))
292305

293306
# Bump patch version (0.4.1 -> 0.4.2)
294307
.PHONY: bump-version-patch
295308
bump-version-patch:
296-
$(call bump-version-internal,patch,$(NEW_VERSION_PATCH))
309+
$(call bump-version-internal,patch,$(NEW_VERSION_PATCH),v$(CURRENT_VERSION))
297310

0 commit comments

Comments
 (0)