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

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

问题描述

我想通过节点和标签插件运行将节点设置为参数的管道.

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天全站免登陆