-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
318 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# ref: https://docs.github.com/en/actions/creating-actions/creating-a-docker-container-action | ||
name: 'Check if image with valid checksum exists in dockerhub' | ||
description: 'WARNING: Only works for single platform images' | ||
inputs: | ||
image: | ||
description: 'Docker Image ' | ||
required: true | ||
username: | ||
description: 'DockerHub username ' | ||
required: false | ||
password: | ||
description: 'DockerHub password ' | ||
required: false | ||
architecture: | ||
description: 'DockerHub architecture to build ' | ||
required: false | ||
default: amd64 | ||
|
||
outputs: | ||
checksum: | ||
description: 'Returns build number for the current branch' | ||
value: ${{ steps.check.outputs.checksum }} | ||
build_number: | ||
description: 'Returns build number for the current branch' | ||
value: ${{ steps.check.outputs.build_number }} | ||
build_date: | ||
description: 'Returns tha image build date' | ||
value: ${{ steps.check.outputs.build_date }} | ||
|
||
runs: | ||
using: 'composite' | ||
steps: | ||
- name: Setup | ||
id: setup | ||
shell: bash | ||
run: | | ||
ref=${{ inputs.image }} | ||
architecture=${{ inputs.architecture }} | ||
repo="${ref%:*}" | ||
tag="${ref##*:}" | ||
res=$(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${repo}:pull") | ||
token=$(echo $res | jq -r '.token') | ||
if [[ -z "$token" ]];then | ||
echo "::error title=⛔ error hint::Unable to get valid token" | ||
exit 1 | ||
fi | ||
echo "token=$token" >> $GITHUB_OUTPUT | ||
echo "repo=$repo" >> $GITHUB_OUTPUT | ||
echo "tag=$tag" >> $GITHUB_OUTPUT | ||
echo "architecture=$architecture" >> $GITHUB_OUTPUT | ||
- name: Check Checksum | ||
id: check | ||
shell: bash | ||
run: | | ||
set -e | ||
echo "exists=false" >> "$GITHUB_OUTPUT" | ||
echo "build_number=1" >> "$GITHUB_OUTPUT" | ||
echo "build_date=-" >> "$GITHUB_OUTPUT" | ||
echo "checksum=-" >> "$GITHUB_OUTPUT" | ||
url="https://registry-1.docker.io/v2/${{steps.setup.outputs.repo}}/manifests/${{steps.setup.outputs.tag}}" | ||
manifest=$(curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \ | ||
-H 'Authorization: Bearer ${{steps.setup.outputs.token}}' \ | ||
-s $url) | ||
if [[ $manifest == *MANIFEST_UNKNOWN* ]];then | ||
echo "::error:: Unknown Manifest" | ||
echo "exists=false" >> "$GITHUB_OUTPUT" | ||
exit 0 | ||
fi | ||
if [[ $manifest == *errors\":* ]];then | ||
code=$(echo $manifest | jq .errors[0].code) | ||
message=$(echo $manifest | jq .errors[0].message) | ||
echo "::error title=$code error hint::$message https://registry-1.docker.io/v2/${repo}/manifests/${tag}" | ||
exit 1 | ||
fi | ||
if [[ -z "$manifest" ]];then | ||
echo "::error title=⛔ error hint::Unable to get manifest from $url" | ||
exit 1 | ||
fi | ||
check1=$(echo $manifest | jq 'try(.manifests[])') | ||
check2=$(echo $manifest | jq 'try(.config.digest)') | ||
if [[ -n "$check1" ]]; then | ||
digest=$(echo $manifest | jq -r ".manifests| map(select(.platform.architecture | contains (\"${{steps.setup.outputs.architecture}}\"))) | .[].digest" 2>&1) | ||
elif [[ -n "$check2" ]]; then | ||
digest=$(echo $manifest | jq -r '.config.digest') | ||
else | ||
echo "::error title=⛔ error hint::Unable to detect digest" | ||
exit 1 | ||
fi | ||
if [[ $digest == null ]]; then | ||
echo "::error title=⛔ error hint::Digest is null" | ||
exit 1 | ||
fi | ||
if [[ -z "$digest" ]];then | ||
echo "::error title=⛔ error hint::Digest is empty" | ||
exit 1 | ||
fi | ||
url=https://registry-1.docker.io/v2/${{steps.setup.outputs.repo}}/blobs/$digest | ||
blob=$(curl \ | ||
--silent \ | ||
--location \ | ||
-H "Accept: application/vnd.docker.disribution.manifest.v2+json" \ | ||
-H 'Authorization: Bearer ${{steps.setup.outputs.token}}' \ | ||
$url ) | ||
if [[ -z "$blob" ]]; then | ||
echo "::error:: Empty Blob" | ||
exit 1 | ||
fi | ||
if [[ $blob == *BLOB_UNKNOWN* ]];then | ||
echo "exists=false" >> "$GITHUB_OUTPUT" | ||
echo "::error:: Unknown Blob at $url" | ||
exit 0 | ||
fi | ||
build_number=$(echo $blob | jq '.config.Labels.BuildNumber') | ||
checksum=$(echo $blob | jq -r '.config.Labels.checksum') | ||
build_date=$(echo $blob | jq -r '.config.Labels.date') | ||
if [[ $build_number =~ '^[0-9]+$' ]] ; then | ||
build_number=$(( build_number + 1 )) | ||
else | ||
build_number=1 | ||
fi | ||
echo "checksum=${checksum}" >> $GITHUB_OUTPUT | ||
echo "build_number=${build_number}" >> $GITHUB_OUTPUT | ||
echo "build_date=${build_date}" >> $GITHUB_OUTPUT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.