Jenkins pipline系列-04-Jenkinsfile01

上一篇 / 下一篇  2017-09-20 08:45:11 / 个人分类:jenkins

版权声明:本文为博主原创文章,未经博主允许不得转载

怎么创建Jenkins Pipeline 查看官网非常的清晰
今天记录一下Jenkinsfile用例的讲解. 

Jenkinsfile用例讲解
Jenkinsfile其实就是一个流程的描述文件。用Groovy 语言来将整个流程代码化,可读性也很强。在之前的章节介绍过jenkinsfile 的一个用例。 里面包含了三个stage, 并不是所有的Pipelines都含有三个stages.接下来的小结介绍在test安装阶段创建和执行一个简单的Pipeline。
使用一个text编译器, 最好是支持Groovy 语法高亮的,创建一个新的Jenkinsfile在项目的根目录下面。
我们通过下面这个例子来熟悉Jenkinsfile:
一下的用例就是描述了整个流程, 先在任意agent或者说node上面Build, 然后开始linux node 上运行,然后在Windows agent 运行。

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent none
    stages {
        stage('Build') {/* 描述了当前的状态 */
agent any/* 运行的agent ,表示可以在任何node 节点上运行*/
steps { /* 步骤*/
                checkout scm
                sh'make'stashincludes:'**/target/*.jar',name:'app'}
        }
        stage('Test on Linux') {
            agent {label'linux'}
            steps {
                unstash'app'sh'make check'}
            post {
                always {
                    junit'**/target/*.xml'}
            }
        }
        stage('Test on Windows') {
            agent {
                label'windows'}
            steps {
                unstash'app'bat'make check'}
            post {
                always {
                    junit'**/target/*.xml'}
            }
        }
    }
}
如果说要并行执行的话 就要添加parallel 步骤, 看如下的例子
Jenkinsfile (Scripted Pipeline)
stage('Build') {/* .. snip .. */}

stage('Test') {
    parallellinux: {
        node('linux') {
            checkout scmtry{
                unstash'app'sh'make check'}finally{
                junit'**/target/*.xml'}
        }
    },windows: {
        node('windows') {/* .. snip .. */}
    }
}

Pipeline 的高阶语法

字符串插入和定义

Jenkins Pipeline uses rules identical to Groovy for string interpolatio
Jenkins Pipeline使用Groovy的语法来定义字符串。
defsinglyQuoted ='Hello'defdoublyQuoted ="World"
定义完了之后们,再次使用就要用$并且用双“来调用这个变量
defusername ='Jenkins'echo'Hello Mr. ${username}'echo"I said, Hello Mr.${username}"
上面的结果就会是
Hello Mr. ${username}
I said, Hello Mr. Jenkins
以下就是将环境变量接入到Jenkinsfile里面的使用方法
pipeline {
    agent any
    parameters {
        string(name:'Greeting',defaultValue:'Hello',description:'How should I greet the world?')
    }
    stages {
        stage('Example') {
            steps {
                echo"${params.Greeting}World!"}
        }
    }
}

环境变量的设置

Jenkins Pipeline通过全局变量env来使用环境变量。这个envJenkinsfile里面全局可见。如果你的Jenkins的地址是localhost:8080, 那么你可以通过localhost:8080/pipeline-syntax/globals#en来获取所有的变量。这些变量包括:

BUILD_ID

当前编译的ID.

JOB_NAME

当前这个Job的名字

JENKINS_URL

Jenkins 的全局URL, 比如说 example.com:port/jenkins/ (NOTE: 只有你URL在 "System Configuration"设置才可见)

以下就是使用环境变量的Jenkinsfile的例子

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo"Running${env.BUILD_ID}on${env.JENKINS_URL}"}
        }
    }
}
错误处理
通过 post sectionPipeline可以支持非常鲁棒的错误信息,它允许声明一系列 "post conditions",比如说: alwaysunstablesuccessfailure, and changed
以下的用例里面定义了出现错误的时候发送邮件等

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh'make check'}
        }
    }
    post {
        always {
            junit'**/target/*.xml'}
        failure {
            mailto: team@example.com,subject:'The Pipeline failed :('}
    }
}

TAG:

 

评分:0

我来说两句

日历

« 2024-04-19  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 5729
  • 日志数: 10
  • 建立时间: 2017-08-29
  • 更新时间: 2017-10-09

RSS订阅

Open Toolbar