From 114b2fe928351838e5b96cb14e97aff6d4dd3dfe Mon Sep 17 00:00:00 2001 From: Thomas Cardin <49320132+ThomasCardin@users.noreply.github.com> Date: Tue, 9 Jan 2024 09:36:41 -0500 Subject: [PATCH 01/17] issue #63: build and push docker container to github registry --- ...ow-build-push-container-github-registry.md | 6 ++ ...w-build-push-container-github-registry.yml | 102 ++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 .github/workflows/workflow-build-push-container-github-registry.md create mode 100644 .github/workflows/workflow-build-push-container-github-registry.yml diff --git a/.github/workflows/workflow-build-push-container-github-registry.md b/.github/workflows/workflow-build-push-container-github-registry.md new file mode 100644 index 00000000..df57ac70 --- /dev/null +++ b/.github/workflows/workflow-build-push-container-github-registry.md @@ -0,0 +1,6 @@ +## Reusable workflow to build and push docker container to GitHub Container Registry (GCR) + + - **Purpose:** Build and push a docker container to GitHub Container Registry (GCR). + - **Usage:** Call this workflow and provide the `container-name`, `tag` and `registry` as inputs. The `registry` should always start with `ghcr.io/` + - **Required Secrets:** + - None \ No newline at end of file diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml new file mode 100644 index 00000000..ebfef015 --- /dev/null +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -0,0 +1,102 @@ +name: Reusable workflow to build and push docker container to github registry + +on: + workflow_call: + inputs: + container-name: + required: true + type: string + tag: + required: true + type: string + registry: + required: true + type: string + +jobs: + build-and-push-on-commit: + runs-on: ubuntu-latest + steps: + - name: Checkout the repo + uses: actions/checkout@v4 + + - name: Set up Docker buildx + id: buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to the github container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ inputs.registry }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Cache Docker layers + uses: actions/cache@v2 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-${{inputs.container-name}}-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-${{inputs.container-name}} + + - name: Build and push + id: docker_build + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: ${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new + + - name: Refresh Cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache + + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} + + build-and-push-on-pr-merge: + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true + steps: + - name: Check Out Repo + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to the github container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ inputs.registry }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Cache Docker layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ inputs.tag }} + restore-keys: ${{ runner.os }}-buildx + + - name: Build and push (latest) + id: docker_build_latest + uses: docker/build-push-action@v5 + with: + context: ./${{ github.event.repository.name }} + file: ./${{ github.event.repository.name }}/Dockerfile + push: true + tags: ${{ inputs.registry }}/${{ inputs.container-name }}:latest + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new + + - name: Refresh Cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache + + - name: Image digest (latest) + run: echo ${{ steps.docker_build_latest.outputs.digest }} \ No newline at end of file From 6db4373245774af5a64cab6a334126054674e45d Mon Sep 17 00:00:00 2001 From: Thomas Cardin <49320132+ThomasCardin@users.noreply.github.com> Date: Tue, 9 Jan 2024 09:53:16 -0500 Subject: [PATCH 02/17] issue #63: fixed context for build-and-push-on-pr-merge --- .../workflow-build-push-container-github-registry.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index ebfef015..e18925b5 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -86,8 +86,7 @@ jobs: id: docker_build_latest uses: docker/build-push-action@v5 with: - context: ./${{ github.event.repository.name }} - file: ./${{ github.event.repository.name }}/Dockerfile + context: . push: true tags: ${{ inputs.registry }}/${{ inputs.container-name }}:latest cache-from: type=local,src=/tmp/.buildx-cache From cc4467e153fb56cd19eb7796b3c5f6412eab25e1 Mon Sep 17 00:00:00 2001 From: Thomas Cardin <49320132+ThomasCardin@users.noreply.github.com> Date: Tue, 9 Jan 2024 13:35:35 -0500 Subject: [PATCH 03/17] issue #63: added EOF --- .../workflow-build-push-container-github-registry.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index e18925b5..eff30242 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -98,4 +98,5 @@ jobs: mv /tmp/.buildx-cache-new /tmp/.buildx-cache - name: Image digest (latest) - run: echo ${{ steps.docker_build_latest.outputs.digest }} \ No newline at end of file + run: echo ${{ steps.docker_build_latest.outputs.digest }} + \ No newline at end of file From a97197bb8e6c05734c6fc68d01c178ca2355ed52 Mon Sep 17 00:00:00 2001 From: Thomas Cardin <49320132+ThomasCardin@users.noreply.github.com> Date: Tue, 9 Jan 2024 13:47:18 -0500 Subject: [PATCH 04/17] issue #63: Removed build-and-push-on-commit, not necessary for now --- ...w-build-push-container-github-registry.yml | 51 +++---------------- 1 file changed, 6 insertions(+), 45 deletions(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index eff30242..3f44338f 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -14,49 +14,6 @@ on: type: string jobs: - build-and-push-on-commit: - runs-on: ubuntu-latest - steps: - - name: Checkout the repo - uses: actions/checkout@v4 - - - name: Set up Docker buildx - id: buildx - uses: docker/setup-buildx-action@v3 - - - name: Log in to the github container registry - uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 - with: - registry: ${{ inputs.registry }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Cache Docker layers - uses: actions/cache@v2 - with: - path: /tmp/.buildx-cache - key: ${{ runner.os }}-${{inputs.container-name}}-${{ github.sha }} - restore-keys: | - ${{ runner.os }}-${{inputs.container-name}} - - - name: Build and push - id: docker_build - uses: docker/build-push-action@v5 - with: - context: . - push: true - tags: ${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }} - cache-from: type=local,src=/tmp/.buildx-cache - cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new - - - name: Refresh Cache - run: | - rm -rf /tmp/.buildx-cache - mv /tmp/.buildx-cache-new /tmp/.buildx-cache - - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} - build-and-push-on-pr-merge: runs-on: ubuntu-latest if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true @@ -82,13 +39,17 @@ jobs: key: ${{ runner.os }}-buildx-${{ inputs.tag }} restore-keys: ${{ runner.os }}-buildx - - name: Build and push (latest) + # Tag :latest to always have the latest build in the dev environment + # Tag :${{ inputs.tag }} to have a specific version of the container (cherry pick if needed and used for production) + - name: Build and push 2 container id: docker_build_latest uses: docker/build-push-action@v5 with: context: . push: true - tags: ${{ inputs.registry }}/${{ inputs.container-name }}:latest + tags: | + ${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }} + ${{ inputs.registry }}/${{ inputs.container-name }}:latest cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new From fafbcff89284b21e378904b2ef9666175a967160 Mon Sep 17 00:00:00 2001 From: ThomasCardin Date: Wed, 10 Jan 2024 18:07:00 -0500 Subject: [PATCH 05/17] issue #63: fixe workflow --- ...ow-build-push-container-github-registry.md | 2 +- ...w-build-push-container-github-registry.yml | 48 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.md b/.github/workflows/workflow-build-push-container-github-registry.md index df57ac70..a32e522a 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.md +++ b/.github/workflows/workflow-build-push-container-github-registry.md @@ -1,6 +1,6 @@ ## Reusable workflow to build and push docker container to GitHub Container Registry (GCR) - **Purpose:** Build and push a docker container to GitHub Container Registry (GCR). - - **Usage:** Call this workflow and provide the `container-name`, `tag` and `registry` as inputs. The `registry` should always start with `ghcr.io/` + - **Usage:** Call this workflow and provide the container-name, tag, and registry as inputs. The registry should always start with ghcr.io/ (e.g., ghcr.io/ai-cfia). If you create, push, or merge a pull request, the workflow will be triggered and will start 2 jobs. The first job will build and push the new image with 2 tags. The first tag is the pull request number, and the second tag is the commit that triggered the action. The second job will remove the previous image based on the previous tag using [this script](/scripts/remove-old-image.py). - **Required Secrets:** - None \ No newline at end of file diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index 3f44338f..20ce6407 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -12,11 +12,15 @@ on: registry: required: true type: string + pull_request: + types: + - opened + - closed + - synchronize jobs: - build-and-push-on-pr-merge: + build-push-image: runs-on: ubuntu-latest - if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true steps: - name: Check Out Repo uses: actions/checkout@v4 @@ -25,7 +29,7 @@ jobs: id: buildx uses: docker/setup-buildx-action@v3 - - name: Log in to the github container registry + - name: Log in to the github container registry (GCR) uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 with: registry: ${{ inputs.registry }} @@ -36,20 +40,18 @@ jobs: uses: actions/cache@v3 with: path: /tmp/.buildx-cache - key: ${{ runner.os }}-buildx-${{ inputs.tag }} + key: ${{ runner.os }}-buildx-${{ github.sha }} restore-keys: ${{ runner.os }}-buildx - # Tag :latest to always have the latest build in the dev environment - # Tag :${{ inputs.tag }} to have a specific version of the container (cherry pick if needed and used for production) - - name: Build and push 2 container + - name: Build and push (latest) id: docker_build_latest uses: docker/build-push-action@v5 with: context: . push: true tags: | - ${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }} - ${{ inputs.registry }}/${{ inputs.container-name }}:latest + ${{ inputs.registry }}/${{ inputs.container-name }}:${{ github.event.number }} + ${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }} cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new @@ -60,4 +62,32 @@ jobs: - name: Image digest (latest) run: echo ${{ steps.docker_build_latest.outputs.digest }} + + remove-old-image: + runs-on: ubuntu-latest + needs: build-push-image + steps: + - name: Check Out Repo + uses: actions/checkout@v4 + + - name: Set up Docker + uses: docker/setup-buildx-action@v3 + + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: 3.8 + + - name: Install Python dependencies + run: pip install requests + + - name: Run Python Script + run: python scripts/remove-old-image.py + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REGISTRY: ${{ inputs.registry }} + CONTAINER_NAME: ${{ inputs.container-name }} + PR_TAG: ${{ github.event.number }} + USER: ${{ github.actor }} + CURRENT_COMMIT: ${{ inputs.tag }} \ No newline at end of file From 195ba6a148bd964d6c665433e9fde15093e022d8 Mon Sep 17 00:00:00 2001 From: ThomasCardin Date: Wed, 10 Jan 2024 18:07:25 -0500 Subject: [PATCH 06/17] issue #63: created script for the second job --- scripts/remove-old-image.py | 77 +++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 scripts/remove-old-image.py diff --git a/scripts/remove-old-image.py b/scripts/remove-old-image.py new file mode 100644 index 00000000..eecdd1b8 --- /dev/null +++ b/scripts/remove-old-image.py @@ -0,0 +1,77 @@ +""" + +This script is based on this documentation: +https://docs.github.com/en/rest/packages/packages?apiVersion=2022-11-28 + +""" + +import os +import requests +from requests.auth import HTTPBasicAuth + +# Delete the old container (based on the previous tag) +def delete_old_image(version_id, org, headers, auth): + url_delete_previous_version = f"https://api.github.com/orgs/{org}/packages/container/{container_name}/versions/{version_id}" + response = requests.delete(url_delete_previous_version, headers=headers, auth=auth) + if response.status_code == 204: + print(f'Previous container deleted!') + else: + print('error deleting the previous container:', response.status_code, response.text) + exit(1) + +# Find the previous tag for a specific container +def find_previous_container_tag(response, pr_tag): + version_id = None + for version in response: + tags = version['metadata']['container']['tags'] + if pr_tag not in tags and tags: + version_id = version['id'] + print(f"Previous tag found {tags[0]} with version_id {version_id}") + return tags[0], version_id + + print(f"Container name {container_name} not found or the only tag found was the current one. If that is the case, you can ignore this error.") + exit(0) + +# Get all GCR container information +def get_container_tags(org, container_name, auth, headers, container_path): + get_versions = f"https://api.github.com/orgs/{org}/packages/container/{container_name}/versions" + response = requests.get(get_versions, headers=headers, auth=auth) + try: + response.raise_for_status() + except requests.exceptions.HTTPError as e: + print(f"Error getting the previous tag for the container {container_path} : {e}") + exit(1) + + return response + +def print_console(message): + print("====================================") + print(message) + +if __name__ == "__main__": + registry = os.getenv("REGISTRY") + github_token = os.getenv("GITHUB_TOKEN") + container_name = os.getenv("CONTAINER_NAME") + pr_tag = os.getenv("PR_TAG") + user = os.getenv("USER") + current_commit = os.getenv("CURRENT_COMMIT") + + headers = { + "Accept": "application/vnd.github.v3+json", + } + auth = HTTPBasicAuth(user, github_token) + + container_path = f"{registry}/{container_name}:{pr_tag}" + org = registry.split("/")[1] + + print_console(f"Getting all tags for this container {container_path}...") + response = get_container_tags(org, container_name, auth, headers, container_path) + print("Done!") + + print_console(f"Looking for the previous tag...") + previous_tag, version_id = find_previous_container_tag(response.json(), pr_tag) + print("Done!") + + print_console(f"Deleting the previous container with tag ({previous_tag}) and version_id {version_id}...") + delete_old_image(version_id, org, headers, auth) + print("Done!") From 619ac6404c5450560153bf1b2d15af25ccd89b8a Mon Sep 17 00:00:00 2001 From: ThomasCardin Date: Wed, 10 Jan 2024 18:30:19 -0500 Subject: [PATCH 07/17] issue #63: check if a dockerfile is present inside the repo --- ...low-build-push-container-github-registry.yml | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index 20ce6407..d184b37e 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -1,4 +1,4 @@ -name: Reusable workflow to build and push docker container to github registry +name: Reusable workflow to build and push docker container to github container registry on: workflow_call: @@ -25,6 +25,21 @@ jobs: - name: Check Out Repo uses: actions/checkout@v4 + - name: Check if a Dockerfile is present at the root of the repo + id: check-file + run: | + if [ -f Dockerfile ]; then + echo "::set-output name=exists::true" + else + echo "::set-output name=exists::false" + fi + + - name: Cancel the workflow if no Dockerfile is present + if: steps.check-file.outputs.exists == 'false' + uses: styfle/cancel-workflow-action@0.9.1 + with: + access_token: ${{ github.token }} + - name: Set up Docker Buildx id: buildx uses: docker/setup-buildx-action@v3 From f0bef9d7b9a41a85922a6fe7411ede51c6819f70 Mon Sep 17 00:00:00 2001 From: Thomas Cardin <49320132+ThomasCardin@users.noreply.github.com> Date: Thu, 11 Jan 2024 08:33:14 -0500 Subject: [PATCH 08/17] issue #63: cancel the action if no dockerfile is found --- .../workflow-build-push-container-github-registry.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index d184b37e..76bf3de5 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -36,9 +36,13 @@ jobs: - name: Cancel the workflow if no Dockerfile is present if: steps.check-file.outputs.exists == 'false' - uses: styfle/cancel-workflow-action@0.9.1 - with: - access_token: ${{ github.token }} + uses: andymckay/cancel-action@0.2 + + # needed since the run is not cancelled immediately + - name: Wait for run cancellation + if: steps.check-file.outputs.exists == 'false' + shell: bash + run: while true; do echo "Waiting for job to be cancelled"; sleep 5; done - name: Set up Docker Buildx id: buildx From bdd3513d3ae1dd151c7c3ae343006d452acb2030 Mon Sep 17 00:00:00 2001 From: Thomas Cardin <49320132+ThomasCardin@users.noreply.github.com> Date: Thu, 11 Jan 2024 10:18:30 -0500 Subject: [PATCH 09/17] issue #63: added logging message --- .../workflow-build-push-container-github-registry.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index 76bf3de5..72c12f37 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -42,7 +42,9 @@ jobs: - name: Wait for run cancellation if: steps.check-file.outputs.exists == 'false' shell: bash - run: while true; do echo "Waiting for job to be cancelled"; sleep 5; done + run: | + echo "No Dockerfile found, cancelling the workflow." + while true; do echo "Waiting for job to be cancelled"; sleep 5; done - name: Set up Docker Buildx id: buildx From 2267d340316dbcf6278ad09df36548a99e7555e3 Mon Sep 17 00:00:00 2001 From: Thomas Cardin <49320132+ThomasCardin@users.noreply.github.com> Date: Thu, 11 Jan 2024 11:21:42 -0500 Subject: [PATCH 10/17] issue #63: manage multiple branch --- ...workflow-build-push-container-github-registry.yml | 12 +++++++++--- scripts/remove-old-image.py | 11 ++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index 72c12f37..9523355c 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -64,8 +64,13 @@ jobs: key: ${{ runner.os }}-buildx-${{ github.sha }} restore-keys: ${{ runner.os }}-buildx - - name: Build and push (latest) - id: docker_build_latest + - name: Extract branch name + shell: bash + run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT + id: extract_branch_name + + - name: Build and push (3 tags | commit number, pr number, branch name) + id: docker_build_and_tag uses: docker/build-push-action@v5 with: context: . @@ -73,6 +78,7 @@ jobs: tags: | ${{ inputs.registry }}/${{ inputs.container-name }}:${{ github.event.number }} ${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }} + ${{ inputs.registry }}/${{ inputs.container-name }}:${{ steps.extract_branch_name.outputs.branch }} cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new @@ -82,7 +88,7 @@ jobs: mv /tmp/.buildx-cache-new /tmp/.buildx-cache - name: Image digest (latest) - run: echo ${{ steps.docker_build_latest.outputs.digest }} + run: echo ${{ steps.docker_build_and_tag.outputs.digest }} remove-old-image: runs-on: ubuntu-latest diff --git a/scripts/remove-old-image.py b/scripts/remove-old-image.py index eecdd1b8..db74c125 100644 --- a/scripts/remove-old-image.py +++ b/scripts/remove-old-image.py @@ -19,19 +19,24 @@ def delete_old_image(version_id, org, headers, auth): print('error deleting the previous container:', response.status_code, response.text) exit(1) -# Find the previous tag for a specific container +""" +Find the previous tag for a specific container. +Delete the previous tag if it exists and it is not the current one. This wont delete the current tag or older PR tags. +Check if tags is not empty and check if the len is == 1. If that is the case, it means that the only tag is the previous one. +""" def find_previous_container_tag(response, pr_tag): version_id = None for version in response: + print(f"Found tags {version['metadata']['container']['tags']})") tags = version['metadata']['container']['tags'] - if pr_tag not in tags and tags: + if pr_tag not in tags and len(tags) == 1 and tags: version_id = version['id'] print(f"Previous tag found {tags[0]} with version_id {version_id}") return tags[0], version_id print(f"Container name {container_name} not found or the only tag found was the current one. If that is the case, you can ignore this error.") exit(0) - + # Get all GCR container information def get_container_tags(org, container_name, auth, headers, container_path): get_versions = f"https://api.github.com/orgs/{org}/packages/container/{container_name}/versions" From 81e894044138537d9a8ac15db4b36ae37af40de3 Mon Sep 17 00:00:00 2001 From: Thomas Cardin <49320132+ThomasCardin@users.noreply.github.com> Date: Thu, 11 Jan 2024 11:33:31 -0500 Subject: [PATCH 11/17] issue #63: updated the doc and download the script --- .../workflow-build-push-container-github-registry.md | 2 +- .../workflow-build-push-container-github-registry.yml | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.md b/.github/workflows/workflow-build-push-container-github-registry.md index a32e522a..ac5a3882 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.md +++ b/.github/workflows/workflow-build-push-container-github-registry.md @@ -1,6 +1,6 @@ ## Reusable workflow to build and push docker container to GitHub Container Registry (GCR) - **Purpose:** Build and push a docker container to GitHub Container Registry (GCR). - - **Usage:** Call this workflow and provide the container-name, tag, and registry as inputs. The registry should always start with ghcr.io/ (e.g., ghcr.io/ai-cfia). If you create, push, or merge a pull request, the workflow will be triggered and will start 2 jobs. The first job will build and push the new image with 2 tags. The first tag is the pull request number, and the second tag is the commit that triggered the action. The second job will remove the previous image based on the previous tag using [this script](/scripts/remove-old-image.py). + - **Usage:** Call this workflow and provide the container-name, tag, and registry as inputs. The registry should always start with ghcr.io/ (e.g., ghcr.io/ai-cfia). If you create, push, or merge a pull request, the workflow will be triggered and will start 2 jobs. The first job will build and push the new image with 2 tags. The first tag is the pull request number, and the second tag is the commit that triggered the action. The second job will remove the previous image based on the previous tag using [this script](/scripts/remove-old-image.py). If no Dockerfile is found at the root of the repository, the workflow will be cancelled. - **Required Secrets:** - None \ No newline at end of file diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index 9523355c..a289f9c8 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -108,8 +108,12 @@ jobs: - name: Install Python dependencies run: pip install requests + - name: Download the python script from https://github.com/ai-cfia/github-workflows + run: | + curl -LO https://raw.githubusercontent.com/ai-cfia/github-workflows/main/scripts/remove-old-image.py + - name: Run Python Script - run: python scripts/remove-old-image.py + run: python remove-old-image.py env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} REGISTRY: ${{ inputs.registry }} From 583fb4c58f4d324b661e0b649cad875e093e8cbb Mon Sep 17 00:00:00 2001 From: Thomas Cardin <49320132+ThomasCardin@users.noreply.github.com> Date: Thu, 11 Jan 2024 12:13:34 -0500 Subject: [PATCH 12/17] issue #63: fixed issues and added the doc to the readme --- ...kflow-build-push-container-github-registry.yml | 15 +++++---------- README.md | 2 ++ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index a289f9c8..d4816247 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -34,17 +34,13 @@ jobs: echo "::set-output name=exists::false" fi - - name: Cancel the workflow if no Dockerfile is present + - name: Early exit if no Dockerfile is present if: steps.check-file.outputs.exists == 'false' - uses: andymckay/cancel-action@0.2 - - # needed since the run is not cancelled immediately - - name: Wait for run cancellation - if: steps.check-file.outputs.exists == 'false' - shell: bash run: | - echo "No Dockerfile found, cancelling the workflow." - while true; do echo "Waiting for job to be cancelled"; sleep 5; done + gh run cancel ${{ github.run_id }} + gh run watch ${{ github.run_id }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Set up Docker Buildx id: buildx @@ -121,4 +117,3 @@ jobs: PR_TAG: ${{ github.event.number }} USER: ${{ github.actor }} CURRENT_COMMIT: ${{ inputs.tag }} - \ No newline at end of file diff --git a/README.md b/README.md index b2477f24..107912dc 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ Here are the available workflows and their associated documentation: Pages](.github/workflows/workflow-gh-pages-deployment.md) - [Reusable Vercel Deployment](.github/workflows/workflow-vercel-deployment.md) +- [Reusable Build + and Push to GCR](.github/workflows/workflow-build-push-container-github-registry.md) ## Usage From 9c1cd650366824c84038dfafe049e95c254ac08d Mon Sep 17 00:00:00 2001 From: Thomas Cardin <49320132+ThomasCardin@users.noreply.github.com> Date: Thu, 11 Jan 2024 14:01:09 -0500 Subject: [PATCH 13/17] issue #63: removed on push_branches for this repository --- .../workflow-build-push-container-github-registry.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index d4816247..357b729f 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -12,11 +12,6 @@ on: registry: required: true type: string - pull_request: - types: - - opened - - closed - - synchronize jobs: build-push-image: From a4f437c49f9d2af2e82ef1ec2c3994b95ab13008 Mon Sep 17 00:00:00 2001 From: ThomasCardin Date: Fri, 12 Jan 2024 11:16:23 -0500 Subject: [PATCH 14/17] issue #63: changed the url for the good one (testing) --- .../workflows/workflow-build-push-container-github-registry.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index 357b729f..c61c7d6e 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -101,7 +101,7 @@ jobs: - name: Download the python script from https://github.com/ai-cfia/github-workflows run: | - curl -LO https://raw.githubusercontent.com/ai-cfia/github-workflows/main/scripts/remove-old-image.py + curl -LO https://raw.githubusercontent.com/ai-cfia/github-workflows/63-as-a-devops-i-would-like-to-create-a-workflow-to-push-images-to-this-organisation-docker-registry/scripts/remove-old-image.py - name: Run Python Script run: python remove-old-image.py From 25f5e3401d39fbf30e55cf5b21375598511dd1ed Mon Sep 17 00:00:00 2001 From: ThomasCardin Date: Mon, 29 Jan 2024 13:25:32 -0500 Subject: [PATCH 15/17] issue #63: support merged branch and added .vscode --- ...ow-build-push-container-github-registry.md | 18 +++++++--- ...w-build-push-container-github-registry.yml | 34 ++++++++++++++++--- .vscode/extensions.json | 9 +++++ .vscode/settings.json | 6 ++++ scripts/remove-old-image.py | 28 ++++++++------- 5 files changed, 74 insertions(+), 21 deletions(-) create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json diff --git a/.github/workflows/workflow-build-push-container-github-registry.md b/.github/workflows/workflow-build-push-container-github-registry.md index ac5a3882..faab493b 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.md +++ b/.github/workflows/workflow-build-push-container-github-registry.md @@ -1,6 +1,14 @@ -## Reusable workflow to build and push docker container to GitHub Container Registry (GCR) +# Reusable workflow to build and push docker container to GitHub Container Registry (GCR) - - **Purpose:** Build and push a docker container to GitHub Container Registry (GCR). - - **Usage:** Call this workflow and provide the container-name, tag, and registry as inputs. The registry should always start with ghcr.io/ (e.g., ghcr.io/ai-cfia). If you create, push, or merge a pull request, the workflow will be triggered and will start 2 jobs. The first job will build and push the new image with 2 tags. The first tag is the pull request number, and the second tag is the commit that triggered the action. The second job will remove the previous image based on the previous tag using [this script](/scripts/remove-old-image.py). If no Dockerfile is found at the root of the repository, the workflow will be cancelled. - - **Required Secrets:** - - None \ No newline at end of file +- **Purpose:** Build and push a docker container to GitHub Container Registry (GCR). +- **Usage:** Call this workflow and provide the container-name, tag, and +registry as inputs. The registry should always start with +ghcr.io/ (e.g., ghcr.io/ai-cfia). If you create, push, or +merge a pull request, the workflow will be triggered and will start 2 jobs. +The first job will build and push the new image with 2 tags. The first tag is +the pull request number, and the second tag is the commit that triggered the +action. The second job will remove the previous image based on the previous +tag using [this script](/scripts/remove-old-image.py). If no Dockerfile is found +at the root of the repository, the workflow will be cancelled. +- **Required Secrets:** + - None diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index c61c7d6e..84d376bf 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -60,8 +60,9 @@ jobs: run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT id: extract_branch_name - - name: Build and push (3 tags | commit number, pr number, branch name) + - name: Build and push (3 tags | commit number, pr number and branch name) (unmerged) id: docker_build_and_tag + if: github.event.pull_request.merged == false uses: docker/build-push-action@v5 with: context: . @@ -73,6 +74,19 @@ jobs: cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new + - name: Build and push (2 tags | commit number and branch name) (merged) + id: docker_build_and_tag + if: github.event.pull_request.merged == true + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: | + ${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }} + ${{ inputs.registry }}/${{ inputs.container-name }}:${{ github.event.pull_request.base.ref }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new + - name: Refresh Cache run: | rm -rf /tmp/.buildx-cache @@ -99,16 +113,28 @@ jobs: - name: Install Python dependencies run: pip install requests - - name: Download the python script from https://github.com/ai-cfia/github-workflows + - name: Download the python script from https://github.com/ai-cfia/github-workflows to delete the previous image run: | curl -LO https://raw.githubusercontent.com/ai-cfia/github-workflows/63-as-a-devops-i-would-like-to-create-a-workflow-to-push-images-to-this-organisation-docker-registry/scripts/remove-old-image.py - - name: Run Python Script + - name: Delete the previous image (unmerged pull request) + if: github.event.pull_request.merged == false + run: python remove-old-image.py + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REGISTRY: ${{ inputs.registry }} + CONTAINER_NAME: ${{ inputs.container-name }} + UNIQUE_TAG: ${{ github.event.number }} + USER: ${{ github.actor }} + CURRENT_COMMIT: ${{ inputs.tag }} + + - name: Delete the previous image (merged pull request) + if: github.event.pull_request.merged == true run: python remove-old-image.py env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} REGISTRY: ${{ inputs.registry }} CONTAINER_NAME: ${{ inputs.container-name }} - PR_TAG: ${{ github.event.number }} + UNIQUE_TAG: ${{ github.event.pull_request.base.ref }} USER: ${{ github.actor }} CURRENT_COMMIT: ${{ inputs.tag }} diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 00000000..d156db8b --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,9 @@ +{ + "recommendations": [ + "stkb.rewrap", + "DavidAnson.vscode-markdownlint" + ], + "unwantedRecommendations": [ + + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..bbf84028 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "editor.rulers": [80], + "files.trimTrailingWhitespace": true, + "files.trimFinalNewlines": true, + "files.insertFinalNewline": true +} diff --git a/scripts/remove-old-image.py b/scripts/remove-old-image.py index db74c125..27f25010 100644 --- a/scripts/remove-old-image.py +++ b/scripts/remove-old-image.py @@ -1,6 +1,6 @@ """ -This script is based on this documentation: +This script is based on this documentation: https://docs.github.com/en/rest/packages/packages?apiVersion=2022-11-28 """ @@ -9,7 +9,9 @@ import requests from requests.auth import HTTPBasicAuth -# Delete the old container (based on the previous tag) +""" +Delete the old container (based on the previous tag) +""" def delete_old_image(version_id, org, headers, auth): url_delete_previous_version = f"https://api.github.com/orgs/{org}/packages/container/{container_name}/versions/{version_id}" response = requests.delete(url_delete_previous_version, headers=headers, auth=auth) @@ -18,26 +20,28 @@ def delete_old_image(version_id, org, headers, auth): else: print('error deleting the previous container:', response.status_code, response.text) exit(1) - + """ Find the previous tag for a specific container. Delete the previous tag if it exists and it is not the current one. This wont delete the current tag or older PR tags. Check if tags is not empty and check if the len is == 1. If that is the case, it means that the only tag is the previous one. """ -def find_previous_container_tag(response, pr_tag): +def find_previous_container_tag(response, unique_tag): version_id = None for version in response: print(f"Found tags {version['metadata']['container']['tags']})") tags = version['metadata']['container']['tags'] - if pr_tag not in tags and len(tags) == 1 and tags: + if unique_tag not in tags and len(tags) == 1 and tags: version_id = version['id'] print(f"Previous tag found {tags[0]} with version_id {version_id}") return tags[0], version_id print(f"Container name {container_name} not found or the only tag found was the current one. If that is the case, you can ignore this error.") exit(0) - -# Get all GCR container information + +""" +Get all GCR containers information +""" def get_container_tags(org, container_name, auth, headers, container_path): get_versions = f"https://api.github.com/orgs/{org}/packages/container/{container_name}/versions" response = requests.get(get_versions, headers=headers, auth=auth) @@ -46,18 +50,18 @@ def get_container_tags(org, container_name, auth, headers, container_path): except requests.exceptions.HTTPError as e: print(f"Error getting the previous tag for the container {container_path} : {e}") exit(1) - + return response def print_console(message): print("====================================") print(message) - + if __name__ == "__main__": registry = os.getenv("REGISTRY") github_token = os.getenv("GITHUB_TOKEN") container_name = os.getenv("CONTAINER_NAME") - pr_tag = os.getenv("PR_TAG") + unique_tag = os.getenv("UNIQUE_TAG") user = os.getenv("USER") current_commit = os.getenv("CURRENT_COMMIT") @@ -66,7 +70,7 @@ def print_console(message): } auth = HTTPBasicAuth(user, github_token) - container_path = f"{registry}/{container_name}:{pr_tag}" + container_path = f"{registry}/{container_name}:{unique_tag}" org = registry.split("/")[1] print_console(f"Getting all tags for this container {container_path}...") @@ -74,7 +78,7 @@ def print_console(message): print("Done!") print_console(f"Looking for the previous tag...") - previous_tag, version_id = find_previous_container_tag(response.json(), pr_tag) + previous_tag, version_id = find_previous_container_tag(response.json(), unique_tag) print("Done!") print_console(f"Deleting the previous container with tag ({previous_tag}) and version_id {version_id}...") From 38e339419b7d37b56a23d4a9f8ec604dc6ae0b1d Mon Sep 17 00:00:00 2001 From: ThomasCardin Date: Mon, 29 Jan 2024 13:40:00 -0500 Subject: [PATCH 16/17] issue #63: fix duplicated step name --- .../workflow-build-push-container-github-registry.yml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index 84d376bf..f4e94202 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -61,7 +61,7 @@ jobs: id: extract_branch_name - name: Build and push (3 tags | commit number, pr number and branch name) (unmerged) - id: docker_build_and_tag + id: docker_build_and_tag_unmerged if: github.event.pull_request.merged == false uses: docker/build-push-action@v5 with: @@ -75,7 +75,7 @@ jobs: cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new - name: Build and push (2 tags | commit number and branch name) (merged) - id: docker_build_and_tag + id: docker_build_and_tag_merged if: github.event.pull_request.merged == true uses: docker/build-push-action@v5 with: @@ -93,7 +93,12 @@ jobs: mv /tmp/.buildx-cache-new /tmp/.buildx-cache - name: Image digest (latest) - run: echo ${{ steps.docker_build_and_tag.outputs.digest }} + if: github.event.pull_request.merged == false + run: echo ${{ steps.docker_build_and_tag_unmerged.outputs.digest }} + + - name: Image digest (latest) + if: github.event.pull_request.merged == true + run: echo ${{ steps.docker_build_and_tag_merged.outputs.digest }} remove-old-image: runs-on: ubuntu-latest From f5c6ce48e70ab64b57996d88d65f926b00cb61cc Mon Sep 17 00:00:00 2001 From: Thomas Cardin <49320132+ThomasCardin@users.noreply.github.com> Date: Tue, 30 Jan 2024 10:17:38 -0500 Subject: [PATCH 17/17] installing the script with python -m pip install instead of downloading the whole repo --- ...ow-build-push-container-github-registry.md | 2 +- ...w-build-push-container-github-registry.yml | 23 +++-- scripts/remove-old-image.py | 86 ------------------- 3 files changed, 12 insertions(+), 99 deletions(-) delete mode 100644 scripts/remove-old-image.py diff --git a/.github/workflows/workflow-build-push-container-github-registry.md b/.github/workflows/workflow-build-push-container-github-registry.md index faab493b..ca4f6aa7 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.md +++ b/.github/workflows/workflow-build-push-container-github-registry.md @@ -8,7 +8,7 @@ merge a pull request, the workflow will be triggered and will start 2 jobs. The first job will build and push the new image with 2 tags. The first tag is the pull request number, and the second tag is the commit that triggered the action. The second job will remove the previous image based on the previous -tag using [this script](/scripts/remove-old-image.py). If no Dockerfile is found +tag. If no Dockerfile is found at the root of the repository, the workflow will be cancelled. - **Required Secrets:** - None diff --git a/.github/workflows/workflow-build-push-container-github-registry.yml b/.github/workflows/workflow-build-push-container-github-registry.yml index f4e94202..2d9f0b11 100644 --- a/.github/workflows/workflow-build-push-container-github-registry.yml +++ b/.github/workflows/workflow-build-push-container-github-registry.yml @@ -104,27 +104,26 @@ jobs: runs-on: ubuntu-latest needs: build-push-image steps: - - name: Check Out Repo - uses: actions/checkout@v4 - - - name: Set up Docker - uses: docker/setup-buildx-action@v3 - - name: Set up Python uses: actions/setup-python@v3 with: python-version: 3.8 - - name: Install Python dependencies - run: pip install requests + - name: Install the remove-previous-image from github.com/ai-cfia/devops inside the user-site + run: python -m pip install --user git+https://$USER:$USER_TOKEN@github.com/ai-cfia/devops.git@26-as-a-devops-i-want-to-create-unit-tests-for-the-remove-previous-imagepy-script + env: + USER: ${{ secrets.USER }} + USER_TOKEN: ${{ secrets.USER_TOKEN }} - - name: Download the python script from https://github.com/ai-cfia/github-workflows to delete the previous image + - name: Access user site-packages run: | - curl -LO https://raw.githubusercontent.com/ai-cfia/github-workflows/63-as-a-devops-i-would-like-to-create-a-workflow-to-push-images-to-this-organisation-docker-registry/scripts/remove-old-image.py + USER_SITE=$(python -m site --user-site) + echo "Path to site-packages is $USER_SITE" + echo "USER_SITE=$USER_SITE" >> $GITHUB_ENV - name: Delete the previous image (unmerged pull request) if: github.event.pull_request.merged == false - run: python remove-old-image.py + run: python $USER_SITE/remove-previous-image/remove_previous_image.py env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} REGISTRY: ${{ inputs.registry }} @@ -135,7 +134,7 @@ jobs: - name: Delete the previous image (merged pull request) if: github.event.pull_request.merged == true - run: python remove-old-image.py + run: python $USER_SITE/remove-previous-image/remove_previous_image.py env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} REGISTRY: ${{ inputs.registry }} diff --git a/scripts/remove-old-image.py b/scripts/remove-old-image.py deleted file mode 100644 index 27f25010..00000000 --- a/scripts/remove-old-image.py +++ /dev/null @@ -1,86 +0,0 @@ -""" - -This script is based on this documentation: -https://docs.github.com/en/rest/packages/packages?apiVersion=2022-11-28 - -""" - -import os -import requests -from requests.auth import HTTPBasicAuth - -""" -Delete the old container (based on the previous tag) -""" -def delete_old_image(version_id, org, headers, auth): - url_delete_previous_version = f"https://api.github.com/orgs/{org}/packages/container/{container_name}/versions/{version_id}" - response = requests.delete(url_delete_previous_version, headers=headers, auth=auth) - if response.status_code == 204: - print(f'Previous container deleted!') - else: - print('error deleting the previous container:', response.status_code, response.text) - exit(1) - -""" -Find the previous tag for a specific container. -Delete the previous tag if it exists and it is not the current one. This wont delete the current tag or older PR tags. -Check if tags is not empty and check if the len is == 1. If that is the case, it means that the only tag is the previous one. -""" -def find_previous_container_tag(response, unique_tag): - version_id = None - for version in response: - print(f"Found tags {version['metadata']['container']['tags']})") - tags = version['metadata']['container']['tags'] - if unique_tag not in tags and len(tags) == 1 and tags: - version_id = version['id'] - print(f"Previous tag found {tags[0]} with version_id {version_id}") - return tags[0], version_id - - print(f"Container name {container_name} not found or the only tag found was the current one. If that is the case, you can ignore this error.") - exit(0) - -""" -Get all GCR containers information -""" -def get_container_tags(org, container_name, auth, headers, container_path): - get_versions = f"https://api.github.com/orgs/{org}/packages/container/{container_name}/versions" - response = requests.get(get_versions, headers=headers, auth=auth) - try: - response.raise_for_status() - except requests.exceptions.HTTPError as e: - print(f"Error getting the previous tag for the container {container_path} : {e}") - exit(1) - - return response - -def print_console(message): - print("====================================") - print(message) - -if __name__ == "__main__": - registry = os.getenv("REGISTRY") - github_token = os.getenv("GITHUB_TOKEN") - container_name = os.getenv("CONTAINER_NAME") - unique_tag = os.getenv("UNIQUE_TAG") - user = os.getenv("USER") - current_commit = os.getenv("CURRENT_COMMIT") - - headers = { - "Accept": "application/vnd.github.v3+json", - } - auth = HTTPBasicAuth(user, github_token) - - container_path = f"{registry}/{container_name}:{unique_tag}" - org = registry.split("/")[1] - - print_console(f"Getting all tags for this container {container_path}...") - response = get_container_tags(org, container_name, auth, headers, container_path) - print("Done!") - - print_console(f"Looking for the previous tag...") - previous_tag, version_id = find_previous_container_tag(response.json(), unique_tag) - print("Done!") - - print_console(f"Deleting the previous container with tag ({previous_tag}) and version_id {version_id}...") - delete_old_image(version_id, org, headers, auth) - print("Done!")