Search code examples
jenkinsjenkins-pipeline

How to retrieve all lines from a text parameter in a Jenkins declarative pipeline?


I have a text parameter in my jenkinsFile used by a Jenkins pipeline job. This text parameter is used to enter a list of filenames (one per line).

Strangely only value on the first line of the text field seems to be detected when I print out build environment variables.

jenkinsFile

pipeline {
        agent any
        
        parameters {
            
            text(
                    name: 'FILE_LIST', 
                    defaultValue: '', 
                    description: 'File list'
            )
        }
        
        stages {

            stage("Environnement variables") {
                steps {
                    sh 'printenv | grep FILE_LIST'
                }
            }
        }
}

Content example for text field

test.sql
MY_TEST.SQL
test_01.sql

Result

FILE_LIST=test.sql

How to access values of all lines from a text parameter within a Jenkins pipeline?


Solution

  • if you want to get something like this "printenv | grep file1 file2 " use the below code

    pipeline {
        agent any
        
        parameters {
            
            text(
                    name: 'FILE_LIST', 
                    defaultValue: '', 
                    description: 'File list'
            )
        }
        
        stages {
    
            stage("Environnement variables") {
                steps {
                    script{
                        String fllist=FILE_LIST.split("\r?\n").join(' ')
                        echo fllist
                         
                        sh 'printenv | grep $fllist'
                    
                    }
                }
            }
        }
    

    }