-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkustdiff
executable file
·106 lines (85 loc) · 2.69 KB
/
kustdiff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env bash
set -eux
TMP_DIR="$(mktemp -d)"
ROOT_DIR="${INPUT_ROOT_DIR:-${1:-./kustomize}}" # First check env var, then argument, then default
MAX_DEPTH="${INPUT_MAX_DEPTH:-2}" # Default to 2 if not specified
DEBUG="${DEBUG:-true}"
function debug_log() {
if [ "$DEBUG" = "true" ]; then
printf "[DEBUG] %s \n" "$1"
fi
}
function validate_root_dir() {
if [ ! -d "$ROOT_DIR" ]; then
echo "Error: Root directory '$ROOT_DIR' does not exist"
exit 1
fi
}
function validate_max_depth() {
if ! [[ "$MAX_DEPTH" =~ ^[0-9]+$ ]]; then
echo "Error: max_depth must be a positive integer, got: $MAX_DEPTH"
exit 1
fi
}
function get_targets {
find "$ROOT_DIR" -maxdepth "$MAX_DEPTH" -name kustomization.yaml -exec dirname {} \;
}
function safe_dirname() {
echo "$1" | sed 's/[^a-zA-Z0-9.]/_/g'
}
function safe_filename() {
echo "$1" | sed 's/[^a-zA-Z0-9.]/_/g'
}
function build {
local ref="$1"
local safe_ref=$(safe_filename "$ref")
echo "Checking out ref: $ref"
git checkout "$ref" --quiet
mkdir -p "$TMP_DIR/$safe_ref"
for envpath in $(get_targets); do
local relative_path="${envpath#$ROOT_DIR/}"
local safe_path=$(safe_filename "$relative_path")
local output_file="$TMP_DIR/$safe_ref/${safe_path}.yaml"
echo "Running kustomize for $envpath"
kustomize build "$envpath" -o "$output_file"
# debug_log "Contents of $output_file:"
# debug_log "$(cat "$output_file")"
# debug_log "End of $output_file"
# debug_log "------------------------------------"
done
}
function main {
# Validate inputs before proceeding
validate_root_dir
validate_max_depth
git config --global --add safe.directory "$GITHUB_WORKSPACE"
local diff escaped_output output
build "$INPUT_HEAD_REF"
build "$INPUT_BASE_REF"
local safe_head_ref=$(safe_dirname "$INPUT_HEAD_REF")
local safe_base_ref=$(safe_dirname "$INPUT_BASE_REF")
set +e
diff=$(git diff --no-index "$TMP_DIR/$safe_base_ref" "$TMP_DIR/$safe_head_ref")
debug_log "Git diff output:"
debug_log "$diff"
debug_log "End of git diff output"
debug_log "------------------------------------"
if [[ -z "$diff" ]]; then
output="No differences found between $INPUT_BASE_REF and $INPUT_HEAD_REF"
else
# Just pass through the raw git diff output
output="$diff"
fi
escaped_output=${output//$'\n'/'%0A'}
if [ ${#escaped_output} -gt 65000 ]; then
escaped_output="Output is greater than 65000 characters, and therefore too large to print as a github comment."
fi
echo "::set-output name=diff::$escaped_output"
}
# Print initial configuration
echo "Configuration:"
echo "ROOT_DIR: $ROOT_DIR"
echo "MAX_DEPTH: $MAX_DEPTH"
echo "DEBUG: $DEBUG"
debug_log "Debug mode is enabled"
main