Rebuild image with labels after finding the file; move example to example dir

This commit is contained in:
Joe 2021-04-28 20:49:41 -05:00
parent 4c420c3cf4
commit 273d457222
4 changed files with 35 additions and 5 deletions

View File

@ -1,3 +1,5 @@
#!/usr/bin/env bash
set -e set -e
verbose=1 verbose=1
@ -5,6 +7,11 @@ image="$1"
export searchpath="$2" export searchpath="$2"
export tarfile="$(mktemp)-$image.tar" export tarfile="$(mktemp)-$image.tar"
# check to see if image exists locally
if ! docker image inspect "$image" > /dev/null ; then
exit 1
fi
# Save image tar to temp file # Save image tar to temp file
[ $verbose ] && >&2 echo "Exporting image '$image' to temp file '$tarfile'..." [ $verbose ] && >&2 echo "Exporting image '$image' to temp file '$tarfile'..."
docker image save "$image" -o "$tarfile" docker image save "$image" -o "$tarfile"
@ -30,19 +37,27 @@ export idmap=$(echo "$manifest" "$config" | jq -sr '[ [ .[0][0].Layers, .[1].roo
[ $verbose ] && >&2 echo "Searching layers for '$searchpath'..." [ $verbose ] && >&2 echo "Searching layers for '$searchpath'..."
found=$(echo "$manifest" | jq '.[0].Layers[]' | xargs -I {} sh -c 'digest=$(echo "$idmap" | jq -r ".[\"{}\"]"); tar -f "$tarfile" -x {} -O | tar -t | sed s_^_/_ | grep -wx "$searchpath" | xargs -I [] echo "[]=$digest"') found=$(echo "$manifest" | jq '.[0].Layers[]' | xargs -I {} sh -c 'digest=$(echo "$idmap" | jq -r ".[\"{}\"]"); tar -f "$tarfile" -x {} -O | tar -t | sed s_^_/_ | grep -wx "$searchpath" | xargs -I [] echo "[]=$digest"')
foundcount=$(echo "$found" | grep -c . || true)
# If more than one is found, then bail # If more than one is found, then bail
if [ $(echo "$found" | wc -l) -gt 1 ]; then if [ $foundcount -gt 1 ]; then
>&2 echo "Multiple matches found, aborting:" >&2 echo "File was changed in multiple layers, aborting. Found files and layer ids:"
>&2 echo "$found" >&2 echo "$found"
exit 2 exit 2
elif [ $foundcount -eq 0 ]; then
>&2 echo "No files found matching '$searchpath'"
exit 3
fi fi
# print out labels to add during image rebuild
labels=$(echo "$found" | sed 's_^.*$_--label "\0"_' | paste -d' ') labels=$(echo "$found" | sed 's_^.*$_--label "\0"_' | paste -d' ')
echo "$labels" # Add a layer to the existing image to add the labels and tag the new image with the same image name
[ $verbose ] && >&2 echo "Rebuilding image and adding labels: $labels"
echo "FROM $image" | eval docker build $labels -t "$image" - &> /dev/null
echo "Rebuilt image '$image' with the following added labels:"
docker image inspect "$image" | jq '.[0].Config.Labels'
echo "Run 'docker push $image' to push it to docker hub"
[ $verbose ] && >&2 echo "Done!"
exit exit

15
example/example.sh Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -e
source=$(dirname "${BASH_SOURCE[0]}")
pushd "$source" &> /dev/null
# Build example image
docker build . -t test-image &> /dev/null
# Execute docker artifact to add a label to /app/othertestfile.txt
set +e
../docker-artifact.sh test-image /app/othertestfile.txt
popd &> /dev/null