Skip to content

Commit

Permalink
Add output with the number of deleted package versions
Browse files Browse the repository at this point in the history
Use this output also to improve tests
  • Loading branch information
mering committed Nov 2, 2023
1 parent ca3edcb commit 43e8c4b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,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 }}" != 24 ]]; then
exit 1
fi
clean_untagged_pkgs2:
name: clean untagged packages
Expand All @@ -107,13 +113,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,13 +134,19 @@ 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, p2
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
Expand All @@ -137,9 +155,15 @@ 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: false
owner_type: user
- shell: bash
run: |
if [[ "${{ steps.deleted-action.outputs.num_deleted }}" != 1 ]]; 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&utm_medium=referral&utm_content=Chizkiyahu/delete-untagged-ghcr-action&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
5 changes: 5 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 @@ -159,7 +160,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 43e8c4b

Please sign in to comment.