How to define a Label parameter in a parameterized build using scripted pipeline approach

六月ゝ 毕业季﹏ 提交于 2019-12-13 03:49:50

问题


I'm trying to solve the same problem as this SO question: How to trigger a jenkins build on specific node using pipeline plugin?

The only difference in my case is that the job I'm triggering is another scripted pipeline job. So the second step in the proposed solution does not apply in my case:

  1. Install Node and Label parameter plugin
  2. In test_job's configuration, select 'This build is parameterized' and add a Label parameter and set the parameter name to 'node'
  3. In pipeline script, use code (code omitted)

My question is how to define the :

org.jvnet.jenkins.plugins.nodelabelparameter.LabelParameterDefinition

parameter inside my scripted pipeline parameterized job (not through the GUI).

What I have tried:

properties([[$class         : 'RebuildSettings',
         autoRebuild    : false,
         rebuildDisabled: false],
         parameters([org.jvnet.jenkins.plugins.nodelabelparameter.LabelParameterDefinition(name: 'node')])])

回答1:


The easiest way to generate the code you need for your parameterized scripted pipeline is to:

  1. Go to Pipeline Snippet Generator
  2. Select "properties: Set job properties"
  3. Check "This project is parameterized"
  4. Click "Add parameter" and select "Label"
  5. Click "Generate pipeline script"

This gives you:

properties([

    [$class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: false], 

    parameters([
        [$class: 'LabelParameterDefinition', 
            allNodesMatchingLabel: false, 
            defaultValue: '', 
            description: '', 
            name: 'node', 
            nodeEligibility: [$class: 'AllNodeEligibility'], t
            riggerIfResult: 'allCases']
        ]
    )

])

But in my case this wasn't even necessary. All you need is a regular string parameter with a custom name, lets say "node" and then do:

node(params.node){}


来源:https://stackoverflow.com/questions/55470917/how-to-define-a-label-parameter-in-a-parameterized-build-using-scripted-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!