虚拟化容器,大数据,DBA,中间件,监控。

Jenkins 流水线语法 06 options 运行时选项和 parameters参数化构建

21 12月
作者:admin|分类:系统运维

选项参数都是在字符界面配置的。

options {
  buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '5', numToKeepStr: '1')
}

前面三个是针对pipeline的,后面三个可以针对于每个阶段 

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

## 禁止并行构建,因为用的worksapce还是同一个
options { disableConcurrentBuilds() }


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


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


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


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

pipeline {
    agent any

    environment{
      VERSION = "1.1.1"
      ENV_TYPE = "DEV"
    }

    options{
       buildDiscarder(logRotator(numToKeepStr: '1'))
       disableConcurrentBuilds()
       skipDefaultCheckout()
       timeout(time: 1, unit: 'HOURS')
       retry(3)
       timestamps()
    }
    
    stages {
        stage('Hello') {
        
            environment{
                VERSION="1.0.0"
            }

            steps {
                script{
                    echo "The variable version is ${VERSION}"
                    echo "The variable env_type is ${env.ENV_TYPE}"
                    echo "The job name is ${env.JOB_NAME}" //内置的环境变量
                    env.JOB_NAMEA="mytest-pipeline"   //自定义的全局变量,也就是整个流水线可以去使用
                    echo "the variable env.JOB_NAMEA is ${env.JOB_NAMEA}"
                }
            }
   
        }
    }

}



Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on build-01 in /data/cicd/jenkinsagent/workspace/pipeline-test
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] timeout
Timeout set to expire in 1 hr 0 min
[Pipeline] {
[Pipeline] retry
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] echo
15:54:10  The variable version is 1.0.0
[Pipeline] echo
15:54:10  The variable env_type is DEV
[Pipeline] echo
15:54:10  The job name is pipeline-test
[Pipeline] echo
15:54:10  the variable env.JOB_NAMEA is mytest-pipeline
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // retry
[Pipeline] }
[Pipeline] // timeout
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

 Options这就是将图形化界面的配置信息代码化了

 

parameters 流水线参数(参数化构建)


  • 定义: 流水线在运行时设置的参数,UI页面的参数。所有的参数都存储在params对象中。
  • 将web ui页面中定义的参数,以代码的方式定义。

这些参数化构建是添加在UI界面生成的,那么可以在代码里面生成这些参数

parameters {
  string defaultValue: '1.1.1', description: '版本号', name: 'Version', trim: true
}

[Pipeline] echo (hide)
16:28:03  The variable version is 1.1.1


pipeline {
    agent any

    environment{
      VERSION = "1.1.1"
      ENV_TYPE = "DEV"
    }
    
    parameters {
  string defaultValue: '1.1.1', description: '版本号', name: 'Version', trim: true
}


    options{
       buildDiscarder(logRotator(numToKeepStr: '1'))
       disableConcurrentBuilds()
       skipDefaultCheckout()
       timeout(time: 1, unit: 'HOURS')
       retry(3)
       timestamps()
    }
    
    stages {
        stage('Hello') {
        
            environment{
                VERSION="1.0.0"
            }

            steps {
                script{
                    echo "The variable version is ${params.Version}"
                    echo "The variable env_type is ${env.ENV_TYPE}"
                    echo "The job name is ${env.JOB_NAME}" //内置的环境变量
                    env.JOB_NAMEA="mytest-pipeline"   //自定义的全局变量,也就是整个流水线可以去使用
                    echo "the variable env.JOB_NAMEA is ${env.JOB_NAMEA}"
                }
            }
   
        }
    }

}
pipeline {
    agent any
    
	parameters { 
        string(name: 'VERSION', defaultValue: '1.1.1', description: '') 
    }
    
    stages {
        stage("Build"){
            steps {
                echo "${params.VERSION}"
            }
        }
    }
}

 FAQ: 没有找到相关的环境变量, 这个是我们在parameters中引用了流水线中的变量导致的,可能因为加载顺序不同导致的,解决方法是可以在pipeline{} 外部定义变量进行引用。 

roovy.lang.MissingPropertyException: No such property: DEPLOY_DESC for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)

构建参数变量的信息也可以使用参数传入,可以使用String去定义变量写groovy代码(可以放在pipeline的外面,在流水线之前定义一些参数都可以使用String去定义,剩下的比如environment,paramters都是在pipeline运行时候定义的) 

String description = "版本号"

pipeline {
    agent any

    environment{
      VERSION = "1.1.1"
      ENV_TYPE = "DEV"
    }
    
    parameters {
  string defaultValue: '1.1.1', description: "${description}", name: 'Version', trim: true
}

浏览431 评论0
返回
目录
返回
首页
Jenkins pipeline 08 when 阶段运行控制 Jenkins 流水线语法 05 构建时变量 environment