pipeline

一条流水线通过jenkinsfile描述

安装声明式插件Pipeline: Declarative

Jenkinsfile组成:

  指定node节点/workspace

  指定运行选项

  指定stages阶段

  指定构建后操作

Pipeline定义 - agent/options

agent { node { label "master"  //指定运行节点的标签或者名称
               customWorkspace "${workspace}"  //指定运行工作目录(可选)
        }
    
}


options {
    timestamps() //日志会有时间
    skipDefaultCheckout() //删除隐式 checkout scm语句
    disableConcurrentBuilds() //禁止并行
    timeout(time: 1, unit: 'HOURS')  //流水线超时设置1h,如果不设置超时时间,会消耗资源, 占用构建队列,导致构建队列出现堵塞
}

  

指定stages阶段(一个或多个)
解释:在这里我添加了三个阶段
GetCode
Build
CodeScan

stages {
    //下载代码
    stage("GetCode"){ //阶段名称
        steps{ //步骤
            timeout(time:5, unit:"MINUTES"){
                script{ //填写运行代码
                    println("获取代码")
                }
            }
        }
    }
    
    //构建
    stage("Build"){
        steps{
            timeout(time:20,unit:"MINUTES"){
                script{
                    println("应用打包")
                }
            }
        }
    }
    
    stage("CodeScan"){
        steps{
            timeout(time:30,unit:"MINUTES"){
                script{
                    println("代码扫描")
                }
            }
        }
    }
}

  

指定构建后操作

解释:
always{}: 总是执行脚本片段
success{}: 成功后执行
failure{}: 失败后执行
aborted{}:取消后执行


currentBuild 是一个全局变量
description: 构建描述

//构建后操作

post{
    always{
        script{
            println("always")
        }
    }
    
    success{
        script{
            currentBuild.description += "
 构建成功"
        }
    }
    
    failure{
        script{
            currentBuild.description += "
 构建失败"
        }
    }
    
    aborted{
        script{
            currentBuild.description += "
 构建取消"
        }
    }
}

  

原文地址:https://www.cnblogs.com/liulilitoday/p/14379523.html