diff --git a/.github/actions/docker_build/action.yml b/.github/actions/docker_build/action.yml index 713e0b4e..44a6dc5c 100644 --- a/.github/actions/docker_build/action.yml +++ b/.github/actions/docker_build/action.yml @@ -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: . diff --git a/.github/actions/image_meta/action.yml b/.github/actions/image_meta/action.yml new file mode 100644 index 00000000..fe8d43cb --- /dev/null +++ b/.github/actions/image_meta/action.yml @@ -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 diff --git a/.github/actions/last_commit/action.yml b/.github/actions/last_commit/action.yml index 488e226b..771e2fa0 100644 --- a/.github/actions/last_commit/action.yml +++ b/.github/actions/last_commit/action.yml @@ -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} \ No newline at end of file + echo "LAST_COMMIT_SHA=${GITHUB_SHA}" >> $GITHUB_OUTPUT \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ec73bb49..4b4e3e7f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,15 +33,23 @@ jobs: run: shell: bash outputs: - test_files: ${{ steps.changes.outputs.run_tests }} - docker: ${{ steps.changes.outputs.docker_base }} - python_files: ${{ steps.changes.outputs.python }} - branch: ${{ steps.extract_branch.outputs.branch }} - hash: ${{ steps.release_hash.outputs.hash }} - tags: ${{ steps.meta.outputs.tags }} - tag: ${{ steps.meta.outputs.version }} - version: ${{ steps.version.outputs.version }} - image_name: ${{ steps.image_name.outputs.name }} + checksum: ${{ steps.release_hash.outputs.hash }} + branch: ${{ steps.env.outputs.branch }} + date: ${{ steps.env.outputs.date }} + test_image: ${{ steps.env.outputs.test_image }} + dist_image: ${{ steps.env.outputs.dist_image }} + version: ${{ steps.env.outputs.version }} + last_commit: ${{ steps.env.outputs.last_commit }} + + image_build_date: ${{ steps.env2.outputs.image_build_date }} + image_build_number: ${{ steps.env2.outputs.image_build_number }} + image_checksum: ${{ steps.env2.outputs.image_checksum }} + updated: ${{ steps.env2.outputs.updated }} +# docker: ${{ steps.changes.outputs.docker_base }} +# python_files: ${{ steps.changes.outputs.python }} +# tags: ${{ steps.meta.outputs.tags }} +# tag: ${{ steps.meta.outputs.version }} +# image_name: ${{ steps.image_name.outputs.name }} steps: # - run: git config --global --add safe.directory $(realpath .) - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 @@ -52,22 +60,116 @@ jobs: base: ${{ github.ref }} token: ${{ github.token }} filters: .github/file-filters.yml - - id: extract_branch - name: Extract branch name - run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT +# - name: Docker meta +# id: meta +# uses: docker/metadata-action@v5 - id: release_hash uses: ./.github/actions/distro_hash - - name: Docker meta + - id: last_commit + uses: ./.github/actions/last_commit + - id: env + run: | + branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}} + build_date=$(date +"%Y-%m-%d %H:%M") + ver="${{steps.meta.outputs.version}}" + + ver="${branch/\//-}" + + echo "version=$ver" >> $GITHUB_OUTPUT + echo "date=$build_date" >> $GITHUB_OUTPUT + echo "branch=$branch" >> $GITHUB_OUTPUT + + echo "test_image=${{vars.DOCKER_IMAGE}}:test-$ver" >> $GITHUB_OUTPUT + echo "dist_image=${{vars.DOCKER_IMAGE}}:$ver" >> $GITHUB_OUTPUT + + echo "last_commit=${{steps.last_commit.outputs.LAST_COMMIT_SHA}}" >> $GITHUB_OUTPUT + - name: Image meta id: meta - uses: docker/metadata-action@v5 - - id: version + uses: ./.github/actions/image_meta + with: + image: ${{steps.env.outputs.test_image}} + username: ${{ inputs.username }} + password: ${{ inputs.password }} + - id: env2 run: | - br=${{ steps.extract_branch.outputs.branch }} - echo "version=${br##*/}" >> $GITHUB_OUTPUT + echo "image_build_date=${{steps.meta.outputs.build_date}}" >> $GITHUB_OUTPUT + echo "image_build_number=${{steps.meta.outputs.build_number}}" >> $GITHUB_OUTPUT + echo "image_checksum=${{steps.meta.outputs.checksum}}" >> $GITHUB_OUTPUT + if [[ "${{steps.meta.outputs.checksum}}" == "${{needs.release_hash.outputs.hash}}" ]];then + echo "updated=true" >> $GITHUB_OUTPUT + else + echo "updated=false" >> $GITHUB_OUTPUT + fi +# echo "${{ toJSON(steps.meta.outputs) }}" +# echo "build_number=${{steps.meta.outputs.build_number}}" >> $GITHUB_OUTPUT +# echo "exists=${{steps.meta.outputs.exists}}" >> $GITHUB_OUTPUT +# echo "checksum=${{steps.meta.outputs.checksum}}" >> $GITHUB_OUTPUT +# +# +# +# - name: Docker meta +# id: meta +# uses: docker/metadata-action@v5 +# - id: env +# run: | +# br=${{ steps.extract_branch.outputs.branch }} +# build_date=$(date +"%Y-%m-%d %H:%M") +# +# echo "version=${br##*/}" >> $GITHUB_OUTPUT +# echo "date=$build_date" >> $GITHUB_OUTPUT +# echo "image=${tag%:*}" >> $GITHUB_OUTPUT +# echo "name=${{vars.DOCKER_IMAGE}}:test-${{needs.setup.outputs.tag}}" >> $GITHUB_OUTPUT +# +# - 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 }} +# - 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 "=====================================" + check: + name: Build Testing Docker Image + needs: [setup] + runs-on: ubuntu-latest + steps: + - run: | + echo "" + echo "${{ toJSON(needs.setup.outputs) }}" + echo "${{ toJSON(needs.setup.env) }}" + build: # if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name - name: Build Test Docker Image + name: Build Testing Docker Image needs: [setup] runs-on: ubuntu-latest defaults: