Skip to content

Commit b7c5dad

Browse files
committed
ci(build): 🎡 implement parent branch tag fallback strategy
The build workflow previously failed to resolve a reference tag if the current branch had no specific build tags. This update introduces a robust resolution strategy: - scans parent branches (main, master, develop, dev) to find the closest common ancestor - selects the "best" tag based on the shortest commit distance from the merge-base - adds a final fallback to search for any ancestor tag if branch-specific resolution fails
1 parent 0990069 commit b7c5dad

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

.github/workflows/build.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,77 @@ jobs:
236236
echo "No new format tag found. Falling back to search for any older build tag..."
237237
LAST_TAG=$(git describe --tags --abbrev=0 --match="$BRANCH_NAME/build-*" 2>/dev/null || echo "")
238238
fi
239+
240+
# ═══════════════════════════════════════════════════════════
241+
# PARENT BRANCH FALLBACK: Find closest parent branch's tag
242+
# ═══════════════════════════════════════════════════════════
243+
if [ -z "$LAST_TAG" ]; then
244+
echo ""
245+
echo "⚠️ No tag found for '$BRANCH_NAME', searching parent branches..."
246+
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
247+
248+
FALLBACK_BRANCHES="main master develop dev"
249+
BEST_TAG=""
250+
BEST_DISTANCE=999999
251+
BEST_PARENT=""
252+
253+
for PARENT in $FALLBACK_BRANCHES; do
254+
# Skip if same as current branch
255+
[ "$PARENT" == "$BRANCH_NAME" ] && continue
256+
257+
# Check if branch exists (remote first, then local)
258+
if git rev-parse --verify "origin/$PARENT" >/dev/null 2>&1; then
259+
BRANCH_REF="origin/$PARENT"
260+
elif git rev-parse --verify "$PARENT" >/dev/null 2>&1; then
261+
BRANCH_REF="$PARENT"
262+
else
263+
echo " $PARENT: doesn't exist, skipping"
264+
continue
265+
fi
266+
267+
# Find merge-base (common ancestor)
268+
MERGE_BASE=$(git merge-base HEAD "$BRANCH_REF" 2>/dev/null || true)
269+
if [ -z "$MERGE_BASE" ]; then
270+
echo " $PARENT: no common ancestor, skipping"
271+
continue
272+
fi
273+
274+
# Count commits from merge-base to HEAD (distance = how far we've diverged)
275+
DISTANCE=$(git rev-list --count "$MERGE_BASE"..HEAD 2>/dev/null || echo "999999")
276+
277+
# Find tag at or before merge-base
278+
PARENT_TAG=$(git describe --tags --abbrev=0 --match="$PARENT/build-*" "$MERGE_BASE" 2>/dev/null || true)
279+
280+
if [ -n "$PARENT_TAG" ]; then
281+
echo " $PARENT: found $PARENT_TAG (distance: $DISTANCE commits)"
282+
if [ "$DISTANCE" -lt "$BEST_DISTANCE" ]; then
283+
BEST_DISTANCE=$DISTANCE
284+
BEST_TAG=$PARENT_TAG
285+
BEST_PARENT=$PARENT
286+
fi
287+
else
288+
echo " $PARENT: no build tag found at merge-base"
289+
fi
290+
done
291+
292+
if [ -n "$BEST_TAG" ]; then
293+
LAST_TAG="$BEST_TAG"
294+
echo ""
295+
echo "✅ Using parent tag: $LAST_TAG (from '$BEST_PARENT', $BEST_DISTANCE commits ago)"
296+
fi
297+
fi
298+
299+
# ═══════════════════════════════════════════════════════════
300+
# ULTIMATE FALLBACK: Any ancestor tag
301+
# ═══════════════════════════════════════════════════════════
302+
if [ -z "$LAST_TAG" ]; then
303+
echo ""
304+
echo "🔍 No parent branch tag found, trying any ancestor tag..."
305+
LAST_TAG=$(git describe --tags --abbrev=0 --match="*/build-*" HEAD 2>/dev/null || true)
306+
if [ -n "$LAST_TAG" ]; then
307+
echo "✅ Found ancestor tag: $LAST_TAG"
308+
fi
309+
fi
239310
fi
240311
241312
echo "✅ Using tag: $LAST_TAG"

0 commit comments

Comments
 (0)