-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GTC-2958 Tag Docker image from docker hash, so it always exists
Currently, we tag the new Docker image with the current Git SHA. But the container_registry module only creates a new Docker image if the docker contents change. So, if the docker contents haven't changed with this Git change, we can have a bug where we reference the app docker via a tag (of the new Git SHA) which doesn't exist. The fix is to use instead as a tag the hash of the docker contents. We use the same hash script that the container_registry module uses. Therefore, we will always be using a tag that exists, either because the container_register module just pushed a new docker with the new tag, or the docker already exists under the docker hash tag, because the docker contents and has haven't changed. I noticed a bunch of bugs in the container_registry module's hash.sh script, which I will fix later. One of the main things is that it doesn't ignore comments, so it can match on words in the comments. For that reason, I removed the '# Docker Files' comment, which was causing the Dockerfile itself to be ignored during hashing.
- Loading branch information
Showing
4 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/bin/bash | ||
# | ||
# Calculates hash of Docker image source contents | ||
# | ||
# Must be identical to the script that is used by the | ||
# gfw-terraform-modules:terraform/modules/container_registry Terraform module. | ||
# | ||
# Usage: | ||
# | ||
# $ ./hash.sh . | ||
# | ||
|
||
set -e | ||
|
||
pushd () { | ||
command pushd "$@" > /dev/null | ||
} | ||
|
||
popd () { | ||
command popd "$@" > /dev/null | ||
} | ||
|
||
ROOT_DIR=${1:-.} | ||
DOCKER_PATH=${2:-.} | ||
IGNORE="${DOCKER_PATH}/.dockerignore" | ||
|
||
pushd "$ROOT_DIR" | ||
|
||
# Hash all source files of the Docker image | ||
if [ -f "$IGNORE" ]; then | ||
# We don't want to compute hashes for files listed in .dockerignore | ||
# to match regex pattern we need to escape leading . | ||
a=$(printf "! -regex ^./%s.* " `< .dockerignore`) | ||
b=${a//\/.//\\\.} | ||
|
||
file_hashes="$( | ||
find . -type f $b -exec md5sum {} \; | ||
)" | ||
else | ||
# Exclude Python cache files, dot files | ||
file_hashes="$( | ||
find . -type f -not -name '*.pyc' -not -path './.**' -exec md5sum {} \; | ||
)" | ||
fi | ||
|
||
popd | ||
|
||
hash="$(echo "$file_hashes" | md5sum | cut -d' ' -f1)" | ||
|
||
echo '{ "hash": "'"$hash"'" }' |