Main image
15th May
2019
written by Chris

I love Terraform but, as we’re fond of saying at work, “it’s a loaded gun pointed at your production environment.” There are ways to minimize the risk but they boil down to writing a bunch of test automation around Terraform so as to be sure that it’s not doing something evil.

That’s a great idea, but no one much wants to pay for it. Toil budgets would be a great way to get buy in on this, but that’s another topic for another day.

This means that while I can easily spin up all kinds of expensive cloud resources just by running a Jenkins job, it’s easy to lose track of them and forget to spin them down. This then leads to an annoying hunt through a bunch of Jenkins console output panes trying to find environments with weird names like “trusting-manatee” and “polite-tick”

So, since I’m not allowed to have a script to clean up environments, I wrote one to find the jobs I need to promote in order to clean up environments. Hooray!

SEARCH_STRING = "lost-environment"  
Jenkins.instance.getAllItems(AbstractItem.class).each {
    if(it instanceof hudson.model.FreeStyleProject) { 
      for (build in it.builds) {
      	def log = build.log 
        if (log.contains(SEARCH_STRING)) {
    		println "${it.getFullName()}: ${build.id}"
        }
      }
    }
}

Is it fast? No. Is it elegant? No. Does it work? You bet.

Leave a Reply