如何在 Jenkins 声明式管道中创建方法? [英] How to create methods in Jenkins Declarative pipeline?

查看:30
本文介绍了如何在 Jenkins 声明式管道中创建方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Jenkins 脚本管道中,我们能够创建方法并调用它们.

In Jenkins scripted pipeline we are able to create methods and can call them.

是否也可以在 Jenkins 声明式管道中使用?以及如何?

Is it possible also in the Jenkins declarative pipeline? And how?

推荐答案

较新版本的声明式管道支持这一点,而这在之前(~2017 年中期)是不可能的.您可以像从 groovy 脚本中期望的那样声明函数:

Newer versions of the declarative pipelines support this, while this was not possible before (~mid 2017). You can just declare functions as you'd expect it from a groovy script:

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                whateverFunction()
            }
        }
    }
}

void whateverFunction() {
    sh 'ls /'
}

这篇关于如何在 Jenkins 声明式管道中创建方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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