dev: update helper script to change alpine base image (#28128)

Previously the helper script only supported changing the tag@digest. With this
change the script supports changing the image name too.
This commit is contained in:
Stefan Hengl 2021-11-25 11:23:14 +01:00 committed by GitHub
parent 6eb4943174
commit d636fa561d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,18 @@
#!/usr/bin/env bash
# Update all Docker images with a new alpine base image.
# Before running this script, update the alpine image and merge into main. CI will then push the new image
# to Docker Hub. Observe the tag of the new image and call this script to update all Docker images.
#
# Usage:
# $ ./update-all-dockerfiles.sh <old image> <new image> <tag>
#
# Example:
# $ ./update-all-dockerfiles.sh sourcegraph/alpine-1.12 sourcegraph/alpine-1.14 117627_2021-11-24_7ab43a9
OLD_IMAGE=$1
NEW_IMAGE=$2
NEW_TAG=$3
cd "$(dirname "${BASH_SOURCE[0]}")"/../..
set -euo pipefail
@ -10,13 +24,21 @@ check_sd_installed() {
fi
}
check_fd_installed() {
if ! command -v fd &>/dev/null; then
echo "'fd' command not installed. Please install 'fd' by following the instructions on https://github.com/sharkdp/fd"
exit 1
fi
}
update_image_reference() {
local old_image_stub="$1"
local new_tag_and_digest="$2"
local file="$3"
local new_image_stub="$2"
local new_tag_and_digest="$3"
local file="$4"
local original="(?P<repo>$old_image_stub:)(\S*@sha256:\S*)"
local replacement="\${repo}$new_tag_and_digest"
local original="$old_image_stub:(\S*@sha256:\S*)"
local replacement="$new_image_stub:$new_tag_and_digest"
sd "$original" "$replacement" "$file"
}
@ -34,17 +56,14 @@ get_new_tag_and_digest() {
}
check_sd_installed
REPO="sourcegraph/alpine-3.14"
check_fd_installed
MISSING_MESSAGE="Please provide the image tag either via the 'TAG' environent variable or as a shell script argument"
TAG="${TAG:-${1:?"$MISSING_MESSAGE"}}"
TAG="${TAG:-${NEW_TAG:?"$MISSING_MESSAGE"}}"
NEW_TAG_AND_DIGEST="$(get_new_tag_and_digest "$REPO" "$TAG")"
NEW_TAG_AND_DIGEST="$(get_new_tag_and_digest "$NEW_IMAGE" "$TAG")"
DOCKERFILES=()
mapfile -t DOCKERFILES < <(fd --glob Dockerfile .)
for file in "${DOCKERFILES[@]}"; do
update_image_reference "$REPO" "$NEW_TAG_AND_DIGEST" "$file"
for file in $(fd --glob Dockerfile .); do
update_image_reference "$OLD_IMAGE" "$NEW_IMAGE" "$NEW_TAG_AND_DIGEST" "$file"
echo "$file"
done