Search code examples
jenkinspipeline

Jenkins Parameterized with batch commands or powershell script


I want to add to a pipeline parameters of folders and files in a directory on an agent. e.g when I click on Build with Parameters it will show my checkbox of all the folders&files at c:\project and then I can choose which files I want for the job. I try using plugin Active Choices Parameter, and to run a groovy script

node (node1){
   stage('folders'){
       bat "dir /b /s c:\\project"
   }
}

I've tried also with powershell script Get-ChildItem -path c:/project1 -Recurse -Name


Solution

  • You can do something like below. I don't have WIndows to test. But this should work on Windows as well. Just provide the correct Path.

    pipeline {
        agent any
        parameters { choice(name: 'CHOICES', choices: listFiles("/var/jenkins_home/test"), description: '') }
        stages {
            stage('Test') {
                steps {
                    echo "Run!!!"
                    }
                }
        }
    }
    
    @NonCPS
    def listFiles(def path) {
         def files= []
         new File(path).traverse(type: groovy.io.FileType.FILES) { file ->
                            files.add(file)
                        }
        return files
    }