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

Add output with the number of deleted package versions #35

Closed
wants to merge 1 commit into from
Closed
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
40 changes: 36 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
i: [1, 2]
type: ['p1', 'p2', 'p3']
type: ['p1', 'p2', 'p3', 'p4']
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
strategy:
matrix:
i: [1, 2]
type: ['p1', 'p2', 'p3']
type: ['p1', 'p2', 'p3', 'p4']
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
Expand Down 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 }}" != 32 ]]; then
exit 1
fi

delete_package:
name: delete package
Expand All @@ -122,39 +134,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 }}" != 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 @@ -160,7 +161,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