Creating Interdependant CascadeChoiceParameter in jenkins

20 Views Asked by At

I have following directory structure C:/Jenkins_Tools/RU/ [1]: https://i.stack.imgur.com/i6eKO.png Also I have text file for versions C:\RU_Versions.txt its data like following version=2202,9999

I created following jenkins script for creating three choices, in first choice box I read version value. second's value dynamiclly fetching after selecting first choice parameter. And I want to fetch value in third choice box which fetch value after selecting second choice. It means when we select version suppose 2202, it make list of folders in C:/Jenkins_Tools/RU/2202/ReleaseUpdate And when we select second choice, suppose it is C:/Jenkins_Tools/RU/2202/ReleaseUpdate/2202.0100 version, then it make list of values in C:/Jenkins_Tools/RU/2202/ReleaseUpdate/2202.0100
But I am facing in fetching value in third choice box.

    properties([
        parameters([
        [$class: 'CascadeChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT',
            description: 'Release Update',
            filterLength: 1,
            filterable: false,
            name: 'Release_Update',
            referencedParameters: 'Version_Number',
            script: [$class: 'GroovyScript',
                    fallbackScript: [
                    classpath: [], 
                    sandbox: false, 
                    script: 'return ["ERROR"]'
                ],
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: """
                            import groovy.io.*
                            import groovy.io.FileType

                            def list = []
                            //for taking this structure we need blank dir structure on server machine, currently our main server machine is simjen
                            def dir = new File("C:/Jenkins_Tools/RU/" + Version_Number + "/ReleaseUpdate/")
                            dir.eachDir { subDir ->
                                list.add(subDir.getName())
                            }
                            //all this liste folder is return in Release_update choice box dynamically
                            return list.reverse()
                    """.stripIndent()
                ] //end script
            ]
        ],
        [$class: 'CascadeChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT',
            description: 'Build No',
            filterLength: 1,
            filterable: false,
            name: 'Build_No',
            referencedParameters: 'Release_Update',
            script: [$class: 'GroovyScript',
                fallbackScript: [
                    classpath: [], 
                    sandbox: false, 
                    script: 'return ["ERROR"]'
                ],
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: """
                            import groovy.io.*
                            import groovy.io.FileType
                            
                            //following static part working
                            /*def list = ['01','02','03']
                            return list.reverse() */
                            
                            /*dynamic value fetching not worked yet*/
                            echo "Release_Update: ${Release_Update}"
                            def list = []
                            //for taking this structure we need blank dir structure on server machine, currently our main server machine is simjen
                            def dir = new File("C:/Jenkins_Tools/RU/" + Version_Number + "/ReleaseUpdate/" + Release_Update + "/")
                            dir.eachDir { subDir ->
                                list.add(subDir.getName())
                            }
                            //all this liste folder is return in Release_update choice box dynamically
                            return list.reverse()
                    """.stripIndent()
                ] //end script
            ]
        ]
    ])
])
    pipeline 
    {  
      agent { label "iltlvinteg6" }
      parameters 
      {
        extendedChoice name: 'Version_Number', description: 'Version Number', multiSelectDelimiter: ',', propertyFile: 'C:\\RU_Versions.txt', propertyKey: 'Version', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', visibleItemCount: 6
    }
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
                
                script {
                    def dirPath = "C:/Jenkins_Tools/RU/" + params.Version_Number + "/ReleaseUpdate/"
                    def dir = new File(dirPath)
                    print("1. Listing subdirectories of ${dir}:")

                    // Check if the directory exists
                    if (dir.exists() && dir.isDirectory()) 
                    {
                        def subDirs = dir.listFiles().findAll { it.isDirectory() }
                        // Example: Only list directories starting with "SETUP_"
                        for (subDir in subDirs) {
                                echo "${subDir.name}"
                        }
                    } 
                    else 
                    {
                        error "Directory not found: ${dirPath}"
                    }
                        
                    dir = new File("C:/Jenkins_Tools/RU/" + params.Version_Number + "/ReleaseUpdate/" + params.Release_Update + "/") // Adjust the path as needed
                    print("2. Listing subdirectories of ${dir}:")
                    // Check if the directory exists
                    if (dir.exists() && dir.isDirectory()) 
                    {
                        def subDirs = dir.listFiles().findAll { it.isDirectory() }
                        for (subDir in subDirs) {
                                echo "${subDir.name}"
                        }
                    } 
                    else 
                    {
                        echo "Directory not found: ${dirPath}"
                    }

                }
            }
        }
    }
}
0

There are 0 best solutions below