6

How can I convert all the parameters in a Jenkins pipeline to lowercase. Similar to trim, is there an attribute that one could add as part of the parameter declaration,

For trim, I have something like below,

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

In my pipeline job, I have more than 10 string parameters and would like to convert them all to lowercase

Sai
  • 1,790
  • 5
  • 29
  • 51
  • 3
    you can call toLowerCase() on them when you use them – Rich Duncan Jun 29 '19 at 17:56
  • I'm passing the parameters to shell in a stage like below, per your comment can i do like stage("test") { step { script { sh ''' someparameter=${someparameter}.toLowerCase() ''' } } } – Sai Jun 30 '19 at 01:54

4 Answers4

12

Here's one approach:

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}"'
            }
        }
    }
}
Rich Duncan
  • 1,845
  • 1
  • 12
  • 13
  • Thank you for the suggestion, however, I do have more than 10 string parameters and also i'm using env vars for few other stuffs, I think it will not be a good idea to set parameters as env vars. Will it be possible to refer params.testName.toLoweCase() inside the shell? – Sai Jul 01 '19 at 00:37
8
sh """ ${the_parameter.toLowerCase()} """
  1. Need to use double quote so you have a GString
  2. Put the toLowerCase() function call inside the brace for shell to refer back to groovy
ItsPete
  • 2,363
  • 3
  • 27
  • 35
Jacccck
  • 469
  • 5
  • 6
2

Actually one can just do

VAR = "${VAR.toLowerCase()}"
alfredyang
  • 31
  • 4
0

Had to use this for my use case. It will not convert but it will prevent passing wrong value.

validatingString(name: "MYVAR",
                 defaultValue: "",
                 regex: /^[a-z0-9]+$/,
                 failedValidationMessage: "",
                 description: "")
Nik
  • 1