【问题标题】:Jenkinsfile: Trim choice parameters and pass it to stageJenkinsfile:修剪选择参数并将其传递给阶段
【发布时间】:2021-06-05 00:29:22
【问题描述】:

下面是我正在使用的 jenkins 文件。但是现在我需要根据 ENVIRONMENT 选择 ID,因为根据 ENVIRONMENT 记住帐户 ID 并不容易。 当我选择 sdbx 来选择 1807402234 时,我可以在 jenkinsfile 中添加什么方法。请建议

如果我使用下面的选择参数,有没有办法在舞台上修剪 ID 参数。

choice(
    name: 'ID',
    choices: [ 'sdbx-1807402234', 'devl-5187460678', 'test-891137040']
)

注意:我需要 ENVIRONMENT 和 ID 参数。我也是 jenkins groovy 的初学者。

我的 Jenkins 文件

def deploy(env) {
  step([$class: 'UCDeployPublisher',
  siteName: siteName,
  deploy: [
  $class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
  deployApp: appName,
  deployEnv: 'DEV',
  deployVersions: "${compName}:${version}",
  deployProc: simpleDeploy,
  deployOnlyChanged: false,
  deployReqProps: "ID=${params.ID}"
 ]])

parameters {
  choice(
  name: 'ENVIRONMENT',
  choices: [ 'sdbx', 'devl', 'test' ]
  )

  choice(
    name: 'ID',
    choices: [ '1807402234', '5187460678', '891137040']
)
stage (DEV') {

    steps {
        script {
         if (params.ENVIRONMENT == "dev"){
             deploy('devl') ===> this will call my deploy function
         }
     }
  }
}

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    是的,您可以使用split 函数来实现这一点。
    PARAMS = params.ID.split("-")
    示例:

    def PARAMS = []
    pipeline {
         parameters
        {
             choice(
                    choices: ['sdbx-1807402234', 'devl-5187460678', 'test-891137040'], 
                    name: 'ID'
                   )
        }
    agent any
       stages {
                stage (DEV') {
                  steps {
                   script{
                       // Split choice parameter selected  ID by '-'
                       PARAMS = params.ID.split("-")
                       // Get environment name selected
                       def environment = PARAMS[0]
                       // Get ID selected
                       def id = PARAMS[1]
                       println ("environment is : ${environment}")
                       println ("id is : ${id}")
                       if (environment  == "dev"){
                              deploy('devl') ===> this will call my deploy function
                           }
                       }
                   }
                }
       }
    
    

    输出:

    【讨论】:

      猜你喜欢
      • 2021-08-01
      • 1970-01-01
      • 2012-08-31
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      相关资源
      最近更新 更多