Jenkins 2.0

[TOC]

Jenkins 2.0

支持Blue Ocean界面,如下:

《Jenkins 2.0》 image.png

2.0的三个特点:

  • Pipeline as Code:
  • 全新的开箱体验:即变好看了。
  • 1.x兼容性

Pipeline as Code

其帮助Jenkins从CI到CD转变。其是一套运行在Jenkins上的工作流框架,把原本独立运行于
单个或多个节点的任务连接起来,实现复杂的发布流程。Pipeline的实现方式是一套Groovy DSL(类似Gradle),任何发布流程都可以表述为一段Groovy脚本,并且Jenkins支持从代码库直接读取脚本,从而实现了Pipeline as Code的理念。

基本概念:

  • Step:最基本的操作单元,小到创建一个目录,大到构建一个Docker镜像,由各类Jenkins Plugin提供。
  • Node:一个Node就是一个jenkins节点,可以是MasterAgent。是执行Step的具体运行期环境。
  • Stage:一个Pipeline可以划分为若干个Stage,每个Stage代表一组操作。Stage是个逻辑分组,可以跨多个Node

Jenkins 2默认支持3种类型的Pipeline:普通PipelineMultibranch PipelineOrganization Folders,后两种其实是批量创建一组普通Pipeline的快捷方式,分别对应于多分支的应用和多应用的大型组织

Jenkinsfile

其是一个文本文件,名称为:Jenkinsfile,放在根目录下即可。

在其中定义了Jenkins Pipeline。如下为一个定义了三个stage的pipeline

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

在Steps里面可以运行以下信息:

脚本命令的执行:

  • unin/linux: 使用sh,如:sh 'mvn -version'
  • windows:使用bat

超时、重试机制:

pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                retry(3) {
                    sh './flakey-deploy.sh'
                }

                timeout(time: 3, unit: 'MINUTES') {
                    sh './health-check.sh'
                }
            }
        }
        stage('Deploy2') {
            steps {
                timeout(time: 3, unit: 'MINUTES') {
                    retry(5) {
                        sh './flakey-deploy.sh'
                    }
                }
            }
        }
    }
}

Finishing up:使用post来完成一些资源的清理工作。其和stages平级:

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh 'echo "Fail!"; exit 1'
            }
        }
    }
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}

定义执行环境

通过agent来定义pipeline的执行环境,在每个Pipeline,agent是必需存在的。

pipeline {
    agent {
        docker { image 'node:7-alpine' }
    }
    stages {
        stage('Test') {
            steps {
                sh 'node --version'
            }
        }
    }
}

在最外面把agent none后,可以为每个stages设置相应的agent

环境变量

可以定义为全局的,也可以为Stage来定义。

pipeline {
    agent any
    environment { 
        CC = 'clang'
    }
    stages {
        stage('Example') {
            environment { 
                DEBUG_FLAGS = '-g'
            }
            steps {
                sh 'printenv'
            }
        }
    }
}

已定义的环境变量,可以通过env来访问,其中有:

  • BUILD_ID: 当前build id.
  • JOB_NAME:项目名,如:boot或者boot/dev,其中dev为分支名
  • JENKINS_URL
steps {
    echo 'Deploying....'
    echo "当前BuildId: ${env.BUILD_ID}"
    echo "当前Job: ${env.JOB_NAME}"
    echo "当前URL: ${env.JENKINS_URL}"
}

参数:可以通过params来访问编译时的参数。如:

pipeline {
    agent any
    parameters {
        string(name: 'Greeting', defaultValue: 'Hello', description: 'How should I greet the world?')
    }
    stages {
        stage('Example') {
            steps {
                echo "${params.Greeting} World!"
            }
        }
    }
}

清理并通知

清理功能见上面,使用post来定义。

email通知:

post {
    failure {
        mail to: 'team@example.com',
             subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
             body: "Something is wrong with ${env.BUILD_URL}"
    }
}

steps里面可以通过input "Does the staging environment look ok?"这种让人工进行确认。

《Jenkins 2.0》 image.png

其具体参数用法-参考文档](https://jenkins.io/doc/book/pipeline/syntax/))

Blue Ocean创建项目

《Jenkins 2.0》 image.png
《Jenkins 2.0》 image.png
《Jenkins 2.0》 image.png

则这时创建成功后,会自动扫描Jenkinsfile
如果需要变更后自动触发,则可以配置其Scan Multibranch Pipeline Triggers

《Jenkins 2.0》 image.png

Jenkinsfile样例文件

pipeline {
    agent any

    tools {
        maven 'Default'
    }

    parameters {
        string(name: 'Greeting', defaultValue: 'Hello', description: 'How should I greet the world?')
    }

    stages {
        stage('Build') {
            steps {
                echo 'Building..'
                sh 'java -version'
                sh 'mvn -version'
                sh 'mvn clean'
                echo 'build over...'
            }
        }
        stage('Test') {
            steps {
                input "Does the staging environment look ok?"
                echo 'Testing..'
            }
        }
        stage('hello') {
            steps {
                echo 'hello world'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
                echo "当前BuildId: ${env.BUILD_ID}"
                echo "当前Job: ${env.JOB_NAME}"
                echo "当前URL: ${env.JENKINS_URL}"
                echo "${params.Greeting} World!"
            }
        }
    }
}
    原文作者:三无程序员
    原文地址: https://www.jianshu.com/p/6d5dc1da5d47
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞