Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(docker): Improve docker build and deployment #2185

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/actions/clean_up_package_registry/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright Helio Chissini de Castro, 2023
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0

name: 'Delete old non-release packages from Github package registry'
description: 'Delete older packages set by a minimal level input'
author: 'The ORT Project Authors'

inputs:
registry:
description: 'Github container registry'
default: 'ghcr.io'
token:
description: 'Github token'
required: true
keep:
description: 'Number of non-release packages to keep'
required: false
default: '3'
packages:
description: 'Name of the packages to be cleaned up'
required: true
dry-run:
description: 'Execute a dry run operation to check the execution is correct'
default: 'false'

runs:
using: 'composite'

steps:
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'pip'

- name: Execute the operation
id: check_image
shell: bash
env:
INPUT_REGISTRY: ${{ inputs.registry }}
INPUT_TOKEN: ${{ inputs.token }}
INPUT_KEEP: ${{ inputs.keep }}
INPUT_PACKAGES: ${{ inputs.packages }}
INPUT_DRY_RUN: ${{ inputs.dry-run}}
run: |
pip install -q -U pip requests rich
python ./.github/actions/clean_up_package_registry/clean_up_package_registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright Helio Chissini de Castro, 2023
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0


import os

import requests
from rich import print

""" Use current Github API to list packages
in registry and remove all but last 3 or custom
set number of packages.
Reference: https://docs.github.com/en/rest/packages/packages?apiVersion=2022-11-28#about-github-packages
"""

dry_run: bool = True if os.getenv("INPUT_DRY_RUN") == "true" else False
keep = int(os.getenv("INPUT_KEEP"))
org = os.getenv("GITHUB_REPOSITORY_OWNER")
packages = os.getenv("INPUT_PACKAGES").split("\n")
token = os.getenv("INPUT_TOKEN")

headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28",
}

# Assembly organization packages url string
pkg_url: str = f"https://api.github.com/orgs/{org}/packages"


def delete_packages():
for package in packages:
print(f":package: {package}")
url = f"{pkg_url}/container/{package.replace('/', '%2F')}/versions?per_page=100"
response = requests.get(url, headers=headers)

if response.status_code == 404:
print(f":cross_mark: Not found - {url}")
continue

# Sort all images on id.
images = sorted(response.json(), key=lambda x: x["id"], reverse=True)

# Slice and remove all
if len(images) > keep:
for image in images[keep + 1 :]:
url = f"{pkg_url}/container/{package.replace('/', '%2F')}/versions/{image['id']}"

# Never remove latest or non snapshot tagged images
if restrict_delete_tags(image["metadata"]["container"]["tags"]):
print(
f":package: Skip tagged {package} id {image['id']} tags {image['metadata']['container']['tags']}"
)
continue

if not dry_run:
response = requests.delete(url, headers=headers)
if response.status_code != 204:
print(
f":cross_mark: Failed to delete package {package} version id {image['id']}."
)
continue
print(
f":white_heavy_check_mark: Deleted package {package} version id {image['id']}."
)


def restrict_delete_tags(tags: list) -> bool:
if not tags:
return False
for tag in tags:
if tag == "latest":
return True
elif "nightly" in tag:
return True
return False


if __name__ == "__main__":
delete_packages()
33 changes: 33 additions & 0 deletions .github/workflows/clean_up_package_registry.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright Helio Chissini de Castro, 2023
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0

name: Clean up packages in Github package registry

on:
workflow_dispatch:
# Runs always Sunday Midnight
schedule:
- cron: "0 0 * * 0"

jobs:
clean_all:
name: Cleaning older packages
runs-on: ubuntu-22.04
steps:
- name: Checkout default branch
uses: actions/checkout@v4
- name: Clean up package registry
uses: ./.github/actions/clean_up_package_registry
with:
token: ${{ secrets.GITHUB_TOKEN }}
packages: |-
thrift
clucene
base
binaries
sw360
Loading