Two ways to write Jenkins pipeline jenkinsfile, declarative and script

Two writing methods of Jenkins pipeline jenkinsfile, declarative and scripted.

Why do you need a pipeline?

In the era of Jenkins 1.x, where Jenkins became the most popular continuous integration server many years ago, all new features were enhanced by installing plugins, and all configuration was achieved through a web interface.

As Jenkins entered the era of 2.x, in order to follow the pace of the era (everything is code), Jenkins introduced the concept of configuration and code. The code is used to express the process for the convenience of version control and automation, making the configuration reproducible as the source code, and easier to support multi-branch development and deployment. Specifically, Jenkins introduced the concept of pipeline, you can use groovy to write Jenkinsfile.

Why are there two ways to implement pipeline?

It ’s really not a big deal to watch a movie. Two implementations of the same function, many users will be fooled. Let's take a look at this development process. Jenkins is implemented in Java, so groovy was introduced as a DSL very early, and administrators can use groovy to implement some automation and advanced management functions. Because the groovy engine has been integrated into Jenkins, it is natural to use groovy to write the Jenkinsfile at the beginning of the pipeline.

But groovy is a language after all, and the learning curve for Xiaobai, who does not have much programming experience, is a little too steep. At this time, the declarative pipeline appeared, mainly to reduce the difficulty of getting started.

How to choose as a Jenkins user?

  • The declarative pipeline encourages a declarative programming model and is more suitable for beginners without programming experience.
  • The scripted pipeline is based on groovy's DSL language and provides Jenkins users with a lot of flexibility and scalability.
  • Declarative pipelines will receive more and more official support. You can use the Visual Pipeline Editor to edit and verify declarative pipelines.
  • If it is really a function that is not supported by declarative syntax, you can also call the script directly in declarative syntax.

So it is recommended that novices start with declarative pipelines.

Declarative pipeline:  https://jenkins.io/doc/book/pipeline/syntax/#declarative-pipeline
scripted pipeline:  https://jenkins.io/doc/book/pipeline/syntax/#scripted-pipeline

Let's take an example to take a preliminary look, and see which one you prefer?

Declarative pipeline

pipeline {
    agent any
    stages {
        stage('Example Build') {
            steps {
                echo 'Hello World'
            }
        }
        stage('Example Deploy') {
            when {
                branch 'production'
                environment name: 'DEPLOY_TO', value: 'production'
            }
            steps {
                echo 'Deploying'
            }
        }
    }
}

Screenplay pipeline

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

Call scripted syntax in declarative pipeline

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

reference:

https://stackoverflow.com/questions/43484979/jenkins-scripted-pipeline-or-declarative-pipeline
https://medium.com/@brianrthompson/scripted-vs-declarative-pipelines-which-to-choose-c6af403f1e97

Guess you like

Origin www.cnblogs.com/itech/p/12737284.html