如何在 Jenkins Pipeline 中使用“def"? [英] How can I use `def` in Jenkins Pipeline?

查看:538
本文介绍了如何在 Jenkins Pipeline 中使用“def"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Jenkins 流水线,并尝试遵循这条流水线 代码.但是我的 Jenkins 总是抱怨 def 不合法.

I am learning Jenkins Pipeline, and I tried to follow this Pipeline code. But my Jenkins always complains that def is not legal.

我想知道我是否错过了任何插件?我已经安装了groovyjob-dsl,但是还是不行.

I am wondering did I miss any plugins? I already installed groovy, job-dsl, but it doesn't work.

推荐答案

正如@Rob 所说,管道有两种类型:scripteddeclarative.这就像 imperativedeclarative.def 仅允许在 scripted 管道中或包装在 script {} 中.

As @Rob said, There are 2 types of pipelines: scripted and declarative. It is like imperative vs declarative. def is only allowed in scripted pipeline or wrapped in script {}.

node 开始,并且允许 defif,如下所示.这是传统的方式.

Start with node, and def or if is allowed, like below. It is traditional way.

node {
    stage('Example') {
        if (env.BRANCH_NAME == 'master') {
            echo 'I only execute on the master branch'
        } else {
            echo 'I execute elsewhere'
        }
    }
}

声明式管道(首选)

pipeline 开头,并且不允许 defif,除非它被包裹在 script {...}.声明式管道让很多事情都易于编写和阅读.

Declarative pipeline (Preferred)

Start with pipeline, and def or if is NOT allowed, unless it is wrapped in script {...}. Declarative pipeline make a lot things easy to write and read.

pipeline {
    agent any
    triggers {
        cron('H 4/* 0 0 1-5')
    }
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

什么时候

pipeline {
    agent any
    stages {
        stage('Example Build') {
            steps {
                echo 'Hello World'
            }
        }
        stage('Example Deploy') {
            when {
                branch 'production'
            }
            steps {
                echo 'Deploying'
            }
        }
    }
}

平行

pipeline {
    agent any
    stages {
        stage('Non-Parallel Stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
        stage('Parallel Stage') {
            when {
                branch 'master'
            }
            failFast true
            parallel {
                stage('Branch A') {
                    agent {
                        label "for-branch-a"
                    }
                    steps {
                        echo "On Branch A"
                    }
                }
                stage('Branch B') {
                    agent {
                        label "for-branch-b"
                    }
                    steps {
                        echo "On Branch B"
                    }
                }
            }
        }
    }
}

嵌入脚本代码

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

                script {
                    def browsers = ['chrome', 'firefox']
                    for (int i = 0; i < browsers.size(); ++i) {
                        echo "Testing the ${browsers[i]} browser"
                    }
                }
            }
        }
    }
}

要阅读更多声明性管道语法,请参考官方文档这里

To read more declarative pipeline grammar, please refer the official doc here

这篇关于如何在 Jenkins Pipeline 中使用“def"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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