如何在通过参数和标签选项选择的节点上运行jenkins声明式管道 [英] how to run jenkins declarative pipeline on node selected via parameter and label option

查看:286
本文介绍了如何在通过参数和标签选项选择的节点上运行jenkins声明式管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Node and Label插件运行将节点设置为参数的管道.

I want to run a pipeline with the node set as parameter via the Node and Label plugin.

如何更改声明性管道

pipeline {
    agent  {
        label 'whatever'
    }
...

使用EXECUTION_NODE作为代理执行管道?这似乎比我想象的要复杂得多,或者我缺少明显的东西.

to use EXECUTION_NODE as agent to execute the pipeline? This seems to be much more complicated than I thought, or I am missing something obvious.

推荐答案

问题是这样的:向您展示使用参数构建"页面,Jenkins需要运行您的管道并解析其参数.要运行管道,Jenkins需要一个节点.要拥有一个节点,它将解析您的管道.因此,在显示对话框时,该节点已被选中.而且,在声明式流水线中,所有阶段的所有节点都在开始时被选择.

The issue is this: to present you the "Build with parameters" page, Jenkins needs to run your pipeline and parse its parameters. To run a pipeline, Jenkins needs a node. To have a node, it parses your pipeline. So the node is already selected by the time the dialog is shown. Moreover, in declarative pipeline all the nodes of all the stages get selected in the beginning.

您可以通过运行node并提供params.EXECUTION_NODE作为标签来尝试运行脚本管道或脚本与声明性脚本的组合.脚本化管道逐行执行脚本.

You can try running a scripted pipeline or a combination of scripted and declarative, by running node and supplying params.EXECUTION_NODE as label. Scripted pipeline executes the script line by line.

这有效:

NODE = null
echo "This should be Null: $NODE"

node() {
    stage("Define node") {
        NODE = params.NODE
        echo "This is now $NODE"
    }
}
    
pipeline {
    agent { node { label "$NODE" }}
    parameters { string(name: 'NODE', defaultValue: 'some_node', description: '') }
    stages {
        stage("Main") {
            steps {
                echo "Hi"
            }
        }
    }
}

以下是第二次运行的输出,其中以"master"作为参数:

Here is an output of a second run with 'master' as parameter:

Started by user marat 
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] echo
This should be Null: null
[Pipeline] node
Running on Jenkins in /home/jenkins/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Define node)
[Pipeline] echo
This is now master
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] node
Running on master in /var/jenkins_home/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Main)
[Pipeline] echo
Hi
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

这篇关于如何在通过参数和标签选项选择的节点上运行jenkins声明式管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆