詹金斯问题 - 将帖子部分添加到基于脚本的&方法之间的区别

发布于 2025-01-27 22:25:17 字数 493 浏览 2 评论 0原文

我有2个有关Jenkins

1的问题。两种方法之间有什么区别?

方法1

pipeline {
    agent any
    stages { 
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    } 

方法2,

node ("jenkins-nodes") {
    stage("git clone"){
        echo 'Hello World'    }
    }
  1. 我在第一个方法中了解到,我可以添加 post 部分,无论工作的结果如何。我希望为第二种方法添加相同的帖子部分,但它不起作用。有什么想法吗?

I have 2 questions regarding jenkins

1. What is the difference between the 2 methods?

Method 1

pipeline {
    agent any
    stages { 
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    } 

Method 2

node ("jenkins-nodes") {
    stage("git clone"){
        echo 'Hello World'    }
    }
  1. As I understand in the first method I can add Post section that will be running regardless of the result of the job. I wish to add the same post section for the second method, but it is not working. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱纠缠 2025-02-03 22:25:17

两种方法有什么区别?

AS noam helmer 写道,pipeline {} is 声明语法 andtax和and node {}} is scripteded 语法。

通常,我建议始终使用pipeline {},因为它使使用蓝色海洋插件更易于编写和可视化的通用任务在声明管道中最有效。

当声明管道变得过于不舒服时,您可以使用script {}在声明管道中插入脚本块:

pipeline {
    agent any
    stages { 
        stage('Example') {
            steps {
                script {
                    echo 'Hello World'               
                }
            }
        }
    }
}

清洁方法是定义一个函数,该函数是按定义脚本的,并将其用作自定义逐步宣布管道。请注意,即使没有脚本{}

pipeline {
    agent any
    stages { 
        stage('Example') {
            steps {
                myStep 42
            }
        }
    }
}

void myStep( def x ) {
    echo "Hello World $x"        // prints "Hello World 42"
}

在使用大量自定义代码的复杂管道中,我通常每个阶段具有一个功能。这样可以使管道{}清洁,并可以轻松查看管道的整体结构,而没有script {}整个地方混乱。

据我了解,在第一个方法中,我可以添加发布部分
无论工作结果如何,都可以运行。我想添加一样
发布第二种方法的部分,但它不起作用。有什么想法吗?

post {}仅在声明管道中可用,但不在声明管道的脚本管道或脚本段中。您可以使用尝试{} catch {}最后{}而不是。 捕获{}仅在发生错误时运行,最后{}总是运行。您可以同时使用catch {}最后{}

node ("jenkins-nodes") {
    stage("git clone"){
        echo 'Hello World'    
        try {
            // some steps that may fail
        }
        catch( Exception e ) {
            echo "An error happened: $e"
            // Rethrow exception, to let the build fail.
            // If you remove "throw", the error would be ignored by Jenkins.
            throw  
        }
        finally {
            echo "Cleaning up some stuff"
        }
    }
}

What is the difference between the 2 methods?

As Noam Helmer wrote, pipeline{} is declarative syntax and and node{} is scripted syntax.

In general I recommend to always use pipeline{} as it makes common tasks easier to write and visualization using Blue Ocean plugin works best with declarative pipeline.

When declarative pipeline becomes too unflexible, you can insert scripted blocks using the script{} step in a declarative pipeline:

pipeline {
    agent any
    stages { 
        stage('Example') {
            steps {
                script {
                    echo 'Hello World'               
                }
            }
        }
    }
}

A cleaner approach is to define a function, which is scripted by definition and use that as a custom step in declarative pipeline. Note this works even without script{}!

pipeline {
    agent any
    stages { 
        stage('Example') {
            steps {
                myStep 42
            }
        }
    }
}

void myStep( def x ) {
    echo "Hello World $x"        // prints "Hello World 42"
}

In complex pipelines that use lots of custom code, I usually have one function per stage. This keeps the pipeline{} clean and makes it easy to see the overall structure of the pipeline, without script{} clutter all over the place.

As I understand in the first method I can add Post section that will
be running regardless of the result of the job. I wish to add the same
post section for the second method, but it is not working. Any ideas?

post{} is only available in declarative pipeline, but not within a scripted pipeline or a scripted section of a declarative pipeline. You can use try{} catch{} finally{} instead. catch{} runs only when an error occurs, finally{} always runs. You can use both or either of catch{} and finally{}.

node ("jenkins-nodes") {
    stage("git clone"){
        echo 'Hello World'    
        try {
            // some steps that may fail
        }
        catch( Exception e ) {
            echo "An error happened: $e"
            // Rethrow exception, to let the build fail.
            // If you remove "throw", the error would be ignored by Jenkins.
            throw  
        }
        finally {
            echo "Cleaning up some stuff"
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文