Просмотр исходного кода

fix: improve template version sync to process only changed files

- Move script from scripts/ to .github/scripts/
- Add parameter support to process specific files only
- Update workflow to detect changed files and pass to script
- Add explicit exit 0 to prevent false failures
- Only sync templates that were actually changed by Renovate
xcad 4 месяцев назад
Родитель
Сommit
aa08a94356

+ 113 - 0
.github/scripts/sync-template-version.sh

@@ -0,0 +1,113 @@
+#!/usr/bin/env bash
+# Sync Docker image versions to template.yaml metadata
+# This script is triggered by GitHub Actions when Renovate updates dependencies
+#
+# Usage:
+#   ./sync-template-version.sh                    # Process all templates
+#   ./sync-template-version.sh file1 file2 ...    # Process specific files only
+#
+# Supports:
+# - Docker Compose templates (compose.yaml.j2)
+# - Kubernetes Helm templates (values.yaml, values.yml)
+# - Kubernetes manifest templates (*.j2 files)
+
+set -euo pipefail
+
+# Color output for better readability
+GREEN='\033[0;32m'
+YELLOW='\033[1;33m'
+NC='\033[0m' # No Color
+
+echo "Starting template version sync..."
+
+# Function to update template.yaml version
+update_template_version() {
+    local template_file="$1"
+    local new_version="$2"
+    
+    # Get current version from template.yaml
+    local current_version
+    current_version=$(grep -E '^\s*version:\s*' "$template_file" | sed -E 's/.*version:\s*['\''"]?([^'\''"]+)['\''"]?/\1/' | tr -d ' ' || true)
+    
+    # Only update if versions are different
+    if [ -n "$current_version" ] && [ "$new_version" != "$current_version" ]; then
+        echo -e "${GREEN}✓${NC} Updating $template_file: ${YELLOW}$current_version${NC} → ${GREEN}$new_version${NC}"
+        
+        # Update version in template.yaml (cross-platform compatible)
+        if [[ "$OSTYPE" == "darwin"* ]]; then
+            sed -i '' "s/version: .*/version: $new_version/" "$template_file"
+        else
+            sed -i "s/version: .*/version: $new_version/" "$template_file"
+        fi
+        return 0
+    fi
+    return 1
+}
+
+# Counter for updated templates
+updated_count=0
+
+# Determine which files to process
+if [ $# -gt 0 ]; then
+    # Process only specified files
+    echo "Processing ${#@} changed file(s)..."
+    FILES_TO_PROCESS=("$@")
+else
+    # Process all templates
+    echo "Processing all templates..."
+    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)
+fi
+
+# Process each file
+for file_path in "${FILES_TO_PROCESS[@]}"; do
+    # Skip if file doesn't exist
+    [ ! -f "$file_path" ] && continue
+    
+    template_dir=$(dirname "$file_path")
+    template_file="$template_dir/template.yaml"
+    
+    # Skip if template.yaml doesn't exist
+    [ ! -f "$template_file" ] && continue
+    
+    # Determine file type and extract version accordingly
+    filename=$(basename "$file_path")
+    version=""
+    
+    if [[ "$filename" == "compose.yaml.j2" ]]; then
+        # Docker Compose template
+        # Extract the first Docker image version
+        # Matches: image: repo/name:version or image: name:version
+        # Ignores Jinja2 variables like {{ variable }}
+        version=$(grep -E '^\s*image:\s*[^{]*:[^{}\s]+' "$file_path" | head -n1 | sed -E 's/.*:([^:]+)$/\1/' | tr -d ' ' || true)
+    elif [[ "$filename" == "values.yaml" ]] || [[ "$filename" == "values.yml" ]]; then
+        # Kubernetes Helm values file
+        # Extract version from repository + tag pattern
+        version=$(grep -A1 'repository:' "$file_path" | grep 'tag:' | sed -E 's/.*tag:\s*['\''"]?([^'\''" ]+)['\''"]?/\1/' | head -n1 | tr -d ' ' || true)
+    elif [[ "$filename" == *.j2 ]]; then
+        # Kubernetes manifest template
+        # Extract the first Docker image version
+        version=$(grep -E '^\s*image:\s*[^{]*:[^{}\s]+' "$file_path" | head -n1 | sed -E 's/.*:([^:]+)$/\1/' | tr -d ' ' || true)
+    fi
+    
+    # Skip if no version found or if it's a Jinja2 variable
+    if [ -z "$version" ] || [[ "$version" =~ \{\{ ]]; then
+        continue
+    fi
+    
+    # Update template version
+    if update_template_version "$template_file" "$version"; then
+        ((updated_count++))
+    fi
+done
+
+echo ""
+echo "=================================================="
+if [ $updated_count -gt 0 ]; then
+    echo -e "${GREEN}✓${NC} Template version sync complete: ${GREEN}$updated_count${NC} template(s) updated"
+else
+    echo "No template version updates needed"
+fi
+echo "=================================================="
+
+# Exit successfully
+exit 0

+ 2 - 2
.github/workflows/lint.yaml → .github/workflows/codequality-yamllint.yaml

@@ -1,7 +1,7 @@
 ---
-name: Lint
+name: Code Quality - yamllint
 
-on:  # yamllint disable-line rule:truthy
+'on':
   pull_request:
     branches:
       - main

+ 1 - 1
.github/workflows/release.yaml → .github/workflows/release-create-cli-release.yaml

@@ -1,5 +1,5 @@
 ---
-name: Release
+name: Release - Create CLI GitHub Release
 
 'on':
   push:

+ 28 - 7
.github/workflows/renovate-sync-versions.yaml

@@ -1,7 +1,7 @@
 ---
 name: Renovate - Sync Template Versions
 
-on:  # yamllint disable-line rule:truthy
+'on':
   pull_request:
     branches:
       - main
@@ -25,15 +25,36 @@ jobs:
           ref: ${{ github.head_ref }}
           token: ${{ secrets.GITHUB_TOKEN }}
 
+      - name: Detect changed template files
+        id: changes
+        run: |
+          # Get list of changed files in library/
+          CHANGED_FILES=$(git diff --name-only origin/main...HEAD | grep '^library/' | grep -E '\.(j2|yaml|yml)$' || true)
+          
+          if [ -n "$CHANGED_FILES" ]; then
+            echo "Changed template files:"
+            echo "$CHANGED_FILES"
+            echo "files<<EOF" >> $GITHUB_OUTPUT
+            echo "$CHANGED_FILES" >> $GITHUB_OUTPUT
+            echo "EOF" >> $GITHUB_OUTPUT
+            echo "has_files=true" >> $GITHUB_OUTPUT
+          else
+            echo "No template files changed"
+            echo "has_files=false" >> $GITHUB_OUTPUT
+          fi
+
       - name: Run template version sync
         id: sync
+        if: steps.changes.outputs.has_files == 'true'
         run: |
           echo "Running template version sync script..."
-          chmod +x ./scripts/sync-template-version.sh
-          ./scripts/sync-template-version.sh
+          chmod +x .github/scripts/sync-template-version.sh
+          
+          # Pass changed files to the script
+          .github/scripts/sync-template-version.sh ${{ steps.changes.outputs.files }}
 
-      - name: Check for changes
-        id: changes
+      - name: Check for template.yaml changes
+        id: template_changes
         run: |
           if [[ -n $(git status --porcelain library/**/template.yaml) ]]; then
             echo "has_changes=true" >> $GITHUB_OUTPUT
@@ -44,11 +65,11 @@ jobs:
           fi
 
       - name: Commit and push changes
-        if: steps.changes.outputs.has_changes == 'true'
+        if: steps.template_changes.outputs.has_changes == 'true'
         run: |
           git config --local user.email "github-actions[bot]@users.noreply.github.com"
           git config --local user.name "github-actions[bot]"
-          
+
           git add library/**/template.yaml
           git commit -m "chore: sync template versions with image updates"
           git push

+ 0 - 143
scripts/sync-template-version.sh

@@ -1,143 +0,0 @@
-#!/usr/bin/env bash
-# Sync Docker image versions to template.yaml metadata
-# This script is triggered by GitHub Actions when Renovate updates dependencies
-#
-# Supports:
-# - Docker Compose templates (compose.yaml.j2)
-# - Kubernetes Helm templates (values.yaml, values.yml)
-# - Kubernetes manifest templates (*.j2 files)
-
-set -euo pipefail
-
-# Color output for better readability
-GREEN='\033[0;32m'
-YELLOW='\033[1;33m'
-NC='\033[0m' # No Color
-
-echo "Starting template version sync..."
-
-# Function to extract version from Docker image reference
-# Handles: image:tag, registry/image:tag, registry/namespace/image:tag
-extract_version_from_image() {
-    local image_line="$1"
-    # Extract everything after the last colon, excluding Jinja2 variables
-    echo "$image_line" | sed -E 's/.*:([^:]+)$/\1/' | tr -d ' ' | grep -v '{{' || true
-}
-
-# Function to update template.yaml version
-update_template_version() {
-    local template_file="$1"
-    local new_version="$2"
-    
-    # Get current version from template.yaml
-    local current_version
-    current_version=$(grep -E '^\s*version:\s*' "$template_file" | sed -E 's/.*version:\s*['\''"]?([^'\''"]+)['\''"]?/\1/' | tr -d ' ' || true)
-    
-    # Only update if versions are different
-    if [ -n "$current_version" ] && [ "$new_version" != "$current_version" ]; then
-        echo -e "${GREEN}✓${NC} Updating $template_file: ${YELLOW}$current_version${NC} → ${GREEN}$new_version${NC}"
-        
-        # Update version in template.yaml (cross-platform compatible)
-        if [[ "$OSTYPE" == "darwin"* ]]; then
-            sed -i '' "s/version: .*/version: $new_version/" "$template_file"
-        else
-            sed -i "s/version: .*/version: $new_version/" "$template_file"
-        fi
-        return 0
-    fi
-    return 1
-}
-
-# Counter for updated templates
-updated_count=0
-
-# Process Docker Compose templates
-echo ""
-echo "Scanning Docker Compose templates..."
-while IFS= read -r compose_file; do
-    template_dir=$(dirname "$compose_file")
-    template_file="$template_dir/template.yaml"
-    
-    # Skip if template.yaml doesn't exist
-    [ ! -f "$template_file" ] && continue
-    
-    # Extract the first Docker image version from compose.yaml.j2
-    # Matches: image: repo/name:version or image: name:version
-    # Ignores Jinja2 variables like {{ variable }}
-    version=$(grep -E '^\s*image:\s*[^{]*:[^{}\s]+' "$compose_file" | head -n1 | sed -E 's/.*:([^:]+)$/\1/' | tr -d ' ' || true)
-    
-    # Skip if no version found or if it's a Jinja2 variable
-    if [ -z "$version" ] || [[ "$version" =~ \{\{ ]]; then
-        continue
-    fi
-    
-    # Update template version
-    if update_template_version "$template_file" "$version"; then
-        ((updated_count++))
-    fi
-done < <(find library/compose -type f -name "compose.yaml.j2" 2>/dev/null || true)
-
-# Process Kubernetes Helm templates (values.yaml pattern)
-echo ""
-echo "Scanning Kubernetes Helm templates..."
-while IFS= read -r values_file; do
-    template_dir=$(dirname "$values_file")
-    template_file="$template_dir/template.yaml"
-    
-    # Skip if template.yaml doesn't exist
-    [ ! -f "$template_file" ] && continue
-    
-    # Extract version from Helm values.yaml
-    # Matches repository + tag pattern:
-    #   repository: registry/image
-    #   tag: version
-    version=$(grep -A1 'repository:' "$values_file" | grep 'tag:' | sed -E 's/.*tag:\s*['\''"]?([^'\''" ]+)['\''"]?/\1/' | head -n1 | tr -d ' ' || true)
-    
-    # Skip if no version found or if it's a Jinja2 variable
-    if [ -z "$version" ] || [[ "$version" =~ \{\{ ]]; then
-        continue
-    fi
-    
-    # Update template version
-    if update_template_version "$template_file" "$version"; then
-        ((updated_count++))
-    fi
-done < <(find library/kubernetes -type f \( -name "values.yaml" -o -name "values.yml" \) 2>/dev/null || true)
-
-# Process Kubernetes manifest templates (*.j2 files with image: references)
-echo ""
-echo "Scanning Kubernetes manifest templates..."
-while IFS= read -r manifest_file; do
-    template_dir=$(dirname "$manifest_file")
-    template_file="$template_dir/template.yaml"
-    
-    # Skip if template.yaml doesn't exist
-    [ ! -f "$template_file" ] && continue
-    
-    # Extract the first Docker image version from Kubernetes manifest
-    # Matches: image: repo/name:version or image: name:version
-    # Ignores Jinja2 variables like {{ variable }}
-    version=$(grep -E '^\s*image:\s*[^{]*:[^{}\s]+' "$manifest_file" | head -n1 | sed -E 's/.*:([^:]+)$/\1/' | tr -d ' ' || true)
-    
-    # Skip if no version found or if it's a Jinja2 variable
-    if [ -z "$version" ] || [[ "$version" =~ \{\{ ]]; then
-        continue
-    fi
-    
-    # Update template version
-    if update_template_version "$template_file" "$version"; then
-        ((updated_count++))
-    fi
-done < <(find library/kubernetes -type f -name "*.j2" 2>/dev/null || true)
-
-# Process Terraform/Packer templates if needed in the future
-# (Currently no version syncing implemented for these)
-
-echo ""
-echo "=================================================="
-if [ $updated_count -gt 0 ]; then
-    echo -e "${GREEN}✓${NC} Template version sync complete: ${GREEN}$updated_count${NC} template(s) updated"
-else
-    echo "No template version updates needed"
-fi
-echo "=================================================="