-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from chipkent/initial
Initial implementation
- Loading branch information
Showing
3 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,26 @@ | ||
# action-cleanup_package | ||
GitHub Action to clean up unwanted GitHub packages | ||
# action-cleanup-package | ||
|
||
This is a GitHub Action to delete GitHub packages. It is very useful to clean up unneeded Docker | ||
images in the GitHub Container Registry (ghcr.io) after a PR is closed. | ||
|
||
``` | ||
# Delete Docker images after PR merge | ||
# | ||
name: 'Clean up Docker images from PR' | ||
on: | ||
pull_request: | ||
types: [closed] | ||
jobs: | ||
purge-image: | ||
name: Delete image from ghcr.io | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: chipkent/action-cleanup-package@v1.0.0 | ||
with: | ||
package-name: ${{ github.event.repository.name }} | ||
tag: pr-${{ github.event.pull_request.number }} | ||
github-token: ${{ secrets.CI_ACTION_TOKEN }} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
name: 'Delete ghcr.io package tag' | ||
description: 'Delete a tagged package from ghcr.io' | ||
inputs: | ||
package-name: | ||
description: 'Package name' | ||
required: true | ||
tag: | ||
description: 'Package tag' | ||
required: true | ||
github-token: | ||
description: 'GitHub Personal Access Token with package deletion permissions.' | ||
required: true | ||
runs: | ||
using: "composite" | ||
steps: | ||
- uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.8' | ||
- name: Install packages | ||
run: pip install requests | ||
- name: Delete image from ghcr.io | ||
run: python ${{ github.action_path }}/docker-cleanup.py ${{ inputs.package-name }} ${{ inputs.tag }} ${{ inputs.github-token }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Script to delete tagged docker images. | ||
# Usage: <package_name> <tag> <github_token> | ||
# | ||
|
||
|
||
import requests | ||
|
||
|
||
def get_package_versions(package_name, package_type): | ||
print("get_package_versions", package_name, package_type) | ||
# https://docs.github.com/en/rest/reference/packages#get-all-package-versions-for-a-package-owned-by-the-authenticated-user | ||
result = [] | ||
done = False | ||
page = 1 | ||
|
||
while not done: | ||
response = session.get(f"https://api.github.com/user/packages/{package_type}/{package_name}/versions", params={"per_page" : 100, "page": page}) | ||
response.raise_for_status() | ||
json = response.json() | ||
result.extend(json) | ||
done = len(json) != 100 | ||
page += 1 | ||
|
||
return result | ||
|
||
|
||
def delete_package_version(package_name, package_type, id): | ||
print("delete_package_version", package_name, package_type, id) | ||
|
||
# https://docs.github.com/en/rest/reference/packages#delete-a-package-version-for-the-authenticated-user | ||
response = session.delete(f"https://api.github.com/user/packages/{package_type}/{package_name}/versions/{id}") | ||
response.raise_for_status() | ||
|
||
|
||
def get_tagged_container(package_name, tag): | ||
print("get_tagged_container", package_name, tag) | ||
packages = get_package_versions(package_name, "container") | ||
return [version for version in packages if tag in version["metadata"]["container"]["tags"]] | ||
|
||
|
||
def delete_tagged_container(package_name, tag): | ||
print("delete_tagged_container", package_name, tag) | ||
tags = get_tagged_container(package_name, tag) | ||
|
||
if tags: | ||
for tag in tags: | ||
delete_package_version(package_name, "container", tag["id"]) | ||
else: | ||
raise Exception(f"No containers to delete: {package_name} {tag}") | ||
|
||
|
||
################################ | ||
|
||
import sys | ||
|
||
if len(sys.argv) != 4: | ||
raise SystemExit("Usage: docker-cleanup.py <package_name> <tag> <github_token>") | ||
|
||
package_name = sys.argv[1] | ||
tag = sys.argv[2] | ||
token = sys.argv[3] | ||
|
||
print("package_name = ", package_name) | ||
print("tag = ", tag) | ||
|
||
session = requests.Session() | ||
session.headers.update({"Authorization": f"token {token}", "Accept" : "application/vnd.github.v3+json"}) | ||
delete_tagged_container(package_name, tag) | ||
|