Jenkins作业中的Choice构建参数的Ruby代码(通过Chef配置)

时间:2016-03-28 20:43:11

标签: ruby jenkins chef chef-recipe

我需要为我的一个自动构建做一个下拉/选择选择。

这是我从前端即时配置作业时可以观察到的完成的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>

我还附上了手动配置的截图。 enter image description here

最后,

以下是我为我的Chef Recipe编写的工作代码:

: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'=&gt;”这就是我现在所拥有的。我尝试的任何排列导致配置失败,或者下拉列表中的0个元素成功。

~EDIT:添加更多代码以帮助排除故障:

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

2 个答案:

答案 0 :(得分:2)

使用Chef,您可以使用主厨template resource生成作业的 config.xml 文件。

我还是建议使用Jenkins cookbook,其中 jenkins_job 资源将模板作为参数。

更新 - 食谱示例

我创建了以下&#34;演示&#34; cookbook来说明如何安装Jenkins并配置参数化作业。

├── Berksfile
├── metadata.rb
├── recipes
│   └── default.rb
├── templates
│   └── default
│       └── choice-job.xml.erb
└── attributes
    ├── java.rb
    └── jenkins.rb

配方/ 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 UI中显示为选项的3个参数

模板/默认/选择-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 -%>

&#34;选择&#34;被传递到模板

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"

Berksfile

source 'https://supermarket.chef.io'

metadata

注意:

  • Berkshelf从超市取消的依赖性

属性/ java.rb

normal['java']['jdk_version'] = '7'

注意:

  • Jenkins至少需要Java7

属性/ jenkins.rb

normal['jenkins']['master']['install_method'] = "war"
normal['jenkins']['master']['version']  = "1.655"
normal['jenkins']['master']['checksum'] = "0cee889af697c115961ce50229cc5e39d1b798c0a0a689687b745c0a938c8547"

注意:

  • 指定版本和校验和可以避免检查最新版本

答案 1 :(得分:0)

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”是我的jenkins作业的config.xml文件