【问题标题】:Jenkins pipeline convert all parameters to lowercaseJenkins 管道将所有参数转换为小写
【发布时间】:2019-11-11 03:36:13
【问题描述】:

如何将 Jenkins 管道中的所有参数转换为小写。与 trim 类似,是否有一个属性可以作为参数声明的一部分添加,

对于修剪,我有类似下面的内容,

parameters {
   string defaultValue: '', description: 'Some dummy parameter', name: 'someparameter', trim: true
}

在我的管道工作中,我有超过 10 个字符串参数,我想将它们全部转换为小写

【问题讨论】:

  • 使用时可以调用toLowerCase()
  • 我正在将参数传递给 shell,如下所示,根据您的评论,我可以像 stage("test") { step { script { sh ''' someparameter=${someparameter} .toLowerCase() ''' } } }

标签: groovy parameters jenkins-pipeline jenkins-groovy


【解决方案1】:

这是一种方法:

pipeline {
    agent any
    parameters {
        string ( name: 'testName', description: 'name of the test to run')
    }
    stages {
        stage('only') {
            environment {
                TEST_NAME=params.testName.toLowerCase()
            }
            steps {
                echo "the name of the test to run is: ${params.testName}"
                sh 'echo "In Lower Case the test name is: ${TEST_NAME}"'
            }
        }
    }
}

【讨论】:

  • 感谢您的建议,但是,我确实有超过 10 个字符串参数,而且我将 env vars 用于其他一些东西,我认为将参数设置为环境变量。是否可以在 shell 中引用 params.testName.toLoweCase() ?
【解决方案2】:
sh """ ${the_parameter.toLowerCase()} """
  1. 需要使用双引号才能获得 GString
  2. toLowerCase() 函数调用放在大括号内,以便shell 引用回groovy

【讨论】:

    【解决方案3】:

    其实一个人就可以做到

    VAR = "${VAR.toLowerCase()}"
    

    【讨论】:

      猜你喜欢
      • 2014-02-19
      • 1970-01-01
      • 2022-08-05
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      • 2012-08-02
      相关资源
      最近更新 更多