name: Generate Template Hashes on: push: branches: - main - hashs paths: - 'src/main/resources/templates/**' - 'src/main/resources/static/**' workflow_dispatch: # Allow manual triggering jobs: generate-hash: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Calculate template hashes id: hash run: | # Create a script to calculate individual file hashes cat > calculate-hashes.sh << 'EOF' #!/bin/bash set -e # Directories to hash TEMPLATE_DIR="src/main/resources/templates" STATIC_DIR="src/main/resources/static" OUTPUT_FILE="src/main/resources/reference-hash.json" # Create output directory if it doesn't exist mkdir -p $(dirname "$OUTPUT_FILE") # Text file extensions that need normalization TEXT_EXTENSIONS=("html" "htm" "css" "js" "txt" "md" "xml" "json" "csv" "properties") # Function to check if a file is a text file is_text_file() { ext="${1##*.}" for text_ext in "${TEXT_EXTENSIONS[@]}"; do if [ "$ext" = "$text_ext" ]; then return 0 fi done return 1 } # Function to calculate normalized hash for text files calculate_text_hash() { # Normalize line endings to LF and calculate CRC32 tr -d '\r' < "$1" | cksum | awk '{print $1}' } # Function to calculate hash for binary files calculate_binary_hash() { cksum "$1" | awk '{print $1}' } # Start JSON echo "{" > "$OUTPUT_FILE" # Find all files and calculate CRC32 hash FIRST=true find "$TEMPLATE_DIR" "$STATIC_DIR" -type f | sort | while read file; do # Get relative path from src/main/resources REL_PATH=$(echo "$file" | sed 's|^src/main/resources/||') # Calculate hash based on file type if is_text_file "$file"; then HASH=$(calculate_text_hash "$file") else HASH=$(calculate_binary_hash "$file") fi # Add to JSON if [ "$FIRST" = true ]; then FIRST=false else echo "," >> "$OUTPUT_FILE" fi echo " \"$REL_PATH\": \"$HASH\"" >> "$OUTPUT_FILE" done # End JSON echo "}" >> "$OUTPUT_FILE" echo "Generated hashes for $(grep -c ":" "$OUTPUT_FILE") files" EOF chmod +x calculate-hashes.sh ./calculate-hashes.sh - name: Commit and push if changed run: | git config --local user.email "github-actions[bot]@users.noreply.github.com" git config --local user.name "GitHub Actions" git add src/main/resources/reference-hash.json # Only commit if there are changes if git diff --staged --quiet; then echo "No changes to commit" else git commit -m "Update template reference hashes [skip ci]" git push fi