Skip to content

Commit

Permalink
2024-06-14 22:43 - updates
Browse files Browse the repository at this point in the history
  • Loading branch information
saxix committed Jun 14, 2024
1 parent b5113f3 commit fb59326
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 89 deletions.
138 changes: 69 additions & 69 deletions .github/actions/docker_build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,76 +67,76 @@ outputs:
runs:
using: 'composite'
steps:
- name: Output Inputs
shell: bash
run: |
echo ""
echo "${{ toJSON(inputs) }}"
echo "====================================="
# - run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Calculate Release Hash
id: release_hash
uses: ./.github/actions/distro_hash
- id: image_updated
name: Check if image exists and updated
uses: ./.github/actions/image_updated
with:
image: ${{inputs.image}}
checksum: ${{ steps.release_hash.outputs.hash }}
username: ${{ inputs.username }}
password: ${{ inputs.password }}
- id: setup
name: setup
shell: bash
run: |
tag="${{ inputs.image }}"
build_date=$(date +"%Y-%m-%d %H:%M")
echo "date=$build_date" >> $GITHUB_OUTPUT
echo "image=${tag%:*}" >> $GITHUB_OUTPUT
- name: Retrieve Last Commit Hash
id: last_commit
uses: ./.github/actions/last_commit
- shell: bash
run: |
echo ""
echo "run_id : ${{ github.run_id }}"
echo "date : ${{steps.setup.outputs.date}}"
echo "image : ${{steps.setup.outputs.image}}"
echo "tag : ${{inputs.image}}"
echo "target : ${{inputs.target}}"
echo "target-checksum : ${{inputs.checksum}}"
echo "docker-checksum : ${{steps.image_updated.outputs.checksum }}"
echo "image_exists : ${{steps.image_updated.outputs.exists}}"
echo "image_updated : ${{steps.image_updated.outputs.updated}}"
echo "build_number : ${{steps.image_updated.outputs.build_number}}"
echo "build_date : ${{steps.image_updated.outputs.build_date}}"
echo "force : ${{inputs.force}}"
echo "build : ${{steps.image_updated.outputs.updated != 'true' || inputs.force == 'true'}}"
echo "commit_sha : ${{env.LAST_COMMIT_SHA}}"
echo "====================================="
echo "BUILD_DATE=${{ steps.setup.outputs.date }}
echo "CHECKSUM=${{ steps.release_hash.outputs.hash }}
echo "VERSION=${{ steps.version.outputs.version }}
echo "SOURCE_COMMIT=${{ env.LAST_COMMIT_SHA }}
echo "GITHUB_SERVER_URL=${{ github.server_url }}
echo "GITHUB_REPOSITORY=${{ github.repository }}
echo "====================================="
- uses: docker/login-action@v3
with:
username: ${{ inputs.username }}
password: ${{ inputs.password }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
platforms: linux/amd64
driver: docker-container
driver-opts: 'image=moby/buildkit:v0.13.2'
# - name: Output Inputs
# shell: bash
# run: |
# echo ""
# echo "${{ toJSON(inputs) }}"
# echo "====================================="
## - run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
# - name: Checkout code
# uses: actions/checkout@v4
# with:
# fetch-depth: 0
# - name: Calculate Release Hash
# id: release_hash
# uses: ./.github/actions/distro_hash
# - id: image_updated
# name: Check if image exists and updated
# uses: ./.github/actions/image_updated
# with:
# image: ${{inputs.image}}
# checksum: ${{ steps.release_hash.outputs.hash }}
# username: ${{ inputs.username }}
# password: ${{ inputs.password }}
# - id: setup
# name: setup
# shell: bash
# run: |
# tag="${{ inputs.image }}"
# build_date=$(date +"%Y-%m-%d %H:%M")
# echo "date=$build_date" >> $GITHUB_OUTPUT
# echo "image=${tag%:*}" >> $GITHUB_OUTPUT
# - name: Retrieve Last Commit Hash
# id: last_commit
# uses: ./.github/actions/last_commit
# - shell: bash
# run: |
# echo ""
# echo "run_id : ${{ github.run_id }}"
# echo "date : ${{steps.setup.outputs.date}}"
# echo "image : ${{steps.setup.outputs.image}}"
# echo "tag : ${{inputs.image}}"
# echo "target : ${{inputs.target}}"
# echo "target-checksum : ${{inputs.checksum}}"
# echo "docker-checksum : ${{steps.image_updated.outputs.checksum }}"
# echo "image_exists : ${{steps.image_updated.outputs.exists}}"
# echo "image_updated : ${{steps.image_updated.outputs.updated}}"
# echo "build_number : ${{steps.image_updated.outputs.build_number}}"
# echo "build_date : ${{steps.image_updated.outputs.build_date}}"
# echo "force : ${{inputs.force}}"
# echo "build : ${{steps.image_updated.outputs.updated != 'true' || inputs.force == 'true'}}"
# echo "commit_sha : ${{env.LAST_COMMIT_SHA}}"
# echo "====================================="
# echo "BUILD_DATE=${{ steps.setup.outputs.date }}
# echo "CHECKSUM=${{ steps.release_hash.outputs.hash }}
# echo "VERSION=${{ steps.version.outputs.version }}
# echo "SOURCE_COMMIT=${{ env.LAST_COMMIT_SHA }}
# echo "GITHUB_SERVER_URL=${{ github.server_url }}
# echo "GITHUB_REPOSITORY=${{ github.repository }}
# echo "====================================="
# - uses: docker/login-action@v3
# with:
# username: ${{ inputs.username }}
# password: ${{ inputs.password }}
# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v3
# with:
# platforms: linux/amd64
# driver: docker-container
# driver-opts: 'image=moby/buildkit:v0.13.2'
- name: Build and push
if: ${{ steps.image_updated.outputs.updated != 'true' || inputs.force == 'true' }}
# if: ${{ steps.image_updated.outputs.updated != 'true' || inputs.force == 'true' }}
uses: docker/build-push-action@v5
with:
context: .
Expand Down
127 changes: 127 additions & 0 deletions .github/actions/image_meta/action.yml
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
4 changes: 2 additions & 2 deletions .github/actions/last_commit/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ runs:
if: ${{ github.event_name == 'pull_request' }}
shell: bash
run: |
echo "LAST_COMMIT_SHA=${{ github.event.pull_request.head.sha }}" >> ${GITHUB_ENV}
echo "LAST_COMMIT_SHA=${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT
- name: Setup Environment (Push)
if: ${{ github.event_name == 'push' }}
shell: bash
run: |
echo "LAST_COMMIT_SHA=${GITHUB_SHA}" >> ${GITHUB_ENV}
echo "LAST_COMMIT_SHA=${GITHUB_SHA}" >> $GITHUB_OUTPUT
Loading

0 comments on commit fb59326

Please sign in to comment.