1.pipeline{} 声明式流水线的定义 顶层

 

2.agent{} 流水线运行节点

any:任意节点

label:根据节点标签选择

none:当pipeline全局指定agent为none,则根据每个stage中定义的agent运行(stage必须指定)

node:  和label类似,可以添加些其他配置

 

3.stages{} 定义pipeline阶段

关系:stages>stage>steps>script

举例:

复制代码
pipeline {
    agent any
    stages {
        stage('build') {
            steps {
                echo 'Hello World'
                script {
                    sh 'echo "nihao"'
            }
            }
        }
        stage('test'){
            steps{
                echo "test"
            }
        }
        stage("deploy"){
            steps{
                echo "deploy"
            }
        }

    }

}
View Code
复制代码

 

4.post{} 根据流水线最终状态做相应的操作

 

常见:

always: 不管什么状态总是执行
success: 仅流水线成功后执行
failure: 仅流水线失败后执行
aborted: 仅流水线被取消后执行
unstable:不稳定状态,单侧失败

 举例:

复制代码
pipeline {
    // 选择运行节点
    //agent none

    agent {
        label 'master'
    }

    stages {
        stage('build') {
            // stage level agent
            agent { 
                label 'linux'
            }
            steps {
                echo 'build'
            }
        }

        stage('test') {
            steps{
                echo "test"
            }
        }

        stage('deploy'){
            steps{
                script{
                    // environment self
                    println("job name: ${JOB_NAME}")
                }
                echo "deploy"
            }
        }

    }

    post {
        always{
          echo "always"
      }
        success {
          echo "all stage success"
      }
        failure {
          echo "stage failure"
      }
    }

}
View Code
复制代码

 

5.environment{} 环境变量

流水线级别:pipeline{environment{}}

阶段级别:stage{environment{}}

 

6.options{} 运行时的一些选项,以代码方式定义配置(需要构建一次,才能看到效果)

常用:

//设置保存最近的记录
options { buildDiscarder(logRotator(numToKeepStr: '1')) }

//禁止并行构建
options { disableConcurrentBuilds() }

//跳过默认的代码检出
options { skipDefaultCheckout() }

//设定流水线的超时时间(可用于阶段级别)
options { timeout(time: 1, unit: 'HOURS') }

//设定流水线的重试次数(可用于阶段级别)
options { retry(3) }

//设置日志时间输出(可用于阶段级别)
options { timestamps() }

注:也可以在流水线创建界面选择操作

 

 举例:

复制代码
pipeline {
    agent {
      label 'master'
    }


    //运行选项,pipeline级别
    options {
      disableConcurrentBuilds()  // 禁止并发构建
      buildDiscarder logRotator(artifactDaysToKeepStr: '', 
                                artifactNumToKeepStr: '', 
                                daysToKeepStr: '5', 
                                numToKeepStr: '8')  //历史构建
    }

    stages {
        stage('build') {
            options {  //stage级别
                timeout(time: 5, unit: 'MINUTES')
                retry(3)
            }
            steps {
                echo 'build'
            }
        }
        stage('deploy'){
            steps{
                script{
                    echo "deploy"
                }

            }
        }

    }

}
View Code
复制代码

 

7.parameters{} 代码的方式定义构建参数(需要构建一次,才能看到效果)

流水线在运行时设置的参数,UI页面的参数。所有的参数都存储在params对象中

举例:

复制代码
pipeline {
    agent {
      label 'master'
    }
    // 全局变量
    environment {
      VERSION = "1.0.0"
    }
    // 构建参数
    parameters {
      string defaultValue: 'ysh', description: 'this is name info', name: 'NAME'
      choice choices: ['dev', 'test', 'uat'], description: 'env names', name: 'ENVNAME'
    }


    stages {
        stage('build') {
            // stage level agent
            agent { 
               label 'linux'
            }

            // stage level env local
            environment {
              VERSION = "1.100.100"
            }
            steps {
                echo 'build'

                // 全局和局部变量
                echo "${VERSION}"

                // 打印构建参数值
                echo "姓名:${params.NAME}"
                echo "部署环境: ${params.ENVNAME}"
            }
        }

    }

}
View Code
复制代码

构建一次后,就可以选择了

 8.trigger 触发器

cron 定时触发 : triggers { cron 'H/15 * * * *' }

 

 

9.input{} 

message: 提示信息
ok: 表单中确认按钮的文本
submitter: 提交人,默认所有人可以
parameters: 交互时用户选择的参数

10.when{}

常用:根据环境变量判断、表达式判断、条件判断(not/allOf/anyOf)

11.paraller{} 并行发布

 

举例:

 

复制代码
pipeline {
    agent {
      label 'master'
    }

    // 构建触发器
    triggers {
      cron 'H * * * * '
    }

    stages {
        stage('build') {
            // stage level agent
            agent { 
               label 'linux'
            }

            
            steps {
                echo 'build'

            }
        }

        stage('test') {
            input {
              message '请确认'
              ok 'ok'
              submitterParameter 'approve_user'
              parameters {
                choice choices: ['deploy', 'rollback'], name: 'ops'
              }
            }

            steps{
                echo "test"
                echo "执行的动作: ${ops}"
                echo "批准用户: ${approve_user}"
                script {
                    // 由于下个stage无法获取ops的值,所以特此定义一个新的全局变量
                    // env. 定义全局变量
                    env.OPS = "${ops}"
                }
            }
        }

        stage('deploy'){
            // 是否运行
            when {
              environment name: 'OPS', value: 'deploy'
            }

            steps{
                echo "deploy"
            }
        }

        stage("parallelstage"){
            failFast true
            parallel {
                stage("build01"){
                    steps {
                        echo "windows"
                    }
                }

                stage("build02"){
                    steps{
                        echo "linux"
                    }
                }
            }
        }
    }
}
View Code
复制代码