Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically detect if prefix library/ is needed #11

Merged
merged 3 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ on:
pull_request:

jobs:
test:
test1:
name: Test-Action
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
continue-on-error: true
steps:
- name: Checkout Repository
Expand All @@ -26,3 +26,21 @@ jobs:
- name: Check value
run: echo "Needs updating"
if: steps.test.outputs.needs-updating == 'true'
test2:
name: Test-Action
runs-on: ubuntu-22.04
continue-on-error: true
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Test Action
id: test
uses: ./
with:
base-image: nginx:1.21.0
image: library/nginx:1.21.0
- name: Get Test Output
run: echo "Workflow Docker Image ${{ steps.test.outputs.needs-updating }}"
- name: Check value
run: echo "Does not need updating"
if: steps.test.outputs.needs-updating == 'false'
28 changes: 24 additions & 4 deletions docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ get_layers() {
digestOutput=$(curl -H "Authorization: Bearer $token" \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
"https://index.docker.io/v2/${repo}/manifests/${digest}" 2>/dev/null)

jq -r '[.layers[].digest]' <<<"$digestOutput"

if jq -e -r '.errors[0].code' <<<"$digestOutput" >/dev/null; then
jq -r '.errors[0].code' <<<"$digestOutput"
else
jq -r '[.layers[].digest]' <<<"$digestOutput"
fi
}

get_token() {
Expand All @@ -25,10 +29,26 @@ get_token() {
curl "$url" 2>/dev/null | jq -r '.token'
}

# if we get a "UNAUTHORIZED" response and the $base does not match a image with username -> fallback to a version with "library" as prefix
retry_if_necessary() {
local IMAGE_PATTERN_WITH_USERNAME="^.+/.+$"
local repo=$1
local digest=$2
local result

result=$(get_layers "$repo" "$digest")

if [[ $result == "UNAUTHORIZED" ]] && ! [[ $repo =~ $IMAGE_PATTERN_WITH_USERNAME ]] ; then
result=$(get_layers "library/$repo" "$digest")
fi

echo "$result"
}

IFS=: read base base_tag <<<$base
IFS=: read image image_tag <<<$image

layers_base=$(get_layers $base ${base_tag:-latest})
layers_image=$(get_layers $image ${image_tag:-latest})
layers_base=$(retry_if_necessary $base ${base_tag:-latest})
layers_image=$(retry_if_necessary $image ${image_tag:-latest})

jq '.base-.image | .!=[]' <<<"{\"base\": $layers_base, \"image\": $layers_image }"