自动化测试架构

Jenkins 2.0

2017-05-18  本文已影响855人  三无架构师

[TOC]

Jenkins 2.0

支持Blue Ocean界面,如下:

image.png

2.0的三个特点:

Pipeline as Code

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

基本概念:

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里面可以运行以下信息:

脚本命令的执行:

超时、重试机制:

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来访问,其中有:

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?"这种让人工进行确认。

image.png

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

Blue Ocean创建项目

image.png image.png image.png

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

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!"
            }
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读