Since releasing thePackages & Versions API, several customers have used it prune packages in their repository, automatically yanking old packages as new ones are uploaded.
This blog post will show an example of how to implement pruning yourself, using Ruby.
Lets say we have aJenkinsjob that automatically pushes topackagecloud.ioevery time there is a new build. However, we only want to keep the last 5 builds.
To accomplish this, we will check the package versionsAPIafter every push to see if we have hit our limit (5), if so,yankthe oldest package.
The script:prune.rb
We’ll begin by creating a basicprune.rbscript and listing all of our debian packages.
To keep our script simple, we’ll install therest-clientgem to help us with our API calls.
If you haven’t done so already, you can get your PackagecloudAPI token here.
Listing all debian packages
Run the script and the output should resemble something like:
This is a list of all your debian packages for the repository, among them is aredispackage with 3 versions forubuntu/utopic.
For the purposes of this example, this is the package we’ve decided to prune.
Let’s update ourprune.rbto use theversions_urlreturned above to get all the versions forredis.
Listing allredisversions
This should print out a list of all the different versions for ourredispackage.
Now, let’s sort this list by thecreated_atdate (ascending is the default), and yank the first entry if we are over our limit of 5 packages.
You could also sort byversion, orreleasehere, if you wish.
Sortredisversions bycreated_at
Run this script again, and if you are at theLIMIT, you should see the oldest entry printed out.
If it looks correct, we go ahead and prune the package. Here is the final version of the script:
Prune oldestredisversion if aboveLIMIT
Conclusion
By combining the packageversionsanddestroyAPI with some simple scripting, we were able to achieve our goal. Make sure to explore the rest of ourAPIto find other ways you can improve your software delivery pipeline using Packagecloud.
Don’t hesitate tocontactus with any questions or issues you have using our API or service, we love hearing from our users.