forked from voxpupuli/puppet-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow build jobs to be configured and managed by puppet. Includes vox…
…pupuli#163 and resolves voxpupuli#120.
- Loading branch information
Showing
15 changed files
with
470 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class jenkins::job::build( | ||
$config = undef, | ||
$jobname = $title, | ||
$enabled = 1, | ||
$ensure = 'present', | ||
) { | ||
|
||
if $config == undef { | ||
$real_content = template('jenkins/job/build.xml.erb') | ||
} else { | ||
$real_content = $config | ||
} | ||
|
||
jenkins::job { 'build': | ||
config => $real_content, | ||
jobname => $jobname, | ||
enabled => $enabled, | ||
ensure => $ensure, | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
contrib/examples/job-configuration/templates/build.xml.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<project> | ||
<actions/> | ||
<description></description> | ||
<keepDependencies>false</keepDependencies> | ||
<properties></properties> | ||
<scm class="hudson.scm.NullSCM"/> | ||
<canRoam>true</canRoam> | ||
<disabled>false</disabled> | ||
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> | ||
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> | ||
<triggers/> | ||
<concurrentBuild>false</concurrentBuild> | ||
<builders/> | ||
<publishers/> | ||
<buildWrappers/> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Define: jenkins::job | ||
# | ||
# This class create a new jenkins job given a name and config xml | ||
# | ||
# Parameters: | ||
# | ||
# config | ||
# the content of the jenkins job config file (required) | ||
# | ||
# jobname = $title | ||
# the name of the jenkins job | ||
# | ||
# enabled = true | ||
# whether to enable the job | ||
# | ||
# ensure = 'present' | ||
# choose 'absent' to ensure the job is removed | ||
# | ||
define jenkins::job( | ||
$config, | ||
$jobname = $title, | ||
$enabled = 1, | ||
$ensure = 'present', | ||
){ | ||
|
||
if ($ensure == 'absent') { | ||
jenkins::job::absent { $title: | ||
jobname => $jobname, | ||
} | ||
} else { | ||
jenkins::job::present { $title: | ||
config => $config, | ||
jobname => $jobname, | ||
enabled => $enabled, | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Define: jenkins::job::absent | ||
# | ||
# Removes a jenkins build job | ||
# | ||
# Parameters: | ||
# | ||
# config | ||
# the content of the jenkins job config file (required) | ||
# | ||
# jobname = $title | ||
# the name of the jenkins job | ||
# | ||
define jenkins::job::absent( | ||
$jobname = $title, | ||
){ | ||
include jenkins::cli | ||
|
||
if $jenkins::service_ensure == 'stopped' or $jenkins::service_ensure == false { | ||
fail('Management of Jenkins jobs requires \$jenkins::service_ensure to be set to \'running\'') | ||
} | ||
|
||
$tmp_config_path = "/tmp/${jobname}-config.xml" | ||
$job_dir = "/var/lib/jenkins/jobs/${jobname}" | ||
$config_path = "${job_dir}/config.xml" | ||
|
||
# Temp file to use as stdin for Jenkins CLI executable | ||
file { $tmp_config_path: | ||
ensure => absent, | ||
} | ||
|
||
# Delete the job | ||
exec { "jenkins delete-job ${jobname}": | ||
command => "${jenkins::cli::cmd} delete-job ${jobname}", | ||
logoutput => false, | ||
onlyif => "test -f ${config_path}", | ||
require => Exec['jenkins-cli'], | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Define: jenkins::job::present | ||
# | ||
# Creates or updates a jenkins build job | ||
# | ||
# Parameters: | ||
# | ||
# config | ||
# the content of the jenkins job config file (required) | ||
# | ||
# jobname = $title | ||
# the name of the jenkins job | ||
# | ||
# enabled = 1 | ||
# if the job should be enabled | ||
# | ||
define jenkins::job::present( | ||
$config, | ||
$jobname = $title, | ||
$enabled = 1, | ||
){ | ||
include jenkins::cli | ||
|
||
if $jenkins::service_ensure == 'stopped' or $jenkins::service_ensure == false { | ||
fail('Management of Jenkins jobs requires \$jenkins::service_ensure to be set to \'running\'') | ||
} | ||
|
||
$jenkins_cli = $jenkins::cli::cmd | ||
$tmp_config_path = "/tmp/${jobname}-config.xml" | ||
$job_dir = "/var/lib/jenkins/jobs/${jobname}" | ||
$config_path = "${job_dir}/config.xml" | ||
|
||
Exec { | ||
logoutput => false, | ||
path => '/bin:/usr/bin:/sbin:/usr/sbin', | ||
tries => 5, | ||
try_sleep => 5, | ||
} | ||
|
||
# | ||
# When a Jenkins job is imported via the cli, Jenkins will | ||
# re-format the xml file based on its own internal rules. | ||
# In order to make job management idempotent, we need to | ||
# apply that formatting before the import, so we can do a diff | ||
# on any pre-existing job to determine if an update is needed. | ||
# | ||
# Jenkins likes to change single quotes to double quotes | ||
$a = regsubst($config, 'version=\'1.0\' encoding=\'UTF-8\'', | ||
'version="1.0" encoding="UTF-8"') | ||
# Change empty tags into self-closing tags | ||
$b = regsubst($a, '<([a-z]+)><\/\1>', '<\1/>', 'IG') | ||
# Change " to " since Jenkins is wierd like that | ||
$c = regsubst($b, '"', '"', 'MG') | ||
|
||
# Temp file to use as stdin for Jenkins CLI executable | ||
file { $tmp_config_path: | ||
content => $c, | ||
require => Exec['jenkins-cli'], | ||
} | ||
|
||
# Use Jenkins CLI to create the job | ||
$cat_config = "cat ${tmp_config_path}" | ||
$create_job = "${jenkins_cli} create-job ${jobname}" | ||
exec { "jenkins create-job ${jobname}": | ||
command => "${cat_config} | ${create_job}", | ||
creates => [$config_path, "${job_dir}/builds"], | ||
require => File[$tmp_config_path], | ||
} | ||
|
||
# Use Jenkins CLI to update the job if it already exists | ||
$update_job = "${jenkins_cli} update-job ${jobname}" | ||
exec { "jenkins update-job ${jobname}": | ||
command => "${cat_config} | ${update_job}", | ||
onlyif => "test -e ${config_path}", | ||
unless => "diff -b -q ${config_path} ${tmp_config_path}", | ||
require => File[$tmp_config_path], | ||
notify => Exec['reload-jenkins'], | ||
} | ||
|
||
# Enable or disable the job (if necessary) | ||
if ($enabled == 1) { | ||
exec { "jenkins enable-job ${jobname}": | ||
command => "${jenkins_cli} enable-job ${jobname}", | ||
onlyif => "cat ${config_path} | grep '<disabled>true'", | ||
require => [ | ||
Exec["jenkins create-job ${jobname}"], | ||
Exec["jenkins update-job ${jobname}"], | ||
], | ||
} | ||
} else { | ||
exec { "jenkins disable-job ${jobname}": | ||
command => "${jenkins_cli} disable-job ${jobname}", | ||
onlyif => "cat ${config_path} | grep '<disabled>false'", | ||
require => [ | ||
Exec["jenkins create-job ${jobname}"], | ||
Exec["jenkins update-job ${jobname}"], | ||
], | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Class: jenkins::jobs | ||
# | ||
class jenkins::jobs { | ||
|
||
if $caller_module_name != $module_name { | ||
fail("Use of private class ${name} by ${caller_module_name}") | ||
} | ||
|
||
create_resources('jenkins::job',$::jenkins::job_hash) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'spec_helper' | ||
|
||
describe 'jenkins', :type => :module do | ||
let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'RedHat' } } | ||
|
||
context 'jobs' do | ||
context 'default' do | ||
it { should contain_class('jenkins::jobs') } | ||
end | ||
|
||
context 'with one job' do | ||
let(:params) { { :job_hash => { 'build' => { 'config' => '<xml/>' } } } } | ||
it { should contain_jenkins__job('build').with_config('<xml/>') } | ||
end | ||
|
||
context 'with cli disabled' do | ||
let(:params) { { :service_ensure => 'stopped', | ||
:cli => false, | ||
:job_hash => { 'build' => { 'config' => '<xml/>' } } } } | ||
it { expect { should compile }.to raise_error } | ||
end | ||
|
||
end | ||
|
||
end |
Oops, something went wrong.