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.
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.
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.
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
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.