-
Notifications
You must be signed in to change notification settings - Fork 3
/
y-image-bump
executable file
·80 lines (65 loc) · 2.11 KB
/
y-image-bump
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
#!/usr/bin/env bash
[ -z "$DEBUG" ] || set -x
set -eo pipefail
[ -z "$BUMP_INCLUDE" ] && BUMP_INCLUDE='--include=*.yaml'
[ "$1" = "help" ] && echo "adsf
Replaces image URLs with other image URLs, for git ref tags.
Skips ref changes that do not affect image sha, i.e. cached or deterministic builds.
BUMP_INCLUDE='$BUMP_INCLUDE'
" && exit 0
IMAGE=$1
[ -z "$IMAGE" ] && echo "First arg must be a an image like yolean/node-kafka" && exit 1
TAG=$2
[ -z "$TAG" ] && echo "Second arg must be a tag, like a git ref" && exit 1
SRC=$3
[ -z "$SRC" ] && echo "Third arg must be a path like . or /my/hack.js" && exit 1
[ -z "$REGISTRY" ] && REGISTRY="docker.io"
case $IMAGE in
*.*/*/*)
IMAGE_URL="$IMAGE:$TAG"
REGISTRY=$(echo $IMAGE | cut -d'/' -f 1)
;;
*/*)
IMAGE_URL="$REGISTRY/$IMAGE:$TAG"
;;
*)
echo "image must have format [host?]parent/image" && exit 1
;;
esac
[ ! -e "$SRC" ] && echo "Path $SRC not found" && exit 1
DIGEST=$(y-crane digest $IMAGE_URL)
echo "Got $DIGEST for $IMAGE_URL"
tmp=$(mktemp)
function imagebump_kustomize {
FILE=$1
# newName is currently ignored, if we add support it must take preference over .name
yq -i eval ".images.[] |= select(.name == \"*$IMAGE\" and .newTag != \"*$DIGEST\") |= .newTag = \"$TAG@$DIGEST\"" $FILE
}
function imagebump_text {
FILE=$1
sed "s|\b\($IMAGE:\)\([0123456789abcdef]\{40\}\)\(@sha256:[0123456789abcdef]\{64\}\)\?\b|\1$TAG@$DIGEST|" $FILE > $tmp
diff=$(diff -u --horizon-lines=0 $FILE $tmp || true)
if [ -z "$diff" ]; then
echo "No diff for $FILE"
return
fi
shas=$(echo "$diff" | sed 's|^[-\+].*\(@sha256:[0123456789abcdef]\{64\}\).*$|\1|;t;d')
if [ $(echo "$shas" | sort | uniq | wc -l) -eq 1 ]; then
echo "Skipped $FILE; only one distinct @sha256 in diff"
else
cp $tmp $FILE
echo "Bumped $FILE"
fi
}
function imagebump {
FILE=$1
[ -z "$FILE" ] && echo "bump requires a file path" && exit 1
case $FILE in
*/kustomization.yaml) imagebump_kustomize $FILE ;;
*) imagebump_text $FILE ;;
esac
}
[ -f "$SRC" ] && imagebump $SRC && exit 0
for F in $(grep -lr $BUMP_INCLUDE "$IMAGE" "$SRC"); do
imagebump $F
done