ci: move cleaning scripts to .github/actions/ghcr-clean

This commit is contained in:
lowlighter
2022-09-05 15:33:18 -04:00
parent ec7b9884e4
commit 3dbd8b8fd5
4 changed files with 2 additions and 2 deletions

View File

@@ -16,6 +16,6 @@ jobs:
uses: actions/checkout@v2
- name: Run script
run: ./delete_ghcr_dangling_images.sh lowlighter metrics
working-directory: .github/workflows/maintenance
working-directory: .github/actions/ghcr-clean
env:
GITHUB_TOKEN: ${{ secrets.GHCR_BOT_TOKEN }}

View File

@@ -1,37 +0,0 @@
#!/bin/bash
set -e
# Simple script to remove dangling images from GHCR
# You need to be logged to 'gh' first.
# package_name syntax. i.e: jellyfin-vue
owner="$1"
container="$2"
temp_file="ghcr_prune.ids"
rm -rf $temp_file
echo "Fetching dangling images from GHCR..."
gh api /users/${owner}/packages/container/${container}/versions --paginate > $temp_file
ids_to_delete=$(cat "$temp_file" | jq -r '.[] | select(.metadata.container.tags==[]) | .id')
if [ "${ids_to_delete}" = "" ]
then
echo "There are no dangling images to remove for this package"
exit 0
fi
echo -e "\nDeleting dangling images..."
while read -r line; do
id="$line"
echo "Processing image $id"
## Workaround for https://github.com/cli/cli/issues/4286 and https://github.com/cli/cli/issues/3937
set +e
echo -n | gh api --method DELETE /users/${owner}/packages/container/${container}/versions/${id} --input -
test $? -eq 0 && echo Dangling image with ID $id deleted successfully
set -e
done <<< $ids_to_delete
rm -rf $temp_file
echo -e "\nAll the dangling images have been removed successfully"
exit 0

View File

@@ -1,56 +0,0 @@
#!/bin/bash
set -e
# Simple script to remove workflows that are not successful or failed (with completion status like skipped, cancelled, etc.)
# It also removes successful or failed workflows that doesn't have artifacts or logs.
# You need to be logged to 'gh' first
# owner/repo syntax
repo="$1"
temp_file="workflow_runs.payload"
rm -rf $temp_file
echo "Fetching workflows..."
echo $(gh api /repos/${repo}/actions/runs --paginate | jq '{count: .total_count, runs: [.workflow_runs[] | select(.status=="completed") | {id, conclusion}]}') | jq '.' > $temp_file
ids_to_delete=$(cat "$temp_file" | jq -r '.runs | .[] | select((.conclusion!="success") and (.conclusion!="failure")) | .id')
if [ "${ids_to_delete}" = "" ]
then
echo "All workflows are exited successfully or failed. Nothing to remove"
else
echo "Removing all the workflows that are not successful or failed..."
while read -r line; do
id="$line"
echo "Processing workflow $id"
## Workaround for https://github.com/cli/cli/issues/4286 and https://github.com/cli/cli/issues/3937
echo -n | gh api --method DELETE /repos/${repo}/actions/runs/${id} --input -
echo "Stale workflow run with ID $id deleted successfully!"
done <<< $ids_to_delete
fi
ids_to_delete=$(cat "$temp_file" | jq -r '.runs | .[] | select((.conclusion=="success") or (.conclusion=="failure")) | .id')
if [ "${ids_to_delete}" = "" ]
then
echo "No workflows to check for logs or artifacts. Exiting..."
else
echo -e "\nDeleting workflows without logs and artifacts..."
while read -r line; do
id="$line"
echo "Processing workflow $id"
artifact_count=$(gh api /repos/${repo}/actions/runs/${id}/artifacts | jq -r '.total_count')
if [ "${artifact_count}" = "0" ]
then
gh api --silent /repos/${repo}/actions/runs/${id}/logs || \
echo -n | gh api --method DELETE /repos/${repo}/actions/runs/${id} --input - && \
echo "Workflow run without logs and artifacts with ID $id deleted successfully!"
fi
done <<< $ids_to_delete
fi
rm -rf $temp_file
echo -e "\n\nFinished the workflow run cleaning process"
exit 0

View File

@@ -43,6 +43,6 @@ jobs:
uses: actions/checkout@v2
- name: Run script
run: ./delete_workflows.sh lowlighter/metrics
working-directory: .github/workflows/maintenance
working-directory: .github/actions/ghcr-clean
env:
GITHUB_TOKEN: ${{ github.token }}