如何在声明式管道中使用 NodeLabel 参数插件
How to use NodeLabel parameter plugin in declarative pipeline
我正在尝试将自由式作业转换为声明式管道作业,因为管道提供了更大的灵活性。但是,我不知道如何在管道中使用 NodeLabel 参数插件 (https://wiki.jenkins.io/display/JENKINS/NodeLabel+Parameter+Plugin)。
pipeline {
agent any
parameters {
// Would like something like LabelParameter here
}
stages {
stage('Dummy1') {
steps {
cleanWs()
sh('ls')
sh('pwd')
sh('hostname')
}
}
stage('Dummy2') {
steps {
node("comms-test02") {
sh('ls')
sh('pwd')
sh('hostname')
}
}
}
}
我基本上只需要一种方法来使用指定构建作业位置的参数(使用从属标签)来启动作业。
Jenkins 需要一个代理字段,我设置为 'any'。但是好像没有可用的标签参数?
作为替代方案,我尝试使用 'node' 命令(https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#-node- 分配节点)。但这给我留下了两个 运行ning 工作,虽然工作,但看起来并不那么漂亮。
有谁知道NodeLabel参数插件可以用吗?或者也许有人有更简洁的方法?
编辑:可能我没说清楚。我需要能够 运行 不同节点上的作业。 运行所在的节点应该在触发作业时通过参数来决定。节点标签插件完美地做到了这一点。但是,我无法在管道中重现此行为。
假设您在管道上使用 NodeLabel 插件添加了参数(例如命名为 slaveName
)。您现在需要提取 slaveName
的值并将其输入 agent->node->label 字段。
您可以使用代理中的节点属性指定节点。
像这样-
agent
{
node
{
label "${slaveName}"
}
}
这是一个完整的例子:
pipeline {
parameters {
choice(name: 'node', choices: [nodesByLabel('label')], description: 'The node to run on') //example 1: just listing all the nodes with label
choice(name: 'node2', choices: ['label'] + nodesByLabel('label'), description: 'The node to run on') //example 2: add the label itself as the first choice to make "Any of the nodes" the default choice
}
agent none
stages {
stage('Test') {
agent { label params.node}
stages {
stage('Print environment settings') {
steps {
echo "running on ${env.NODE_NAME}"
sh 'printenv | sort'
}
}
}
}
}
}
以下脚本对我有用 运行 在不同节点上并行执行多个作业。
我参考了构建步骤插件文档。
https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/
def build_one()
{
parallel one: {
stage('XYZ') {
catchError(buildResult: 'SUCCESS', stageResult:'FAILURE') {
build job: 'yourDownStreamJob', parameters: [[$class: 'NodeParameterValue', name: 'NodeToRun',labels: ['nodeName'], nodeEligibility: [$class: 'AllNodeEligibility']], string(name: 'ParentBuildName', value: "XX"), string(name: 'Browser', value: 'chrome'), string(name: 'Environment', value: 'envName')]
}
}
},
two : {
stage('SecondArea') {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
build job: 'yourDownStreamJob', parameters: [[$class: 'NodeParameterValue', name: 'NodeToRun',labels: ['Your'], nodeEligibility: [$class: 'AllNodeEligibility']], string(name: 'ParentBuildName', value: "XYX"), string(name: 'Browser', value: 'firefox'), string(name: 'Environment', value: 'envName')]
}
}
}
}
build_one()
我正在尝试将自由式作业转换为声明式管道作业,因为管道提供了更大的灵活性。但是,我不知道如何在管道中使用 NodeLabel 参数插件 (https://wiki.jenkins.io/display/JENKINS/NodeLabel+Parameter+Plugin)。
pipeline {
agent any
parameters {
// Would like something like LabelParameter here
}
stages {
stage('Dummy1') {
steps {
cleanWs()
sh('ls')
sh('pwd')
sh('hostname')
}
}
stage('Dummy2') {
steps {
node("comms-test02") {
sh('ls')
sh('pwd')
sh('hostname')
}
}
}
}
我基本上只需要一种方法来使用指定构建作业位置的参数(使用从属标签)来启动作业。
Jenkins 需要一个代理字段,我设置为 'any'。但是好像没有可用的标签参数?
作为替代方案,我尝试使用 'node' 命令(https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#-node- 分配节点)。但这给我留下了两个 运行ning 工作,虽然工作,但看起来并不那么漂亮。
有谁知道NodeLabel参数插件可以用吗?或者也许有人有更简洁的方法?
编辑:可能我没说清楚。我需要能够 运行 不同节点上的作业。 运行所在的节点应该在触发作业时通过参数来决定。节点标签插件完美地做到了这一点。但是,我无法在管道中重现此行为。
假设您在管道上使用 NodeLabel 插件添加了参数(例如命名为 slaveName
)。您现在需要提取 slaveName
的值并将其输入 agent->node->label 字段。
您可以使用代理中的节点属性指定节点。 像这样-
agent
{
node
{
label "${slaveName}"
}
}
这是一个完整的例子:
pipeline {
parameters {
choice(name: 'node', choices: [nodesByLabel('label')], description: 'The node to run on') //example 1: just listing all the nodes with label
choice(name: 'node2', choices: ['label'] + nodesByLabel('label'), description: 'The node to run on') //example 2: add the label itself as the first choice to make "Any of the nodes" the default choice
}
agent none
stages {
stage('Test') {
agent { label params.node}
stages {
stage('Print environment settings') {
steps {
echo "running on ${env.NODE_NAME}"
sh 'printenv | sort'
}
}
}
}
}
}
以下脚本对我有用 运行 在不同节点上并行执行多个作业。
我参考了构建步骤插件文档。
https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/
def build_one()
{
parallel one: {
stage('XYZ') {
catchError(buildResult: 'SUCCESS', stageResult:'FAILURE') {
build job: 'yourDownStreamJob', parameters: [[$class: 'NodeParameterValue', name: 'NodeToRun',labels: ['nodeName'], nodeEligibility: [$class: 'AllNodeEligibility']], string(name: 'ParentBuildName', value: "XX"), string(name: 'Browser', value: 'chrome'), string(name: 'Environment', value: 'envName')]
}
}
},
two : {
stage('SecondArea') {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
build job: 'yourDownStreamJob', parameters: [[$class: 'NodeParameterValue', name: 'NodeToRun',labels: ['Your'], nodeEligibility: [$class: 'AllNodeEligibility']], string(name: 'ParentBuildName', value: "XYX"), string(name: 'Browser', value: 'firefox'), string(name: 'Environment', value: 'envName')]
}
}
}
}
build_one()