Jenkins pipeline模板

366 阅读1分钟
#!groovy

@Library('jenkinslib@master') _  // 加载共享库

String  workspace = "/opt/jenkins/workspace"

// Pipeline

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

  // 指定运行的选项(可以省略)
  options {
    timestamps()  // 日志会有时间
    skipDefaultCheckout()  // 删除隐式checkout scm语句
    disableConcurrentBuilds() // 禁止并行
    timeout(time: 1, unit: "HOURS")  // 流水线超时设置1h
  }

  // 指定stages阶段(一个或多个)
  stages {
    // 下载代码
    stage("GetCode") {  // 阶段名称
      steps{
        timeout(time: 5, unit: "MINUTES") {  // 步骤超时时间
          script{  // 填写运行代码
            println('获取代码')  
          }
        }
      }
    }
    // 构建
    stage("Build") {
      steps {
        timeout(time: 20, unit: "MINUTES") {
          script {
            print('应用打包')
          }
        }
      }
    }

    // 代码扫描
    stage("CodeScan") {
      steps {
        timeout(time: 30, unit: "MINUTES") {
          script {
            print("代码扫描")
          }
        }
      }
    }
  }

  // 构建后操作
  // always{} 总是执行的脚本片段
  // success{} 成功后执行
  // failure{} 失败后执行
  // aborted{} 取消后执行
  // currentBuild 是一个全局变量,description 为构建描述
  post {
    always {
      script {
        print("always")
      }
    }
    success {
      script {
        currentBuild.description += "\n 构建成功"
      }
    }
    failure {
      script {
        currentBuild.description += "\n 构建失败"
      }
    }
    aborted {
      script {
        currentBuild.description += "\n 构建取消"
      }
    }
  }
}