Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use `def` in Jenkins Pipeline?

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

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

like image 776
Ron Avatar asked Nov 10 '17 06:11

Ron


People also ask

What does def mean in Jenkinsfile?

Posted on June 18, 2019 by admin. Variables in a Jenkinsfile can be defined by using the def keyword. Such variables should be defined before the pipeline block starts. When variable is defined, it can be called from the Jenkins declarative pipeline using ${...} syntax.

How are variables defined in Jenkins pipeline?

Environment variables can be defined using NAME = VALUE syntax. To access the variable value you can use these three methods $env.NAME , $NAME or ${NAME} There are no differences between these methods.


1 Answers

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 {}.

Scripted pipeline (Imperative)

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

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.

time trigger

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

when

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

Parallel

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

embedded with scripted code

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

like image 127
Ron Avatar answered Oct 09 '22 17:10

Ron