Ruby Jenkins 作业中 Choice 构建参数的代码(通过 Chef 配置)
Ruby code for Choice build parameter within Jenkins job (configured via Chef)
我需要为我的一个自动构建进行下拉/选择选择。
这是我从前端即时配置作业时可以观察到的完成的 XML(我需要将其转换为我正在创建的 Chef 食谱):
<property>
<parameterDefinition>
<defaultParameterValue>
<name>DATA_BAG_NAME</name>
<value>X</value>
</defaultParameterValue>
<description>Select the data bag that contains the job above.</description>
<name>DATA_BAG_NAME</name>
<type>ChoiceParameterDefinition</type>
<choice>X</choice>
<choice>Y</choice>
<choice>Z</choice>
</parameterDefinition>
</property>
我还附上了手动配置的屏幕截图。
最后,
这是我为我的厨师食谱编写的 非 工作代码:
:build_params => [
{ 'name' => 'JENKINS_ID_TO_ADD', 'type' => 'String', 'default' => '',
'description' => ' Enter the JenkinsID you want to add, example: PRCalculator' },
{ 'name' => 'DATA_BAG_NAME', 'type' => 'Choice', 'choices' =>
'description' => ' Select the data bag that contains the job above.' }
],
请帮助我确定我需要哪种类型的 Ruby 语法才能使下拉列表实际包含值,而不是我现在拥有的“'choices' =>”。我尝试的任何排列都会导致配置失败或下拉列表中的 0 个元素成功。
~编辑:添加更多代码以帮助解决问题:
config_name = 'free-style'
job_name = "flag-chef-add-jenkins-id"
job_config = File.join(Chef::Config[:file_cache_path], "#{job_name}-config.xml")
template job_config do
source File.join('jenkins',"job-#{config_name}-config.xml.erb")
variables :job_name => job_name,
:max_builds => '15',
:build_params => [
{ 'name' => 'JENKINS_ID_TO_ADD', 'type' => 'String', 'default' => '',
'description' => ' Enter the JenkinsID you want to add, example: MyJobID' },
{ 'name' => 'DATA_BAG_NAME', 'type' => 'Choice', 'choice' => '1', 'choice' => '2',
'description' => ' Select the data bag that contains the job above.' }
],
:command => "
if [ \"$JENKINS_ID_TO_ADD\" != \"\" ]; then
# run gimmicky update that wont work!
echo \"Jenkins ID to add or update: \"
echo \"Running Jenkins_ID_To_Add job for the following ID: $JENKINS_ID_TO_ADD .'\n\" >> jenkinsIDTest.txt
echo recipe['jenkins::master'],recipe[\"flag_utils::data_bags::promo_lps::$JENKINS_ID_TO_ADD\"] >> jenkinsIDTest.txt
sudo -u root -i chef-client -o recipe['jenkins::master'],recipe[\"flag_utils::data_bags::promo_lps::$JENKINS_ID_TO_ADD\"] --force-formatter >> jenkinsIDTest.txt
fi
",
:email_release_subject => 'Flag Utils Chef add Jenkins ID run! $JENKINS_ID_TO_ADD',
:admin_emails => admin_emails,
:notification_emails => notification_emails
end
jenkins_job job_name do
config job_config
end
使用 Chef,您可以使用 Chef template resource.
生成作业的 config.xml 文件
更好的是,我建议使用 Jenkins cookbook,它有一个 jenkins_job 资源,该资源将模板作为参数。
更新 - 食谱示例
我创建了以下 "demo" 说明书来说明如何安装 Jenkins 和配置参数化作业。
├── Berksfile
├── metadata.rb
├── recipes
│ └── default.rb
├── templates
│ └── default
│ └── choice-job.xml.erb
└── attributes
├── java.rb
└── jenkins.rb
recipes/default.rb
#
# Cookbook Name:: demo
# Recipe:: default
#
# Copyright (c) 2016 The Authors, All Rights Reserved.
#
include_recipe "apt"
include_recipe "java"
include_recipe "jenkins::master"
#
# Jenkins job
#
jobxml = File.join(Chef::Config[:file_cache_path], 'choice-job.xml')
template jobxml do
source "choice-job.xml.erb"
variables :choices => ["X", "Y", "Z"]
end
jenkins_job "Choice Demo" do
config jobxml
end
注:
- xml 文件由模板生成并传递给 Jenkins 作业资源。
- 传递将在 Jenkins 中显示为选项的 3 个参数 UI
templates/default/choice-job.xml.erb
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description>Demo job</description>
<keepDependencies>false</keepDependencies>
<properties>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<hudson.model.ChoiceParameterDefinition>
<name>DATA_BAG_NAME</name>
<description></description>
<choices class="java.util.Arrays$ArrayList">
<a class="string-array">
<% @choices.each do |choice| -%>
<string><%= choice %></string>
<% end -%>
</a>
</choices>
</hudson.model.ChoiceParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>env</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>
注意以下片段:
<% @choices.each do |choice| -%>
<string><%= choice %></string>
<% end -%>
将"choices"传入模板
metadata.rb
name 'demo'
maintainer 'The Authors'
maintainer_email 'you@example.com'
license 'all_rights'
description 'Installs/Configures demo'
long_description 'Installs/Configures demo'
version '0.1.0'
depends "apt"
depends "java"
depends "jenkins"
伯克斯文件
source 'https://supermarket.chef.io'
metadata
注:
- Berkshelf 从超市中提取的依赖项
attributes/java.rb
normal['java']['jdk_version'] = '7'
注:
- Jenkins 至少需要 Java7
attributes/jenkins.rb
normal['jenkins']['master']['install_method'] = "war"
normal['jenkins']['master']['version'] = "1.655"
normal['jenkins']['master']['checksum'] = "0cee889af697c115961ce50229cc5e39d1b798c0a0a689687b745c0a938c8547"
注:
- 指定版本和校验和避免了检查最新版本的需要
xml = File.join(Chef::Config[:file_cache_path], 'rconfig.xml')
template xml do
source 'jconfig.xml.erb'
end
jenkins_job 'project' do
config xml
action :create
end
这是代码示例。这可能会有所帮助。 "jconfig.xml" 是我的 config.xml jenkins 作业文件
我需要为我的一个自动构建进行下拉/选择选择。
这是我从前端即时配置作业时可以观察到的完成的 XML(我需要将其转换为我正在创建的 Chef 食谱):
<property>
<parameterDefinition>
<defaultParameterValue>
<name>DATA_BAG_NAME</name>
<value>X</value>
</defaultParameterValue>
<description>Select the data bag that contains the job above.</description>
<name>DATA_BAG_NAME</name>
<type>ChoiceParameterDefinition</type>
<choice>X</choice>
<choice>Y</choice>
<choice>Z</choice>
</parameterDefinition>
</property>
我还附上了手动配置的屏幕截图。
最后,
这是我为我的厨师食谱编写的 非 工作代码:
:build_params => [
{ 'name' => 'JENKINS_ID_TO_ADD', 'type' => 'String', 'default' => '',
'description' => ' Enter the JenkinsID you want to add, example: PRCalculator' },
{ 'name' => 'DATA_BAG_NAME', 'type' => 'Choice', 'choices' =>
'description' => ' Select the data bag that contains the job above.' }
],
请帮助我确定我需要哪种类型的 Ruby 语法才能使下拉列表实际包含值,而不是我现在拥有的“'choices' =>”。我尝试的任何排列都会导致配置失败或下拉列表中的 0 个元素成功。
~编辑:添加更多代码以帮助解决问题:
config_name = 'free-style'
job_name = "flag-chef-add-jenkins-id"
job_config = File.join(Chef::Config[:file_cache_path], "#{job_name}-config.xml")
template job_config do
source File.join('jenkins',"job-#{config_name}-config.xml.erb")
variables :job_name => job_name,
:max_builds => '15',
:build_params => [
{ 'name' => 'JENKINS_ID_TO_ADD', 'type' => 'String', 'default' => '',
'description' => ' Enter the JenkinsID you want to add, example: MyJobID' },
{ 'name' => 'DATA_BAG_NAME', 'type' => 'Choice', 'choice' => '1', 'choice' => '2',
'description' => ' Select the data bag that contains the job above.' }
],
:command => "
if [ \"$JENKINS_ID_TO_ADD\" != \"\" ]; then
# run gimmicky update that wont work!
echo \"Jenkins ID to add or update: \"
echo \"Running Jenkins_ID_To_Add job for the following ID: $JENKINS_ID_TO_ADD .'\n\" >> jenkinsIDTest.txt
echo recipe['jenkins::master'],recipe[\"flag_utils::data_bags::promo_lps::$JENKINS_ID_TO_ADD\"] >> jenkinsIDTest.txt
sudo -u root -i chef-client -o recipe['jenkins::master'],recipe[\"flag_utils::data_bags::promo_lps::$JENKINS_ID_TO_ADD\"] --force-formatter >> jenkinsIDTest.txt
fi
",
:email_release_subject => 'Flag Utils Chef add Jenkins ID run! $JENKINS_ID_TO_ADD',
:admin_emails => admin_emails,
:notification_emails => notification_emails
end
jenkins_job job_name do
config job_config
end
使用 Chef,您可以使用 Chef template resource.
生成作业的 config.xml 文件更好的是,我建议使用 Jenkins cookbook,它有一个 jenkins_job 资源,该资源将模板作为参数。
更新 - 食谱示例
我创建了以下 "demo" 说明书来说明如何安装 Jenkins 和配置参数化作业。
├── Berksfile
├── metadata.rb
├── recipes
│ └── default.rb
├── templates
│ └── default
│ └── choice-job.xml.erb
└── attributes
├── java.rb
└── jenkins.rb
recipes/default.rb
#
# Cookbook Name:: demo
# Recipe:: default
#
# Copyright (c) 2016 The Authors, All Rights Reserved.
#
include_recipe "apt"
include_recipe "java"
include_recipe "jenkins::master"
#
# Jenkins job
#
jobxml = File.join(Chef::Config[:file_cache_path], 'choice-job.xml')
template jobxml do
source "choice-job.xml.erb"
variables :choices => ["X", "Y", "Z"]
end
jenkins_job "Choice Demo" do
config jobxml
end
注:
- xml 文件由模板生成并传递给 Jenkins 作业资源。
- 传递将在 Jenkins 中显示为选项的 3 个参数 UI
templates/default/choice-job.xml.erb
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description>Demo job</description>
<keepDependencies>false</keepDependencies>
<properties>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<hudson.model.ChoiceParameterDefinition>
<name>DATA_BAG_NAME</name>
<description></description>
<choices class="java.util.Arrays$ArrayList">
<a class="string-array">
<% @choices.each do |choice| -%>
<string><%= choice %></string>
<% end -%>
</a>
</choices>
</hudson.model.ChoiceParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>env</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>
注意以下片段:
<% @choices.each do |choice| -%>
<string><%= choice %></string>
<% end -%>
将"choices"传入模板
metadata.rb
name 'demo'
maintainer 'The Authors'
maintainer_email 'you@example.com'
license 'all_rights'
description 'Installs/Configures demo'
long_description 'Installs/Configures demo'
version '0.1.0'
depends "apt"
depends "java"
depends "jenkins"
伯克斯文件
source 'https://supermarket.chef.io'
metadata
注:
- Berkshelf 从超市中提取的依赖项
attributes/java.rb
normal['java']['jdk_version'] = '7'
注:
- Jenkins 至少需要 Java7
attributes/jenkins.rb
normal['jenkins']['master']['install_method'] = "war"
normal['jenkins']['master']['version'] = "1.655"
normal['jenkins']['master']['checksum'] = "0cee889af697c115961ce50229cc5e39d1b798c0a0a689687b745c0a938c8547"
注:
- 指定版本和校验和避免了检查最新版本的需要
xml = File.join(Chef::Config[:file_cache_path], 'rconfig.xml')
template xml do
source 'jconfig.xml.erb'
end
jenkins_job 'project' do
config xml
action :create
end
这是代码示例。这可能会有所帮助。 "jconfig.xml" 是我的 config.xml jenkins 作业文件