我如何在詹金斯管道中使用`def`

罗恩

我正在学习jenkins管道,并且尝试遵循此管道代码但是我的詹金斯总是抱怨那def是不合法的。我想知道我是否错过任何插件?我已经安装了groovyjob-dsl但是无法正常工作。

罗恩

正如@Rob所说,有两种类型的管道:scripteddeclarative就像imperativevs一样declarativedef仅允许在scripted管道中或包装在中script {}

脚本管道(命令式)

开始node,并且def还是if被允许的,像下面。这是传统方式。 node { stage('Example') { if (env.BRANCH_NAME == 'master') { echo 'I only execute on the master branch' } else { echo 'I execute elsewhere' } } }

声明性管道(首选)

以开头pipeline,并且defif不允许,除非用包裹script {...}声明性管道使很多事情易于编写和阅读。

时间触发

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"
                    }
                }
            }
        }
    }
}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章