|
| 1 | +#!/usr/bin/env bash |
| 2 | +#========================================================= |
| 3 | +# Script: dockerfile_diff_checker.sh |
| 4 | +# Purpose: Scan a directory tree for Dockerfile.konflux.* |
| 5 | +# and compare each with its original Dockerfile, |
| 6 | +# ignoring comments and multi-line LABEL instructions. |
| 7 | +#========================================================= |
| 8 | + |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +#--------------------------------------------------------- |
| 12 | +# Main script execution |
| 13 | +#--------------------------------------------------------- |
| 14 | +main() { |
| 15 | + |
| 16 | + # Define multiple starting directories |
| 17 | + local start_dirs=("./jupyter" "./codeserver" "./runtimes") |
| 18 | + echo "Scanning ${start_dirs[*]} for directories containing Dockerfile.konflux.*" |
| 19 | + echo "Comparing Dockerfiles, ignoring comments and LABEL blocks..." |
| 20 | + |
| 21 | + # Populate array of directories |
| 22 | + local docker_dirs=() |
| 23 | + for dir in "${start_dirs[@]}"; do |
| 24 | + mapfile -t dirs < <(find_docker_dirs "$dir") |
| 25 | + docker_dirs+=("${dirs[@]}") |
| 26 | + done |
| 27 | + |
| 28 | + # Process all directories and check for differences |
| 29 | + if process_dirs "${docker_dirs[@]}"; then |
| 30 | + echo "✅ All Dockerfiles are in sync." |
| 31 | + else |
| 32 | + echo "❌ Differences were found. Please inspect." |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | +} |
| 36 | + |
| 37 | +#--------------------------------------------------------- |
| 38 | +# Function: find_docker_dirs |
| 39 | +# Description: |
| 40 | +# Recursively search for directories containing files named |
| 41 | +# "Dockerfile.konflux.*". Each directory is listed only once. |
| 42 | +# Arguments: |
| 43 | +# $1 - starting directory |
| 44 | +# Returns: |
| 45 | +# Prints each directory path containing at least one |
| 46 | +# Dockerfile.konflux.* file |
| 47 | +#--------------------------------------------------------- |
| 48 | +find_docker_dirs() { |
| 49 | + local start_dir="$1" |
| 50 | + |
| 51 | + # Use 'find' to locate matching files, then 'dirname' to extract directories, 'sort -u' to remove duplicates |
| 52 | + find "$start_dir" -type f -name "Dockerfile.konflux.*" -exec dirname {} \; | sort -u |
| 53 | +} |
| 54 | + |
| 55 | +#--------------------------------------------------------- |
| 56 | +# Function: strip |
| 57 | +# Description: |
| 58 | +# Remove comments and ignore LABEL blocks from a Dockerfile. |
| 59 | +# Useful for comparing Dockerfiles while ignoring cosmetic differences. |
| 60 | +# Arguments: |
| 61 | +# Reads from standard input |
| 62 | +# Returns: |
| 63 | +# Prints stripped Dockerfile to standard output |
| 64 | +#--------------------------------------------------------- |
| 65 | +strip() { |
| 66 | + awk ' |
| 67 | + BEGIN { in_label = 0 } |
| 68 | +
|
| 69 | + # Skip full-line comments |
| 70 | + /^[[:space:]]*#/ { next } |
| 71 | +
|
| 72 | + # Skip lines inside multi-line LABEL blocks |
| 73 | + in_label { |
| 74 | + # End LABEL block if line does not end with backslash |
| 75 | + if ($0 !~ /\\$/) in_label = 0 |
| 76 | + next |
| 77 | + } |
| 78 | +
|
| 79 | + # Detect start of LABEL instruction |
| 80 | + /^[[:space:]]*LABEL([ \t]|$)/ { |
| 81 | + # Multi-line LABEL block |
| 82 | + if ($0 ~ /\\$/) in_label = 1 |
| 83 | + next |
| 84 | + } |
| 85 | +
|
| 86 | + # Print all other lines (trimmed) |
| 87 | + { |
| 88 | + # Trim leading and trailing whitespace |
| 89 | + gsub(/^[ \t]+|[ \t]+$/, "", $0) |
| 90 | + if (length($0) > 0) print $0 |
| 91 | + } |
| 92 | + ' |
| 93 | +} |
| 94 | + |
| 95 | +#--------------------------------------------------------- |
| 96 | +# Function: find_diff |
| 97 | +# Description: |
| 98 | +# Compare a Dockerfile with its corresponding konflux version, |
| 99 | +# ignoring comments and LABEL blocks. Returns 1 if differences exist. |
| 100 | +# Arguments: |
| 101 | +# $1 - Directory containing the Dockerfiles |
| 102 | +# $2 - Original Dockerfile name (basename) |
| 103 | +# $3 - Konflux Dockerfile name (basename) |
| 104 | +# Returns: |
| 105 | +# 0 if no differences, 1 if differences exist |
| 106 | +#--------------------------------------------------------- |
| 107 | +find_diff() { |
| 108 | + local dir="$1" |
| 109 | + local file_orig="$2" |
| 110 | + local file_konflux="$3" |
| 111 | + |
| 112 | + echo "---- diff $file_orig $file_konflux ----" |
| 113 | + |
| 114 | + # Use process substitution to feed stripped files to diff |
| 115 | + local diff_output |
| 116 | + diff_output=$(diff --color=always <(strip <"$dir/$file_orig") <(strip <"$dir/$file_konflux") || true) |
| 117 | + |
| 118 | + if [ -n "$diff_output" ]; then |
| 119 | + echo "❌ Differences found between $file_orig and $file_konflux" |
| 120 | + # Uncomment the next line to see detailed differences |
| 121 | + echo "$diff_output" |
| 122 | + return 1 |
| 123 | + else |
| 124 | + echo "✅ No differences" |
| 125 | + return 0 |
| 126 | + fi |
| 127 | +} |
| 128 | + |
| 129 | +#--------------------------------------------------------- |
| 130 | +# Function: process_dirs |
| 131 | +# Description: |
| 132 | +# Iterate over a list of directories, find konflux Dockerfiles |
| 133 | +# in each, and compare them with their originals. |
| 134 | +# Arguments: |
| 135 | +# Array of directories |
| 136 | +# Returns: |
| 137 | +# 0 if all Dockerfiles match, 1 if any differences exist |
| 138 | +#--------------------------------------------------------- |
| 139 | +process_dirs() { |
| 140 | + local dirs=("$@") |
| 141 | + local diff_found=0 |
| 142 | + |
| 143 | + # Iterate over each directory |
| 144 | + for dir in "${dirs[@]}"; do |
| 145 | + echo "=== Processing $dir ===" |
| 146 | + |
| 147 | + # Iterate over each konflux Dockerfile |
| 148 | + for konflux_file in "$dir"/Dockerfile.konflux.*; do |
| 149 | + [ -e "$konflux_file" ] || continue |
| 150 | + |
| 151 | + # Derive the original Dockerfile name by removing the .konflux |
| 152 | + local dockerfile="${konflux_file/.konflux/}" |
| 153 | + |
| 154 | + # Check if the original Dockerfile exists |
| 155 | + if [ ! -f "$dockerfile" ]; then |
| 156 | + echo "⚠️ $dockerfile not found in $dir" >&2 |
| 157 | + continue |
| 158 | + fi |
| 159 | + |
| 160 | + # Compare the files |
| 161 | + if ! find_diff "$dir" "$(basename "$dockerfile")" "$(basename "$konflux_file")"; then |
| 162 | + diff_found=1 |
| 163 | + fi |
| 164 | + done |
| 165 | + done |
| 166 | + |
| 167 | + return $diff_found |
| 168 | +} |
| 169 | + |
| 170 | +# Call main with all script arguments |
| 171 | +main "$@" |
0 commit comments