Skip to content

Commit

Permalink
Pr35 (#36)
Browse files Browse the repository at this point in the history
* Add output with the number of deleted package versions

Use this output also to improve tests

* patch for github bug

* 🎨 Format Python code with yapf (#37)

Co-authored-by: Chizkiyahu <[email protected]>

---------

Co-authored-by: maleo <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Chizkiyahu <[email protected]>
  • Loading branch information
4 people authored Nov 12, 2023
1 parent bbbab21 commit f975b5e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 18 deletions.
50 changes: 48 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,23 @@ permissions:
contents: read
packages: write
jobs:
reset:
name: reset to a clean state
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./
with:
token: ${{ secrets.PAT_TOKEN }}
repository_owner: ${{ github.repository_owner }}
repository: ${{ github.repository }}
untagged_only: false
owner_type: user

add_temp_pkgs1:
name: Add temporary packages for testing
runs-on: ubuntu-latest
needs: reset
strategy:
matrix:
i: [1, 2]
Expand Down Expand Up @@ -92,13 +106,19 @@ jobs:
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: ./
id: deleted-action
with:
token: ${{ secrets.PAT_TOKEN }}
repository_owner: ${{ github.repository_owner }}
repository: ${{ github.repository }}
untagged_only: true
owner_type: user
except_untagged_multiplatform: true
- shell: bash
run: |
if [[ "${{ steps.deleted-action.outputs.num_deleted }}" != 18 ]]; then
exit 1
fi
clean_untagged_pkgs2:
name: clean untagged packages
Expand All @@ -107,13 +127,19 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: ./
id: deleted-action
with:
token: ${{ secrets.PAT_TOKEN }}
repository_owner: ${{ github.repository_owner }}
repository: ${{ github.repository }}
untagged_only: true
owner_type: user
except_untagged_multiplatform: false
- shell: bash
run: |
if [[ "${{ steps.deleted-action.outputs.num_deleted }}" != 24 ]]; then
exit 1
fi
delete_package:
name: delete package
Expand All @@ -122,39 +148,59 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: ./
id: deleted-action
with:
token: ${{ secrets.GITHUB_TOKEN }}
repository_owner: ${{ github.repository_owner }}
repository: ${{ github.repository }}
package_name: p1
untagged_only: false
owner_type: user
- shell: bash
run: |
if [[ "${{ steps.deleted-action.outputs.num_deleted }}" != 1 ]]; then
exit 1
fi
delete_multiple_packages:
name: delete package
name: delete multiple package
runs-on: ubuntu-latest
needs: clean_untagged_pkgs2
steps:
- uses: actions/checkout@v4
- uses: ./
id: deleted-action
with:
token: ${{ secrets.GITHUB_TOKEN }}
repository_owner: ${{ github.repository_owner }}
repository: ${{ github.repository }}
package_name: p2, p3
untagged_only: false
owner_type: user
- shell: bash
run: |
if [[ "${{ steps.deleted-action.outputs.num_deleted }}" != 2 ]]; then
exit 1
fi
clean_repo:
name: delete all package in repo
runs-on: ubuntu-latest
needs: delete_package
needs:
- delete_package
- delete_multiple_packages
steps:
- uses: actions/checkout@v4
- uses: ./
id: deleted-action
with:
token: ${{ secrets.PAT_TOKEN }}
repository_owner: ${{ github.repository_owner }}
repository: ${{ github.repository }}
untagged_only: false
owner_type: user
- shell: bash
run: |
if [[ "${{ steps.deleted-action.outputs.num_deleted }}" != 0 ]]; then
exit 1
fi
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![test](https://github.com/Chizkiyahu/delete-untagged-ghcr-action/actions/workflows/test.yml/badge.svg)](https://github.com/Chizkiyahu/delete-untagged-ghcr-action/actions/workflows/test.yml)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/94534b5b1d7c4c938149bde7dc6d18e2)](https://www.codacy.com/gh/Chizkiyahu/delete-untagged-ghcr-action/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=Chizkiyahu/delete-untagged-ghcr-action&amp;utm_campaign=Badge_Grade)

Action for delete containers from Github container registry
Action for delete containers from Github container registry

delete all / untagged ghcr containers in a repository
## Usage
Expand Down Expand Up @@ -35,7 +35,7 @@ delete all / untagged ghcr containers in a repository
# Default: true
# choices: true, false
untagged_only: true
# Except untagged multiplatform packages from deletion
# Except untagged multiplatform packages from deletion
# only for untagged_only=true
# needs docker installed
except_untagged_multiplatform: false
Expand Down Expand Up @@ -112,7 +112,7 @@ delete all / untagged ghcr containers in a repository
```


## Delete all containers from repository
## Delete all containers from repository
```yaml
- name: Delete all containers from repository
uses: Chizkiyahu/delete-untagged-ghcr-action@v3
Expand Down
26 changes: 13 additions & 13 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,52 @@ description: 'Delete containers on github container registry without tags'
branding:
icon: 'trash-2'
color: 'blue'
inputs:
inputs:
token:
description: >
Personal access token (PAT) used to fetch the repository. The PAT is configured
with the local git config, which enables your scripts to run authenticated git
commands. The post-job step removes the PAT.
needs delete:packages permissions
required: true

repository_owner:
description: 'The repository owner name'
default: ${{ github.repository_owner }}
required: true

repository:
description: 'Delete only from repository name'
required: false

package_name:
description: 'Delete only from comma separated package names'
required: false

untagged_only:
description: 'Delete only package versions without tag'
default: true

except_untagged_multiplatform:
description: 'Except untagged multiplatform packages from deletion (only for --untagged_only)'
default: false

owner_type:
description : "Owner type (org or user)"
required: true


outputs:
num_deleted:
description: 'Number of package versions that were deleted during the run.'
value: ${{ steps.delete-untagged-ghcr-action.outputs.num_deleted }}

runs:
using: "composite"
steps:
- uses: actions/setup-python@v4
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- run: pip install -r ${{ github.action_path }}/requirements.txt
- name: Install Python dependencies
shell: bash
- run: |
run: pip install -r ${{ github.action_path }}/requirements.txt
- name: Run action
id: delete-untagged-ghcr-action
shell: bash
run: |
args=( "--token" "${{ inputs.token }}" )
args+=( "--repository_owner" "${{ inputs.repository_owner }}" )
if [[ -n "${{ inputs.repository }}" ]]; then
Expand All @@ -62,4 +63,3 @@ runs:
args+=( "--owner_type" "${{ inputs.owner_type }}" )
echo "args: ${args[@]}"
python ${{ github.action_path }}/clean_ghcr.py "${args[@]}"
shell: bash
8 changes: 8 additions & 0 deletions clean_ghcr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import subprocess
import urllib.parse

Expand Down Expand Up @@ -72,6 +73,9 @@ def get_list_packages(owner, repo_name, owner_type, package_names):
pkgs = get_req(
f"/{owner_type}s/{owner}/packages?package_type=container")

# this is a strange bug in github api, it returns deleted packages
# I open a ticket for that
pkgs = [pkg for pkg in pkgs if not pkg["name"].startswith('deleted_')]
if repo_name:
pkgs = [
pkg for pkg in pkgs if pkg.get("repository")
Expand Down Expand Up @@ -160,7 +164,11 @@ def delete_pkgs(owner, repo_name, owner_type, package_names, untagged_only,
status = [del_req(pkg["url"]).ok for pkg in packages]
len_ok = len([ok for ok in status if ok])
len_fail = len(status) - len_ok

print(f"Deleted {len_ok} package")
if "GITHUB_OUTPUT" in os.environ:
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
f.write(f"num_deleted={len_ok}\n")
if len_fail > 0:
raise Exception(f"fail delete {len_fail}")

Expand Down

0 comments on commit f975b5e

Please sign in to comment.