sync-template-version.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env bash
  2. # Sync Docker image versions to template.yaml metadata
  3. # This script is triggered by GitHub Actions when Renovate updates dependencies
  4. #
  5. # Usage:
  6. # ./sync-template-version.sh # Process all templates
  7. # ./sync-template-version.sh file1 file2 ... # Process specific files only
  8. #
  9. # Supports:
  10. # - Docker Compose templates (compose.yaml.j2)
  11. # - Kubernetes Helm templates (values.yaml, values.yml)
  12. # - Kubernetes manifest templates (*.j2 files)
  13. set -euo pipefail
  14. # Color output for better readability
  15. GREEN='\033[0;32m'
  16. YELLOW='\033[1;33m'
  17. NC='\033[0m' # No Color
  18. echo "Starting template version sync..."
  19. # Function to update template.yaml version
  20. update_template_version() {
  21. local template_file="$1"
  22. local new_version="$2"
  23. # Get current version from template.yaml
  24. local current_version
  25. current_version=$(grep -E '^\s*version:\s*' "$template_file" | sed -E 's/.*version:\s*['\''"]?([^'\''"]+)['\''"]?/\1/' | tr -d ' ' || true)
  26. # Only update if versions are different
  27. if [ -n "$current_version" ] && [ "$new_version" != "$current_version" ]; then
  28. echo -e "${GREEN}✓${NC} Updating $template_file: ${YELLOW}$current_version${NC} → ${GREEN}$new_version${NC}"
  29. # Update version in template.yaml (cross-platform compatible)
  30. if [[ "$OSTYPE" == "darwin"* ]]; then
  31. sed -i '' "s/version: .*/version: $new_version/" "$template_file"
  32. else
  33. sed -i "s/version: .*/version: $new_version/" "$template_file"
  34. fi
  35. return 0
  36. fi
  37. return 1
  38. }
  39. # Counter for updated templates
  40. updated_count=0
  41. # Determine which files to process
  42. if [ $# -gt 0 ]; then
  43. # Process only specified files
  44. echo "Processing ${#@} changed file(s)..."
  45. FILES_TO_PROCESS=("$@")
  46. else
  47. # Process all templates
  48. echo "Processing all templates..."
  49. mapfile -t FILES_TO_PROCESS < <(find library -type f \( -name "compose.yaml.j2" -o -name "values.yaml" -o -name "values.yml" \) 2>/dev/null || true)
  50. fi
  51. # Process each file
  52. for file_path in "${FILES_TO_PROCESS[@]}"; do
  53. # Skip if file doesn't exist
  54. [ ! -f "$file_path" ] && continue
  55. template_dir=$(dirname "$file_path")
  56. template_file="$template_dir/template.yaml"
  57. # Skip if template.yaml doesn't exist
  58. [ ! -f "$template_file" ] && continue
  59. # Determine file type and extract version accordingly
  60. filename=$(basename "$file_path")
  61. version=""
  62. if [[ "$filename" == "compose.yaml.j2" ]]; then
  63. # Docker Compose template
  64. # Extract the first Docker image version
  65. # Matches: image: repo/name:version or image: name:version
  66. # Ignores Jinja2 variables like {{ variable }}
  67. version=$(grep -E '^\s*image:\s*[^{]*:[^{}\s]+' "$file_path" | head -n1 | sed -E 's/.*:([^:]+)$/\1/' | tr -d ' ' || true)
  68. elif [[ "$filename" == "values.yaml" ]] || [[ "$filename" == "values.yml" ]]; then
  69. # Kubernetes Helm values file
  70. # Extract version from repository + tag pattern
  71. version=$(grep -A1 'repository:' "$file_path" | grep 'tag:' | sed -E 's/.*tag:\s*['\''"]?([^'\''" ]+)['\''"]?/\1/' | head -n1 | tr -d ' ' || true)
  72. elif [[ "$filename" == *.j2 ]]; then
  73. # Kubernetes manifest template
  74. # Extract the first Docker image version
  75. version=$(grep -E '^\s*image:\s*[^{]*:[^{}\s]+' "$file_path" | head -n1 | sed -E 's/.*:([^:]+)$/\1/' | tr -d ' ' || true)
  76. fi
  77. # Skip if no version found or if it's a Jinja2 variable
  78. if [ -z "$version" ] || [[ "$version" =~ \{\{ ]]; then
  79. continue
  80. fi
  81. # Update template version
  82. if update_template_version "$template_file" "$version"; then
  83. ((updated_count++))
  84. fi
  85. done
  86. echo ""
  87. echo "=================================================="
  88. if [ $updated_count -gt 0 ]; then
  89. echo -e "${GREEN}✓${NC} Template version sync complete: ${GREEN}$updated_count${NC} template(s) updated"
  90. else
  91. echo "No template version updates needed"
  92. fi
  93. echo "=================================================="
  94. # Exit successfully
  95. exit 0