mirror of
https://git.linux-kernel.at/oliver/ivatar.git
synced 2025-11-16 13:08:03 +00:00
Improve deployment verification commit checking
- Use CI_COMMIT_SHA environment variable when available (more reliable in CI) - Add timestamp-based commit comparison as primary method - Fallback to git merge-base for ancestry checking - Add detailed debugging output for commit comparison - Handle short vs long commit hash matching - Better error handling for shallow git clones in CI - More robust version detection and waiting logic
This commit is contained in:
@@ -17,6 +17,7 @@ Usage:
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import random
|
import random
|
||||||
import ssl
|
import ssl
|
||||||
import subprocess
|
import subprocess
|
||||||
@@ -58,7 +59,14 @@ def colored_print(message: str, color: str = Colors.NC) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def get_current_commit_hash() -> Optional[str]:
|
def get_current_commit_hash() -> Optional[str]:
|
||||||
"""Get the current commit hash from git."""
|
"""Get the current commit hash from git or CI environment."""
|
||||||
|
# First try GitLab CI environment variable (most reliable in CI)
|
||||||
|
ci_commit = os.environ.get("CI_COMMIT_SHA")
|
||||||
|
if ci_commit:
|
||||||
|
colored_print(f"Using CI commit hash: {ci_commit}", Colors.BLUE)
|
||||||
|
return ci_commit
|
||||||
|
|
||||||
|
# Fallback to git command
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
["git", "rev-parse", "HEAD"],
|
["git", "rev-parse", "HEAD"],
|
||||||
@@ -66,8 +74,11 @@ def get_current_commit_hash() -> Optional[str]:
|
|||||||
text=True,
|
text=True,
|
||||||
check=True,
|
check=True,
|
||||||
)
|
)
|
||||||
return result.stdout.strip()
|
commit_hash = result.stdout.strip()
|
||||||
|
colored_print(f"Using git commit hash: {commit_hash}", Colors.BLUE)
|
||||||
|
return commit_hash
|
||||||
except (subprocess.CalledProcessError, FileNotFoundError):
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
||||||
|
colored_print("Could not determine current commit hash", Colors.RED)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@@ -81,16 +92,44 @@ def is_commit_newer_or_equal(commit1: str, commit2: str) -> Optional[bool]:
|
|||||||
None if comparison fails
|
None if comparison fails
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Use git merge-base to check if commit1 is reachable from commit2
|
# First try to get commit timestamps for comparison
|
||||||
# If commit1 is newer or equal, it should be reachable from commit2
|
try:
|
||||||
subprocess.run(
|
result1 = subprocess.run(
|
||||||
["git", "merge-base", "--is-ancestor", commit2, commit1],
|
["git", "show", "-s", "--format=%ct", commit1],
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
check=True,
|
text=True,
|
||||||
)
|
check=True,
|
||||||
return True
|
)
|
||||||
|
result2 = subprocess.run(
|
||||||
|
["git", "show", "-s", "--format=%ct", commit2],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
timestamp1 = int(result1.stdout.strip())
|
||||||
|
timestamp2 = int(result2.stdout.strip())
|
||||||
|
|
||||||
|
colored_print(f"Commit {commit1[:8]} timestamp: {timestamp1}", Colors.BLUE)
|
||||||
|
colored_print(f"Commit {commit2[:8]} timestamp: {timestamp2}", Colors.BLUE)
|
||||||
|
|
||||||
|
# commit1 is newer if it has a later timestamp
|
||||||
|
return timestamp1 >= timestamp2
|
||||||
|
|
||||||
|
except (subprocess.CalledProcessError, ValueError):
|
||||||
|
# Fallback to merge-base if timestamp comparison fails
|
||||||
|
colored_print("Timestamp comparison failed, trying merge-base", Colors.YELLOW)
|
||||||
|
|
||||||
|
# Use git merge-base to check if commit2 is ancestor of commit1
|
||||||
|
subprocess.run(
|
||||||
|
["git", "merge-base", "--is-ancestor", commit2, commit1],
|
||||||
|
capture_output=True,
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
# If the above fails, try the reverse - check if commit2 is newer
|
# If the above fails, try the reverse
|
||||||
try:
|
try:
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
["git", "merge-base", "--is-ancestor", commit1, commit2],
|
["git", "merge-base", "--is-ancestor", commit1, commit2],
|
||||||
@@ -99,8 +138,11 @@ def is_commit_newer_or_equal(commit1: str, commit2: str) -> Optional[bool]:
|
|||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
# If both fail, we can't determine the relationship
|
colored_print("Git comparison failed - shallow clone or missing commits", Colors.YELLOW)
|
||||||
return None
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
colored_print(f"Git comparison error: {e}", Colors.RED)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def make_request(
|
def make_request(
|
||||||
@@ -348,16 +390,27 @@ def test_deployment(
|
|||||||
version_ok = True
|
version_ok = True
|
||||||
|
|
||||||
if current_commit and deployed_commit != "Unknown":
|
if current_commit and deployed_commit != "Unknown":
|
||||||
|
colored_print(f"Expected commit: {current_commit[:8]}...", Colors.BLUE)
|
||||||
|
colored_print(f"Deployed commit: {deployed_commit[:8]}...", Colors.BLUE)
|
||||||
|
|
||||||
if deployed_commit == current_commit:
|
if deployed_commit == current_commit:
|
||||||
colored_print(
|
colored_print(
|
||||||
"✅ Exact version match - deployment is up to date!",
|
"✅ Exact version match - deployment is up to date!",
|
||||||
Colors.GREEN,
|
Colors.GREEN,
|
||||||
)
|
)
|
||||||
|
elif deployed_commit.startswith(current_commit[:8]) or current_commit.startswith(deployed_commit[:8]):
|
||||||
|
# Handle case where we have short vs long commit hashes
|
||||||
|
colored_print(
|
||||||
|
"✅ Version match (short hash) - deployment is up to date!",
|
||||||
|
Colors.GREEN,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# Check if deployed version is newer
|
# Check if deployed version is newer using git
|
||||||
comparison = is_commit_newer_or_equal(
|
comparison = is_commit_newer_or_equal(
|
||||||
deployed_commit, current_commit
|
deployed_commit, current_commit
|
||||||
)
|
)
|
||||||
|
colored_print(f"Commit comparison result: {comparison}", Colors.BLUE)
|
||||||
|
|
||||||
if comparison is True:
|
if comparison is True:
|
||||||
colored_print(
|
colored_print(
|
||||||
"ℹ️ Note: A newer version is already deployed (this is fine!)",
|
"ℹ️ Note: A newer version is already deployed (this is fine!)",
|
||||||
@@ -375,9 +428,14 @@ def test_deployment(
|
|||||||
version_ok = False
|
version_ok = False
|
||||||
else:
|
else:
|
||||||
colored_print(
|
colored_print(
|
||||||
"⚠️ Warning: Could not determine version relationship - proceeding with tests",
|
"⚠️ Warning: Could not determine version relationship",
|
||||||
Colors.YELLOW,
|
Colors.YELLOW,
|
||||||
)
|
)
|
||||||
|
colored_print(
|
||||||
|
"This might be due to shallow git clone in CI - treating as version mismatch",
|
||||||
|
Colors.YELLOW,
|
||||||
|
)
|
||||||
|
version_ok = False
|
||||||
|
|
||||||
# Only proceed with functionality tests if version is correct
|
# Only proceed with functionality tests if version is correct
|
||||||
if not version_ok:
|
if not version_ok:
|
||||||
|
|||||||
Reference in New Issue
Block a user