Jenkins job management


#1

So I’m working on a jenkins wrapper cookbook (using the daften:feature/jenkins2 branch for the Jenkins cookbook, as we’re using 2.x), and right now I’ve got a set of cookbook_files that contain the jobs, in subdirectories for each environment. So for our Bellevue office, they’re in cookbook moxie-jenkins/files/default/bel_jobs/filename.xml.

Right now I’m defining a static list of each of these in the recipe, looping over them to determine the filename, doing cookbook_file on that, and then jenkins_job on each xml file. Is there a better way that I can handle determining the list of jobs? Ultimately it would be nice to just be able to add the job file to the right directory, bump the version, and have it get autodetected. I’m not really sure how to go about that, though. Suggestions welcome.

I’ve considered creating databag items for each job instead, but then we’d be managing a set of JSON files for XML content and that doesn’t seem like a good route to go down. Plus I know how to look up a databag item, but I’m not sure how to get a list of the items (although I suspect that is available in the API and I just don’t know the method).

Thanks for any suggestions.


#2

Maybe instead of cookbook_file’ing them one by one based on a hard coded list, you can use remote_directory the whole directory for your environment, and then loop over the files on the node?

Kevin Keane
Whom the IT Pros Call
The NetTech
http://www.4nettech.com
Our values: Privacy, Liberty, Justice
See https://www.4nettech.com/corp/the-nettech-values.html


#3

Hello @nclemons

Did you want to automatically pick up the XML files as you place them in the cookbook?

Bit horrible but you could look at the cookbook_collection on the run_context, it contains a manifest for each cookbook which has details about the files and templates associated with the cookbook. The type for each item in the cookbook_collection is Chef::CookbookVersion

cb = run_context.cookbook_collection[cookbook_name]
cookbook_files = cb.manifest['files']
file = cookbook_files.first
file_location = cb.preferred_filename_on_disk_location(node, :files, file[:path].gsub('files/default', ''))

I take your point about data bags, you could iterate through them with something similar to the following

data_bag('jenkins').each do |item|
  job = data_bag_item('jenkins', item)
end

Or use Chef search to filter data bag items, the users cookbook does this.

Alternatively you could just have a simple manifest file that contains a list of the jenkins jobs?

The static list solution does have some advantages though, you can be explicit about what it deployed, if stored in an attribute can be modified by environments or role attributes.