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] 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!")