Friday, 16 September 2016

Running Groovy From Jenkins And Reading/Updating Jenkins Parameters Set Using Active Choice Parameters Plugin

Running Groovy In Jenins -

1. Need to install Groovy plugin.
2. Update Global Tool Configuration For Groovy to set path -
in my case, it is - C:\Program Files\groovy-2.4.7
Note - its not upto bin
3. add jars to groovy lib folder - jenkins-core-2.7.4.jar,stapler-1.243.jar,commons-lang-2.6.jar
jenkins-core-2.7.4.jar,stapler-1.243.jar are available in jenkins under
{User folder}\.jenkins\war\WEB-INF\lib

commons-lang-2.6.jar can be downloaded from maven.

Run groovy script as build step - Execute System Groovy Script

Reading/Updating Jenkins Parameters
Reference -
http://stackoverflow.com/questions/10882515/how-to-retrieve-jenkins-build-parameters-using-the-groovy-api
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script

My problem statement -
1. I had active choices parameters for MSTest TestCategory to execute. Like - Sanity,Smoke,Regression
2. Active Choices parameters sets parameter as - Sanity,Smoke,Regression.
3. MSTest TestCategory switch needs in Sanity|Smoke if one needs to execute tests from Sanity and Smoke Category on.
4. So, I need to reset TestCategory Parameter used for

Following was my solution -

import hudson.model.*

def testCategory = build.buildVariableResolver.resolve("TestCategory")
testCategory = testCategory.replaceAll(',','|')

def pa = new ParametersAction([
  new StringParameterValue("TestCategory", testCategory)
])

// add variable to current job
Thread.currentThread().executable.addAction(pa)







No comments:

Post a Comment