Skip to content

Commit

Permalink
Fix retrieving manifest for single platform
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome committed Jan 28, 2023
1 parent 99580de commit 8c6e91c
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ get_manifests() {
local digest=$2
local token=$3

manifest_list=$(curl -sSL -H "Authorization: Bearer $token" \
-H "Accept: application/vnd.docker.distribution.manifest.list.v2+json,application/vnd.oci.image.index.v1+json" \
manifest_list=$(curl -sSL -w "{\"digest\": \"%header{docker-content-digest}\", \"type\": \"%header{content-type}\"}" -H "Authorization: Bearer $token" \
-H "Accept: application/vnd.docker.distribution.manifest.list.v2+json,application/vnd.oci.image.index.v1+json,application/vnd.docker.distribution.manifest.v2+json" \
"https://index.docker.io/v2/${repo}/manifests/${digest}" 2>/dev/null)

if jq -e -r '.errors[0].code' <<<"$manifest_list" >/dev/null; then
error_code=$(jq -r '.errors[0].code' <<<"$manifest_list")
message=$(jq -r '.errors[0].message' <<<"$manifest_list")
if jq -e -sr '.[0].errors[0].code' <<<"$manifest_list" >/dev/null; then
error_code=$(jq -sr '.[0].errors[0].code' <<<"$manifest_list")
message=$(jq -sr '.[0].errors[0].message' <<<"$manifest_list")
error "Response from $repo\n code: $error_code\n message: $message"
fi

jq -r '[.manifests[] | select(.platform.architecture | contains ("unknown") | not) | {digest: .digest, platform: (.platform.os +"/"+ .platform.architecture)}]' <<<"$manifest_list"
manifest_v2=$(jq -sr '.[1] | select(.type == "application/vnd.docker.distribution.manifest.v2+json") | [{digest: .digest, platform: "linux/amd64"}]' <<<"$manifest_list")
if [ ! -z "$manifest_v2" ]; then
echo "$manifest_v2"
return
fi

jq -sr '[.[0].manifests[] | select(.platform.architecture | contains ("unknown") | not) | {digest: .digest, platform: (.platform.os +"/"+ .platform.architecture)}]' <<<"$manifest_list"

}

Expand All @@ -37,7 +43,7 @@ get_layers() {
local token=$3

digestOutput=$(curl -sSL -H "Authorization: Bearer $token" \
-H "Accept: application/vnd.docker.distribution.manifest.v1+json,application/vnd.oci.image.manifest.v1+json" \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json,application/vnd.oci.image.manifest.v1+json" \
"https://index.docker.io/v2/${repo}/manifests/${digest}" 2>/dev/null)

if jq -e -r '.errors[0].code' <<<"$digestOutput" >/dev/null; then
Expand Down Expand Up @@ -102,7 +108,7 @@ manifests_image=$(get_manifests $image_repo ${image_tag:-latest} $image_token)

diff=false
# loop through plafforms split by comma
for platform in $(echo $platforms | tr ',' ' '); do
for platform in $(echo $platforms | tr -s ',' ' '); do
# get the digest for the platform
digest_base=$(jq -r ".[] | select(.platform == \"$platform\") | .digest" <<<"$manifests_base")

Expand Down

0 comments on commit 8c6e91c

Please sign in to comment.