我该如何在Jenkins Pipeline中使用`def`?

24

我正在学习Jenkins Pipeline,并尝试按照这个Pipeline code进行操作。但是我的Jenkins一直抱怨def不合法。

我想知道是否缺少任何插件?我已经安装了groovyjob-dsl,但还是不起作用。


2
有两种类型的流水线: 脚本和声明式。某些内容不能在声明式流水线的一部分中使用。了解两者之间的区别,你就会知道 def 在哪里是有效的。 - Rob Hales
2
感谢您的提示,它帮助我找到了答案。在脚本化流水线中使用def,该流水线以node {...}开头;而我的文件以声明式的pipeline开头,除非被script {...}包装,否则不允许使用def - Ron
3个回答

29

正如@Rob所说,有两种类型的pipeline:scripteddeclarative。这就像imperative vs declarative。在scripted pipeline中只允许使用def或包装在script {}中。

Scripted pipeline (Imperative)

node开始,可以使用defif,例如下面的示例。这是传统的方法。

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

要阅读更多关于声明式流水线语法的内容,请参考官方文档这里


请参考我上面的示例,作为在声明性流水线上使用它的另一种替代方式。 - krad

9
您可以在声明式流水线中使用 def,但不能在其中定义它,例如:
def agentLabel
if (BRANCH_NAME =~ /^(staging|master)$/)  {
    agentLabel = "prod"
} else {
    agentLabel = "master"
}

pipeline {
  agent { node { label agentLabel } } 
..

-2
您可以通过以下方式使用node来使用def

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

另一种方法:使用script{..}
    stage ('jon'){
steps{
script{                                                                                              
    def imageLine = 'chiducaff/user_containers:sonnginx'
}
    writeFile file: 'anchore_images', text: imageLine
    anchore name: 'anchore_images'
                           }}

后一种形式不起作用。看来你必须在script块内使用变量,否则会出现groovy.lang.MissingPropertyException的错误。 - okapi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接