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

2.4k views Asked by At

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.

`

2

There are 2 answers

0
Marat On BEST ANSWER

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}"
        }
    }
}
1
Marat On

I would like to recommend a plugin I use myself to run jobs based on cron syntax with ability to specify parameters: Parameterized Scheduler

properties([
    pipelineTriggers([
        parameterizedCron('''
            0 20 * * 1-5 %gitRevision=desktop/dev;brokerName=robotests
            0 00 * * 1-5 %gitRevision=master;brokerName=robotests
        ''')
    ]),
])

If I understood you correctly it should be enough to define several runs with different parameters. If you want to use Active Choice parameter value, then you should understand that it stores all values as a string. Use Groovy magic to convert string to List and specify the required index (split it), e.g.

sh "mvn install test -DsuiteXmlFile=src/test/resources/testSuite/${Scripts.tokenize(',')[0]}"

will be your first value