Skip to content

Instantly share code, notes, and snippets.

@tolleiv
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolleiv/c2b220f69c320d9b3797 to your computer and use it in GitHub Desktop.
Save tolleiv/c2b220f69c320d9b3797 to your computer and use it in GitHub Desktop.
Automatic Jenkins job creation - used as a reference on blog.tolleiv.de
#!/usr/bin/ruby
##############################
# Parameters via environment variables
#
# COOKBOOK - List of recipes which we should have related jobs for
# SERVER - Jenkins server which is used for the job creation
# USER - related Jenkins user
# PASSWORD - related Jenkins password
##############################
require 'jenkins_api_client'
def get_existing
ENV['COOKBOOKS'].split("\n")
end
@client = JenkinsApi::Client.new(:server_ip => ENV['SERVER'],
:username => ENV['USER'], :password => ENV['PASSWORD'])
# The following call will return all jobs matching 'Testjob'
@client.logger = Logger.new('jobs.log', 10, 1024000)
jobs = @client.job.list("^devops_cookbook_.*-ci$")
@job = JenkinsApi::Client::Job.new(@client)
cookbooks = get_existing()
status = {}
cookbooks.each do |c|
status[c] = 'MISSING'
end
jobs.each do |jobname|
cookbook_name = jobname.gsub(/devops_cookbook_|-ci/, '')
config = @job.get_config(jobname)
is_custom = config.match('devops_cookbook_TEMPLATE').nil? #
is_enabled = config.match('<disabled>false</disabled>')
if status.keys.include?(cookbook_name)
if is_custom
status[cookbook_name] = "CUSTOM"
elsif is_enabled
status[cookbook_name] = "OK"
else
status[cookbook_name] = "OK (DISABLED)"
end
else
status[cookbook_name] = "MISSPELLED JOB"
end
end
conf_template = File.read('./resource-jobconfig.xml')
status.sort.each do |key, value|
puts "#{key} -> #{value}"
@job.create_or_update("devops_cookbook_#{key}-ci", conf_template.gsub('###COOKBOOK###', key)) if value == 'MISSING'
@job.build("devops_cookbook_#{key}-ci") if value == 'OK'
end
#!/bin/bash -l
cat > Gemfile <<"EOF"
source 'https://rubygems.org'
gem 'foodcritic', '>= 1.3.0'
EOF
source ~/.rvm/scripts/rvm
rvm use --install --create ruby@devops-cookbooks
export > rvm.env
if ! gem spec "bundler" > /dev/null 2>&1; then
gem install bundler --no-ri --no-rdoc
fi
bundle install
foodcritic --epic-fail any "./${COOKBOOK}/"
source 'https://rubygems.org'
gem 'jenkins_api_client'
<project>
<actions/>
<description/>
<logRotator class="hudson.tasks.LogRotator">
<daysToKeep>-1</daysToKeep>
<numToKeep>25</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>-1</artifactNumToKeep>
</logRotator>
<keepDependencies>false</keepDependencies>
<properties/>
<scm class="hudson.plugins.git.GitSCM" plugin="git@1.1.15">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<name/>
<refspec/>
<url>
git@git.yourserver.com:libraries/chef/cookbooks/###COOKBOOK###
</url>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>master</name>
</hudson.plugins.git.BranchSpec>
</branches>
<disableSubmodules>false</disableSubmodules>
<recursiveSubmodules>false</recursiveSubmodules>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<authorOrCommitter>false</authorOrCommitter>
<clean>false</clean>
<wipeOutWorkspace>false</wipeOutWorkspace>
<pruneBranches>false</pruneBranches>
<remotePoll>false</remotePoll>
<buildChooser class="hudson.plugins.git.util.DefaultBuildChooser"/>
<gitTool>Default</gitTool>
<submoduleCfg class="list"/>
<relativeTargetDir>###COOKBOOK###</relativeTargetDir>
<reference/>
<excludedRegions/>
<excludedUsers/>
<gitConfigName/>
<gitConfigEmail/>
<skipTag>false</skipTag>
<scmName/>
</scm>
<assignedNode>vagrant</assignedNode>
<canRoam>false</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers>
<hudson.triggers.SCMTrigger>
<spec>*/5 * * * *</spec>
<ignorePostCommitHooks>false</ignorePostCommitHooks>
</hudson.triggers.SCMTrigger>
</triggers>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.plugins.templateproject.ProxyBuilder plugin="template-project@1.4">
<projectName>devops_cookbook_TEMPLATE</projectName>
</hudson.plugins.templateproject.ProxyBuilder>
</builders>
<publishers>
<hudson.plugins.templateproject.ProxyPublisher plugin="template-project@1.4">
<projectName>devops_cookbook_TEMPLATE</projectName>
</hudson.plugins.templateproject.ProxyPublisher>
</publishers>
<buildWrappers>
<EnvInjectBuildWrapper plugin="envinject@1.85">
<info>
<loadFilesFromMaster>false</loadFilesFromMaster>
</info>
</EnvInjectBuildWrapper>
</buildWrappers>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment