Skip to content

Commit

Permalink
Fixes #138 #158. Pull image before building image (#159)
Browse files Browse the repository at this point in the history
* Fixes #138 #158. Pull image before building image

This commit fixes error in case of e.g. Fedora registry is not
accessible for temporary reason like HTTP 503.
Second pulling should work usually.

Signed-off-by: Petr "Stone" Hracek <phracek@redhat.com>

* Check if docker image is not already pulled.

Signed-off-by: Petr "Stone" Hracek <phracek@redhat.com>

* remove duplicate code and check docker images for already pulled image

Signed-off-by: Petr "Stone" Hracek <phracek@redhat.com>

* Move image_name definition and check if equals to scratch

Signed-off-by: Petr "Stone" Hracek <phracek@redhat.com>
  • Loading branch information
phracek committed Jul 27, 2020
1 parent 4e27991 commit e65a5f5
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ function clean_image {
fi
}

# Pull image based on FROM, before we build our own.
function pull_image {
local dockerfile="$1"

# Get image_name from Dockerfile before pulling.
while read -r line; do
if ! grep -q "^FROM" <<< "$line"; then
continue
fi

image_name=$(echo "$line" | cut -d ' ' -f2)

# In case FROM scratch is defined, skip it
if [[ x"$image_name" == "xscratch" ]]; then
continue
fi
echo "-> Pulling image $image_name before building image from $dockerfile."
# Sometimes in Fedora case it fails with HTTP 50X
# Check if the image is available locally and try to pull it if it is not
if [[ "$(docker images -q "$image_name" 2>/dev/null)" != "" ]]; then
echo "The image $image_name is already pulled."
continue
fi
# Try pulling the image to see if it is accessible
if ! docker pull "$image_name"; then
echo "Pulling image $image_name failed. Let's wait 2 seconds and try one more time."
sleep 2
docker pull "$image_name"
fi

done < "$dockerfile"
}

# Perform docker build but append the LABEL with GIT commit id at the end
function docker_build_with_version {
local dockerfile="$1"
Expand Down Expand Up @@ -107,6 +140,8 @@ function docker_build_with_version {
fi
fi

pull_image "$dockerfile"

# shellcheck disable=SC2016
parse_output 'docker build '"$BUILD_OPTIONS"' -f "$dockerfile" "${DOCKER_BUILD_CONTEXT}"' \
"tail -n 1 | awk '/Successfully built|(^--> )?(Using cache )?[a-fA-F0-9]+$/{print \$NF}'" \
Expand Down

0 comments on commit e65a5f5

Please sign in to comment.