Search code examples
mavenjenkinsjenkins-pipelinejenkins-groovyjenkins-declarative-pipeline

How to pass selected values of active choice parameters to build script


I'm new to Jenkins, please help me on this. I have a maven project which is bit complex so I have separated the tests using testNG xmls. Based on the execution flow I will selects the appropriate .xml file to build the project. For now what I'm doing is I'm building the script manually by selecting the required xml file as below.

I have set a choice parameter (name=testSuite) which includes all the xmls and include mvn clean test command in Build section.

mvn install test -DsuiteXmlFile=src/test/resources/testSuite/$testSuite

When I build it will get the selected value from the choice parameter drop down and execute.

But my requirement is I want to integrate this to run the build periodically in a specific time period. For that I tried using "Active Choice Parameters" but please help me how to call the selected check box options and proceed with mvn install test

Below is the approach I used.

  1. I created a pipeline script to generated "Active choice parameters and reference parameters"
  2. Then I tried to fetch the selected values using `echo "Scripts: ${params.Scripts}"

Below are my parameters,

  • Active choice parameter: Flow
  • Active choice reactive parameter :Scripts
  • Active Reference parameter: Flow Information

the "Scripts" will include check boxes and the xml files

Build with Parameter UI

Selected xml options

Pipeline console output

In this scenario how will I pass the selected xml to mvn clean test because as of now it's passing all the selected values with comma separators ascaseCreation.xml,testng.xml. Due to this how can I seperate each selected xmls and pass it to mvn install test -DsuiteXmlFile=src/test/resources/testSuite/$testSuite.

And also please help me to figure out a better approach to have periodically builds in specific time period which could cater the above scenario.

Thanks in advance.

`


Solution

  • Below is an example of a declarative pipeline:

    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    script {
                        Scripts.tokenize(',').each{
                            sh "mvn install test -DsuiteXmlFile=src/test/resources/testSuite/${it}"
                        }
                    }
                }
            }
        }
    }
    

    Scripted:

    node (){ 
        stage("Build") {
            Scripts.tokenize(',').each{
                sh "mvn install test -DsuiteXmlFile=src/test/resources/testSuite/${it}"
            }
        }
    }