sync-template-version.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. # Supports:
  6. # - Docker Compose templates (compose.yaml.j2)
  7. # - Kubernetes Helm templates (values.yaml, values.yml)
  8. # - Kubernetes manifest templates (*.j2 files)
  9. set -euo pipefail
  10. # Color output for better readability
  11. GREEN='\033[0;32m'
  12. YELLOW='\033[1;33m'
  13. NC='\033[0m' # No Color
  14. echo "Starting template version sync..."
  15. # Function to extract version from Docker image reference
  16. # Handles: image:tag, registry/image:tag, registry/namespace/image:tag
  17. extract_version_from_image() {
  18. local image_line="$1"
  19. # Extract everything after the last colon, excluding Jinja2 variables
  20. echo "$image_line" | sed -E 's/.*:([^:]+)$/\1/' | tr -d ' ' | grep -v '{{' || true
  21. }
  22. # Function to update template.yaml version
  23. update_template_version() {
  24. local template_file="$1"
  25. local new_version="$2"
  26. # Get current version from template.yaml
  27. local current_version
  28. current_version=$(grep -E '^\s*version:\s*' "$template_file" | sed -E 's/.*version:\s*['\''"]?([^'\''"]+)['\''"]?/\1/' | tr -d ' ' || true)
  29. # Only update if versions are different
  30. if [ -n "$current_version" ] && [ "$new_version" != "$current_version" ]; then
  31. echo -e "${GREEN}✓${NC} Updating $template_file: ${YELLOW}$current_version${NC} → ${GREEN}$new_version${NC}"
  32. # Update version in template.yaml (cross-platform compatible)
  33. if [[ "$OSTYPE" == "darwin"* ]]; then
  34. sed -i '' "s/version: .*/version: $new_version/" "$template_file"
  35. else
  36. sed -i "s/version: .*/version: $new_version/" "$template_file"
  37. fi
  38. return 0
  39. fi
  40. return 1
  41. }
  42. # Counter for updated templates
  43. updated_count=0
  44. # Process Docker Compose templates
  45. echo ""
  46. echo "Scanning Docker Compose templates..."
  47. while IFS= read -r compose_file; do
  48. template_dir=$(dirname "$compose_file")
  49. template_file="$template_dir/template.yaml"
  50. # Skip if template.yaml doesn't exist
  51. [ ! -f "$template_file" ] && continue
  52. # Extract the first Docker image version from compose.yaml.j2
  53. # Matches: image: repo/name:version or image: name:version
  54. # Ignores Jinja2 variables like {{ variable }}
  55. version=$(grep -E '^\s*image:\s*[^{]*:[^{}\s]+' "$compose_file" | head -n1 | sed -E 's/.*:([^:]+)$/\1/' | tr -d ' ' || true)
  56. # Skip if no version found or if it's a Jinja2 variable
  57. if [ -z "$version" ] || [[ "$version" =~ \{\{ ]]; then
  58. continue
  59. fi
  60. # Update template version
  61. if update_template_version "$template_file" "$version"; then
  62. ((updated_count++))
  63. fi
  64. done < <(find library/compose -type f -name "compose.yaml.j2" 2>/dev/null || true)
  65. # Process Kubernetes Helm templates (values.yaml pattern)
  66. echo ""
  67. echo "Scanning Kubernetes Helm templates..."
  68. while IFS= read -r values_file; do
  69. template_dir=$(dirname "$values_file")
  70. template_file="$template_dir/template.yaml"
  71. # Skip if template.yaml doesn't exist
  72. [ ! -f "$template_file" ] && continue
  73. # Extract version from Helm values.yaml
  74. # Matches repository + tag pattern:
  75. # repository: registry/image
  76. # tag: version
  77. version=$(grep -A1 'repository:' "$values_file" | grep 'tag:' | sed -E 's/.*tag:\s*['\''"]?([^'\''" ]+)['\''"]?/\1/' | head -n1 | tr -d ' ' || true)
  78. # Skip if no version found or if it's a Jinja2 variable
  79. if [ -z "$version" ] || [[ "$version" =~ \{\{ ]]; then
  80. continue
  81. fi
  82. # Update template version
  83. if update_template_version "$template_file" "$version"; then
  84. ((updated_count++))
  85. fi
  86. done < <(find library/kubernetes -type f \( -name "values.yaml" -o -name "values.yml" \) 2>/dev/null || true)
  87. # Process Kubernetes manifest templates (*.j2 files with image: references)
  88. echo ""
  89. echo "Scanning Kubernetes manifest templates..."
  90. while IFS= read -r manifest_file; do
  91. template_dir=$(dirname "$manifest_file")
  92. template_file="$template_dir/template.yaml"
  93. # Skip if template.yaml doesn't exist
  94. [ ! -f "$template_file" ] && continue
  95. # Extract the first Docker image version from Kubernetes manifest
  96. # Matches: image: repo/name:version or image: name:version
  97. # Ignores Jinja2 variables like {{ variable }}
  98. version=$(grep -E '^\s*image:\s*[^{]*:[^{}\s]+' "$manifest_file" | head -n1 | sed -E 's/.*:([^:]+)$/\1/' | tr -d ' ' || true)
  99. # Skip if no version found or if it's a Jinja2 variable
  100. if [ -z "$version" ] || [[ "$version" =~ \{\{ ]]; then
  101. continue
  102. fi
  103. # Update template version
  104. if update_template_version "$template_file" "$version"; then
  105. ((updated_count++))
  106. fi
  107. done < <(find library/kubernetes -type f -name "*.j2" 2>/dev/null || true)
  108. # Process Terraform/Packer templates if needed in the future
  109. # (Currently no version syncing implemented for these)
  110. echo ""
  111. echo "=================================================="
  112. if [ $updated_count -gt 0 ]; then
  113. echo -e "${GREEN}✓${NC} Template version sync complete: ${GREEN}$updated_count${NC} template(s) updated"
  114. else
  115. echo "No template version updates needed"
  116. fi
  117. echo "=================================================="