Skip to content

Add GHCR cleanup workflow to delete old container images#3136

Open
AnjaliMishra1st wants to merge 8 commits intoopen-telemetry:mainfrom
AnjaliMishra1st:patch-1
Open

Add GHCR cleanup workflow to delete old container images#3136
AnjaliMishra1st wants to merge 8 commits intoopen-telemetry:mainfrom
AnjaliMishra1st:patch-1

Conversation

@AnjaliMishra1st
Copy link
Copy Markdown

This PR introduces a GitHub Actions workflow to clean up old container images from GHCR.

  • Runs daily using a scheduled workflow
  • Deletes older container image versions
  • Keeps the latest 5 versions

This helps reduce storage clutter and maintain a clean registry.

@AnjaliMishra1st AnjaliMishra1st requested a review from a team as a code owner March 20, 2026 13:09
@osullivandonal
Copy link
Copy Markdown
Contributor

@AnjaliMishra1st nice work on adding this job, just some things to consider here.

This cleanup job is quite aggressive, the otel demo release cadence is usually every few months when a number of changes have accumulated. This job would run every day which give that release cadence is very high. However, we do have a nightly build of images. Also another thing to consider is users of older images in the github container registry, the demo is used a lot by different users to showcase how to instrument a cloud service, we may break a lot of demo's by removing older versions of images.

For example using min-versions-to-keep: 5 would only keep less than a week of images in the nightly builds.

It would be great to hear from the maintainers here @open-telemetry/demo-maintainers .

@AnjaliMishra1st
Copy link
Copy Markdown
Author

Thanks for the helpful feedback!
I’ve updated the workflow to run weekly instead of daily and increased the number of versions to keep for safer retention.
Please let me know if this looks better or if further adjustments are needed.

@github-actions
Copy link
Copy Markdown

This PR was marked stale due to lack of activity. It will be closed in 7 days.

@github-actions github-actions bot added the Stale label Mar 28, 2026
@AnjaliMishra1st
Copy link
Copy Markdown
Author

Hi maintainers 👋
Just a gentle follow-up on this PR. Based on earlier feedback, I’ve updated the workflow to run weekly and increased the number of retained versions to make it safer for users.
Could you please review when you have time? I’d really appreciate your feedback.

@open-telemetry/demo-maintainers
@osullivandonal
Thanks again! 🙂

@github-actions github-actions bot removed the Stale label Mar 30, 2026
Copy link
Copy Markdown
Member

@julianocosta89 julianocosta89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnjaliMishra1st sorry for the delay, last week was KubeCon and I'm still playing catch!
Regarding this PR I think we need to adjust a couple of things.

The Demo currently releases nightly builds, but we also have releases.
If I understood this gh workflow correctly, that will clean up everything that is older than 20 versions, but it doesn't differentiate releases from nightly builds.
Which means that if we run this today, we would remove versions 2.2.0, 2.1.3, and so on.

Ideally the clean up would run just for the nightly builds.

@AnjaliMishra1st
Copy link
Copy Markdown
Author

Hi @julianocosta89 👋🏽
Thanks for the detailed feedback — that makes sense!
I understand that the workflow should not delete release versions and should only target nightly builds.
I’ll update the workflow to restrict cleanup to nightly images only and keep all release versions safe.

I’ll push the changes soon. Thanks again! 🙂

@puckpuck
Copy link
Copy Markdown
Contributor

puckpuck commented Apr 8, 2026

Is there a way we can run this in some kind of dry-run mode first to see what images it would delete?

@AnjaliMishra1st
Copy link
Copy Markdown
Author

Hi @julianocosta89 @puckpuck
Thank you for the helpful feedback! I’ve updated the workflow to include a dry-run mode and to restrict cleanup to nightly builds only, ensuring release versions remain safe.
Whenever you have time, I’d really appreciate your review and any further suggestions. Thank you! 🙂

@puckpuck
Copy link
Copy Markdown
Contributor

puckpuck commented Apr 9, 2026

This looks fine. As it's configured right now nothing destructive will happen, and at 3am it will execute in dry_run mode producing a list of images that it would delete.

If that looks good, we can do another PR to update the dry_run setting to be false.

@puckpuck
Copy link
Copy Markdown
Contributor

puckpuck commented Apr 9, 2026

@AnjaliMishra1st it's failing the yamllint and checklicense checks. We need to add a license copyright header to the file (see other GH action workflows for an example), and yamllint is likely extra white space or empty lines. The failed check output will have better details on the offending entries.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a GitHub Actions workflow intended to prune older GHCR container image versions to reduce registry storage usage in open-telemetry/opentelemetry-demo.

Changes:

  • Introduces a new scheduled + manually-dispatchable workflow for GHCR cleanup.
  • Uses GitHub CLI (gh api) to list container package versions and delete matching versions (currently based on nightly-* tags).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +6 to +9
on:
schedule:
- cron: '0 3 * * 0'
workflow_dispatch:
Comment on lines +37 to +40
versions=$(gh api \
-H "Accept: application/vnd.github+json" \
/orgs/open-telemetry/packages/container/otel-demo/versions \
--jq '.[].id')

versions=$(gh api \
-H "Accept: application/vnd.github+json" \
/orgs/open-telemetry/packages/container/otel-demo/versions \
Comment on lines +37 to +60
versions=$(gh api \
-H "Accept: application/vnd.github+json" \
/orgs/open-telemetry/packages/container/otel-demo/versions \
--jq '.[].id')

for version in $versions; do
tags=$(gh api \
-H "Accept: application/vnd.github+json" \
/orgs/open-telemetry/packages/container/otel-demo/versions/$version \
--jq '.metadata.container.tags[]?')

for tag in $tags; do
if [[ "$tag" == nightly-* ]]; then
echo "Found nightly image: $tag (version: $version)"

if [ "$DRY_RUN" = "true" ]; then
echo "[DRY RUN] Would delete version ID: $version"
else
echo "Deleting version ID: $version"
gh api --method DELETE \
-H "Accept: application/vnd.github+json" \
/orgs/open-telemetry/packages/container/otel-demo/versions/$version
fi
else
gh api --method DELETE \
-H "Accept: application/vnd.github+json" \
/orgs/open-telemetry/packages/container/otel-demo/versions/$version
fi

steps:
- name: Checkout repository
uses: actions/checkout@v3
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After hash add some comment related to version, see other files


jobs:
cleanup:
runs-on: ubuntu-latest
Comment on lines +24 to +25
- name: Install GitHub CLI
run: sudo apt-get update && sudo apt-get install gh -y

steps:
- name: Checkout repository
uses: actions/checkout@v3
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All other GH actions pins versions to the exact commit + tag comment.
It should be applied also here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I’ve updated this to use a pinned commit SHA and added a version comment for consistency with other workflows.

uses: actions/checkout@v3

- name: Install GitHub CLI
run: sudo apt-get update && sudo apt-get install gh -y
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure that it is needed? I think that GitHub CLI should be available by default on all-defualt runmers.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I’ve removed the installation step since GitHub CLI is already available on default runners.

run: sudo apt-get update && sudo apt-get install gh -y

- name: Authenticate GitHub CLI
run: echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this one is also not-necessary. In OTel .NET repository there is usage of gh-cli, but there is no need to login.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out, removed the unnecessary authentication step.


steps:
- name: Checkout repository
uses: actions/checkout@v3
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After hash add some comment related to version, see other files

AnjaliMishra1st and others added 2 commits April 10, 2026 18:06
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@AnjaliMishra1st
Copy link
Copy Markdown
Author

Hi @Kielek @julianocosta89 @puckpuck
Thanks for the helpful feedback!
I’ve made the suggested updates:

  • Pinned checkout action with SHA
  • Removed unnecessary GitHub CLI setup
  • Added required permissions
  • Fixed dry-run logic and pagination
  • Ensured safe deletion with break and tag handling
    I’ve kept the package name as-is for now—happy to adjust if needed.
    Please let me know if anything else should be improved. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants