Jenkinfile入门:Pipeline as code

Jenkinfile入门:Pipeline as code

Xiaobian DevOps perspective

What is pipeline


This is how Pipeline is introduced in "Continuous Delivery". Pipeline refers to the automated manifestation of the process from the software version control library to the user's hands.

In the official documentation of Jenkins, the pipeline is a user-defined CD pipeline model. The pipeline code defines the entire build process, which usually includes the stages of building, testing, and delivering the application.

Jenkinsfile


Jenkinsfile is a text file, just like Dockerfile for Docker.
Jenkins does not support Jenkinfiles by default, and you need to install the Pipeline plug-in to support it. It is because of Jenkinsfile that Jenkins can realize "Pipeline as code".

pipeline syntax

Language


The Goovy language is chosen as the basis to implement pipelines, so when writing scripted pipelines, you are writing Groovy code. There are two types of grammar: scripted grammar and declarative grammar. The pipeline plug-in only supports two syntaxes since version 2.5.

grammar


Scripted grammar

The scripted syntax is flexible and extensible, but it also means more complex.


node {
    stage('Build') {
      //执行构建
    }

    stage('Test') {
      //执行测试
    }

    stage('Deploy') {
        try{
            //执行部署
        }catch(err){
            currentBuild.result = "FAILURE"
            main body "project build error is here:${env.BUILD_URL}"
            from: '[email protected]'
            replyto: '[email protected]'
            subject: 'project build failed'
            to: '[email protected]'
            throw err
        }
    }
}

Declarative grammar

Because the scripted grammar is more complex, there is a declarative grammar, which provides a simpler and more structured grammar.


pipeline {
    agent any
    stages {
        stage('Build'){
            steps {
                echo "building"
            }
        }
        stage('Test'){
            steps {
                echo "testing"
            }
        }
        stage('Deploy'){
            steps {
                echo "deploying"
            }
        }
        post {
            failure {
                mailto: "[email protected]", subject: 'The pipeline  failed'
            }
        }
    }
}

Create the first pipeline

Install the pipeline plugin

[System Management]->[Plugin Management]->[Optional Plugin] Search for Pipeline

Jenkinfile入门:Pipeline as code

Create a pipeline task

Jenkinfile入门:Pipeline as code

Jenkinfile入门:Pipeline as code

Build

Jenkinfile入门:Pipeline as code
Jenkinfile入门:Pipeline as code
Jenkinfile入门:Pipeline as code

Pull the pipeline from version control


Here is to use github to get the Jenkinsfile of the project from github to build.
Need to install the Git plugin plugin.

Configure credentials (credentials)


Jenkinfile入门:Pipeline as code

Jenkinfile入门:Pipeline as code
Jenkinfile入门:Pipeline as code

Configure pipeline


Jenkinfile入门:Pipeline as code

Build

Jenkinfile入门:Pipeline as code
Jenkinfile入门:Pipeline as code

Jenkinfile入门:Pipeline as code

Conclusion


I will not introduce the use of Maven to build Pipeline here. When building with Maven, there are two methods. One is to use Jenkins to install Maven and build, the other is to manually install Maven to build

Guess you like

Origin blog.51cto.com/15127511/2657848