-
Notifications
You must be signed in to change notification settings - Fork 22
ci: establish workflow to clean up image artifacts #390
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a6acc4
ci: establish workflow to clean up image artifacts
crookedstorm aa9a69e
ci: remove PR only job
crookedstorm ebfec7d
ci: adjust when the comment job runs and add space to test a build
crookedstorm 53bf03b
chore: remove test change from makefile
crookedstorm 1c0276c
ci: fix the cache read settings in some jobs
crookedstorm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,108 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| if ! [[ "${STALE_HOURS}" =~ ^[0-9]+$ ]]; then | ||
| echo "stale_hours must be a positive integer, got: ${STALE_HOURS}" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ "${STALE_HOURS}" -lt 1 ]; then | ||
| echo "stale_hours must be at least 1, got: ${STALE_HOURS}" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| owner="${GITHUB_REPOSITORY_OWNER,,}" | ||
| repo="${GITHUB_REPOSITORY#*/}" | ||
| repo="${repo,,}" | ||
|
|
||
| date_utc() { | ||
| if command -v gdate >/dev/null 2>&1; then | ||
| gdate -u -d "$1" '+%Y-%m-%dT%H:%M:%SZ' | ||
| return | ||
| fi | ||
|
|
||
| date -u -d "$1" '+%Y-%m-%dT%H:%M:%SZ' | ||
| } | ||
|
|
||
| cutoff="$(date_utc "${STALE_HOURS} hours ago")" | ||
|
|
||
| # GitHub's package version API exposes updated_at, not a separate last-pulled timestamp. | ||
| # These are the docker-cpu images published by .github/workflows/ci.yaml. | ||
| images=( | ||
| "nmp-api" | ||
| "nmp-core" | ||
| "nmp-cpu-tasks" | ||
| ) | ||
|
|
||
| echo "Scanning ghcr.io/${owner}/${repo} image versions last updated before ${cutoff}" | ||
| echo "dry_run=${DRY_RUN}" | ||
|
|
||
| total_deleted=0 | ||
| total_candidates=0 | ||
|
|
||
| url_encode() { | ||
| jq -nr --arg value "$1" '$value|@uri' | ||
| } | ||
|
|
||
| find_stale_versions() { | ||
| local endpoint="$1" | ||
| local jq_filter | ||
| jq_filter=" | ||
| .[] | | ||
| select(.updated_at != null and .updated_at < \"${cutoff}\") | | ||
| select(((.metadata.container.tags // []) | index(\"latest\")) | not) | | ||
| [.id, .name, .updated_at, ((.metadata.container.tags // []) | join(\",\"))] | | ||
| @tsv | ||
| " | ||
|
|
||
| gh api --paginate "${endpoint}/versions?per_page=100" --jq "${jq_filter}" | ||
| } | ||
|
|
||
| for image in "${images[@]}"; do | ||
| package_name="${repo}/${image}" | ||
| encoded_package_name="$(url_encode "${package_name}")" | ||
| endpoint="/orgs/${owner}/packages/container/${encoded_package_name}" | ||
|
|
||
| echo "::group::${package_name}" | ||
|
|
||
| api_error="$(mktemp)" | ||
| if ! stale_versions="$(find_stale_versions "${endpoint}" 2>"${api_error}")"; then | ||
| if grep -q "HTTP 404" "${api_error}"; then | ||
| echo "Package ${package_name} was not found; skipping." | ||
| echo "::endgroup::" | ||
| continue | ||
| fi | ||
|
|
||
| cat "${api_error}" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ -z "${stale_versions}" ]; then | ||
| echo "No stale package versions found." | ||
| echo "::endgroup::" | ||
| continue | ||
| fi | ||
|
|
||
| while IFS=$'\t' read -r version_id digest updated_at tags; do | ||
| if [ -z "${version_id}" ]; then | ||
| continue | ||
| fi | ||
|
|
||
| total_candidates=$((total_candidates + 1)) | ||
| tag_summary="${tags:-<untagged>}" | ||
| echo "Candidate ${package_name}@${digest} updated_at=${updated_at} tags=${tag_summary}" | ||
|
|
||
| if [ "${DRY_RUN}" = "true" ]; then | ||
| continue | ||
| fi | ||
|
|
||
| gh api -X DELETE "${endpoint}/versions/${version_id}" >/dev/null | ||
| total_deleted=$((total_deleted + 1)) | ||
| echo "Deleted package version ${version_id}" | ||
| done <<< "${stale_versions}" | ||
|
|
||
| echo "::endgroup::" | ||
| done | ||
|
|
||
| echo "Stale candidates: ${total_candidates}" | ||
| echo "Deleted versions: ${total_deleted}" |
This file contains hidden or 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
This file contains hidden or 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,45 @@ | ||
| name: Cleanup GHCR CI Images | ||
|
|
||
| on: | ||
| schedule: | ||
| - cron: "43 4 * * *" | ||
| workflow_dispatch: | ||
| inputs: | ||
| dry_run: | ||
| description: Log stale GHCR package versions without deleting them. | ||
| required: false | ||
| type: boolean | ||
| default: false | ||
| stale_hours: | ||
| description: Delete versions last updated more than this many hours ago. | ||
| required: false | ||
| type: number | ||
| default: 24 | ||
|
|
||
| permissions: {} | ||
|
|
||
| concurrency: | ||
| group: ghcr-cleanup-${{ github.repository }} | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| cleanup: | ||
| name: Delete stale GHCR CI image versions | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| packages: write | ||
| env: | ||
| DRY_RUN: ${{ inputs.dry_run || 'false' }} | ||
| STALE_HOURS: ${{ inputs.stale_hours || '24' }} | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - name: Delete stale package versions | ||
| shell: bash | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| run: bash .github/scripts/cleanup-ghcr-ci-images.sh |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.