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

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

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
}

 

 

参数以代码的方式嵌入在pipeline里面 

parameters {
  string defaultValue: 'http://192.168.11.129/root/devops-maven-service.git', description: 'git仓库地址', name: 'srcURL', trim: true
}

 

pipeline {
    agent any

    parameters {
    string defaultValue: 'http://192.168.11.129/root/devops-maven-service.git', description: 'git仓库地址', name: 'srcURL', trim: true
    string defaultValue: 'master', description: 'git分支', name: 'branchName', trim: true
    }

    stages {
        stage('Hello') {
            steps {
                script{
                    checkout([$class: 'GitSCM', branches: [[name: "${params.branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: '33375fd1-0418-4b7a-a65e-4ffcebd8e7da', url: "${params.srcURL}"]]])
                }
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值