我如何在詹金斯管道中使用`def` [英] How can I use `def` in jenkins pipeline

查看:80
本文介绍了我如何在詹金斯管道中使用`def`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习jenkins管道,并且尝试遵循此

I am learning jenkins pipeline, and I tried to follow this pipeline code. But my jenkins always complain that def is not legal. I am wondering did I miss any plugins? I already installed groovy, job-dsl, but doesn't work.

推荐答案

正如@Rob所说,有两种类型的管道:scripteddeclarative.就像imperative vs declarative.仅在scripted管道中允许使用def或将其包装在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

这篇关于我如何在詹金斯管道中使用`def`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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