Lets say we have a Jenkins job that automatically pushes to packagecloud.io every time there is a new build. However, we only want to keep the last 5 builds.
To accomplish this, we will check the package versions API after every push to see if we have hit our limit (5), if so, yank the oldest package.
The script: prune.rb
We’ll begin by creating a basic prune.rb script and listing all of our debian packages.
To keep our script simple, we’ll install the rest-client gem to help us with our API calls.
If you haven’t done so already, you can get your packagecloud API 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 a redis package with 3 versions for ubuntu/utopic.
For the purposes of this example, this is the package we’ve decided to prune.
Let’s update our prune.rb to use the versions_url returned above to get all the versions for redis.
Listing all redis versions
This should print out a list of all the different versions for our redis package.
Now, let’s sort this list by the created_at date (ascending is the default), and yank the first entry if we are over our limit of 5 packages.
You could also sort by version, or release here, if you wish.
Sort redis versions by created_at
Run this script again, and if you are at the LIMIT, 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 oldest redis version if above LIMIT
By combining the package versions and destroy API with some simple scripting, we were able to achieve our goal. Make sure to explore the rest of our API to find other ways you can improve your software delivery pipeline using packagecloud.
Don’t hesitate to contact us with any questions or issues you have using our API or service, we love hearing from our users.