From 273d4572224ca6d680b6ea10049f8553cd36167d Mon Sep 17 00:00:00 2001 From: infogulch Date: Wed, 28 Apr 2021 20:49:41 -0500 Subject: [PATCH] Rebuild image with labels after finding the file; move example to example dir --- docker-artifact.sh | 25 ++++++++++++++++++++----- Dockerfile => example/Dockerfile | 0 example/example.sh | 15 +++++++++++++++ testfile.txt => example/testfile.txt | 0 4 files changed, 35 insertions(+), 5 deletions(-) rename Dockerfile => example/Dockerfile (100%) create mode 100755 example/example.sh rename testfile.txt => example/testfile.txt (100%) diff --git a/docker-artifact.sh b/docker-artifact.sh index 0f69f8c..ed91601 100755 --- a/docker-artifact.sh +++ b/docker-artifact.sh @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + set -e verbose=1 @@ -5,6 +7,11 @@ image="$1" export searchpath="$2" 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 [ $verbose ] && >&2 echo "Exporting image '$image' to temp file '$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'..." 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 [ $(echo "$found" | wc -l) -gt 1 ]; then - >&2 echo "Multiple matches found, aborting:" +if [ $foundcount -gt 1 ]; then + >&2 echo "File was changed in multiple layers, aborting. Found files and layer ids:" >&2 echo "$found" exit 2 +elif [ $foundcount -eq 0 ]; then + >&2 echo "No files found matching '$searchpath'" + exit 3 fi -# print out labels to add during image rebuild 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 diff --git a/Dockerfile b/example/Dockerfile similarity index 100% rename from Dockerfile rename to example/Dockerfile diff --git a/example/example.sh b/example/example.sh new file mode 100755 index 0000000..5c32356 --- /dev/null +++ b/example/example.sh @@ -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 + diff --git a/testfile.txt b/example/testfile.txt similarity index 100% rename from testfile.txt rename to example/testfile.txt