diff --git a/scripts/check_deployment.py b/scripts/check_deployment.py index 145a881..becb750 100755 --- a/scripts/check_deployment.py +++ b/scripts/check_deployment.py @@ -17,6 +17,7 @@ Usage: import argparse import json +import os import random import ssl import subprocess @@ -58,7 +59,14 @@ def colored_print(message: str, color: str = Colors.NC) -> None: 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: result = subprocess.run( ["git", "rev-parse", "HEAD"], @@ -66,8 +74,11 @@ def get_current_commit_hash() -> Optional[str]: text=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): + colored_print("Could not determine current commit hash", Colors.RED) return None @@ -81,16 +92,44 @@ def is_commit_newer_or_equal(commit1: str, commit2: str) -> Optional[bool]: None if comparison fails """ try: - # Use git merge-base to check if commit1 is reachable from commit2 - # If commit1 is newer or equal, it should be reachable from commit2 - subprocess.run( - ["git", "merge-base", "--is-ancestor", commit2, commit1], - capture_output=True, - check=True, - ) - return True + # First try to get commit timestamps for comparison + try: + result1 = subprocess.run( + ["git", "show", "-s", "--format=%ct", commit1], + capture_output=True, + text=True, + check=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: - # If the above fails, try the reverse - check if commit2 is newer + # If the above fails, try the reverse try: subprocess.run( ["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 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 + except Exception as e: + colored_print(f"Git comparison error: {e}", Colors.RED) + return None def make_request( @@ -348,16 +390,27 @@ def test_deployment( version_ok = True 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: colored_print( "✅ Exact version match - deployment is up to date!", 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: - # Check if deployed version is newer + # Check if deployed version is newer using git comparison = is_commit_newer_or_equal( deployed_commit, current_commit ) + colored_print(f"Commit comparison result: {comparison}", Colors.BLUE) + if comparison is True: colored_print( "ℹ️ Note: A newer version is already deployed (this is fine!)", @@ -375,9 +428,14 @@ def test_deployment( version_ok = False else: colored_print( - "⚠️ Warning: Could not determine version relationship - proceeding with tests", + "⚠️ Warning: Could not determine version relationship", 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 if not version_ok: