1

I have a set of Jenkins jobs that are substantially the same. I have created a job template that creates them all. However, some have builders that others don't (i.e. the first in the chain doesn't copy artifacts from another project) and others have publishers that others don't (they don't all have JUnit tests).

I would like to conditionally include these modules depending on a variable, but I can't find a way of doing this:

  • I can't use a jinja2 template to include or exclude one item in a list
  • Including empty variables typically causes the build to fail
  • I could include yaml files, but I would need to include all of the builders section, and I would need one for each job, meaning a lot of repetition

Is this possible? I would like to include the comment section below in some of the jobs.

43     builders:                                                                   
44         - shell: |   
45             echo Removing working directory from previous run                  
46             rm -rf ${{WORKSPACE}}/css-build/working                             
47 #        - copyartifact:                                                        
48 #            project: "{previous-project}"                                      
49 #            whichbuild: last-successful                                        
50 #            optional: "{copy-optional}"                                        
51         - shell: |                                                              
52             {init-shell}                                                        
53             ${{WORKSPACE}}/css-build/build-util.sh {shell-args} ${{WORKSPACE}}/{location} -w ${{WORKSPACE}}/css-b    uild/working
4

2 回答 2

0

好吧,这是解决方法:

定义一个名称与原始模块不同的新模块(在这种情况下它将是一个构建器)。如果omit标签存在,不要做任何事情;否则,做无论如何都会发生的事情。

def optional_copy(registry, xml_parent, data):
    if data['omit'].lower() == 'true':
        return
    else:
        new_data = collections.OrderedDict()
        new_data['copyartifact'] = data
        registry.dispatch('builder', xml_parent, new_data)

将其注册到 jjb 中setup.py

setup(
        name='JJB config',
        py_modules = ['optionals'],
        entry_points={
            'jenkins_jobs.builders': [
             'optional-copy=optionals:optional_copy'
             ]
        }
    )

然后,在您的 yaml 中,您可以使用optional-copy模块和omit属性:

builders:
 - shell: |
      echo Removing working directory from previous run
      rm -rf "{working-dir}"
 - optional-copy:
       omit: "{omit-copy}"
       project: "{prev}"
       whichbuild: last-successful
 - shell: |
      {init-shell}
      ${{WORKSPACE}}/css-build/build-util.sh -u {diirt-version} {shell-args} -p ${{WORKSPACE}}/{location} -w "{working-dir}"
于 2018-07-19T15:54:13.043 回答
0

对于您的问题,我有一个不需要扩展工作生成器的解决方法。但它需要jenkins 上的Conditional build step 插件的可用性。

可选构建器的示例:

- job-template
    id: my-custom-template
    builders:
    - conditional-step:
        condition-kind: always
        steps: "{obj:optional_builders|[]}"

有了这个,您可以使用optional_builders变量(如果您愿意)将构建器添加到您的工作中。

jobs:
#With optional_builders
- my-custom-template:
    optional_builders:
    - copyartifact:                                                        
        project: "{previous-project}"                                      
        whichbuild: last-successful                                 

#Without optional_builders
- my-custom-template:

可选发布者的示例:

publishers:
- conditional-publisher:
  - condition-kind: always
    action: "{obj:optional_publishers|[]}"
于 2018-10-29T09:59:35.237 回答