2

I have a task where my Jenkins job needs two parameters for build. The first specifies the application name and can be either QA, Dev, Prod etc and the second is a server which is dependent on the first one.

Example: If I chose the app name as QA, the second parameter should display values like QAServer1, QAServer2, QAServer3.

I'm using Active Choices Plugin (https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin) to get this done but facing an problem in fetching the second parameter contents.

Snapshots: enter image description here

For obtaining the second parameter, I've written a Groovy code which reads the respective files of the selected first parameter and gets the details.

code:

#!/usr/bin/env groovy
import hudson.model.*
def Appliname = System.getenv("APPNAME")
//println Appliname
def list1 = []
def directoryName = "C:/Users/Dev/Desktop/JSONSTest"
def fileSubStr = Appliname
def filePattern = ~/${fileSubStr}/
def directory = new File(directoryName)
def findFilenameClosure =
{
   if (filePattern.matcher(it.name).find())
   {
      def jsoname = it.name
      def jsoname1 = jsoname.reverse().take(9).reverse()
      list1.add(jsoname1.substring(1,4))
      String listAsString =  "[\'${list1.join("', '")}\']"
      println "return"+listAsString
   }

}
directory.eachFileRecurse(findFilenameClosure)

The above code will print the output as return['QAServer1', 'QAServer2'] which i want to use it as input for the second parameter.

Snapshot of Second parameter:

enter image description here

Somehow the Groovy script is not being executed and second parameter value remains empty. How can i get this done dynamically. Am i following the right away to it. Kindly help me figure out. TIA

Rao
  • 18,922
  • 9
  • 46
  • 66
sdgd
  • 643
  • 12
  • 29
  • If you run the above script from out side, what do you get as output? – Rao Nov 19 '17 at 13:24
  • I'm able to get the required output - `return['QAServer1', 'QAServer2']` (which acts as an input to the second parameter - POP) – sdgd Nov 19 '17 at 13:26
  • And you are finding that the same script is not working from jenkins parameters script? – Rao Nov 19 '17 at 13:37
  • Yes. that's right. but as per the documentation given for the plugin, only if-else clauses were used to fetch the second parameter contents based on the first parameter. In case you are trying out from your end, kindly write a groovy script which just gives the output as `return['QAServer1', 'QAServer2']` and try executing it. Still wondering what am i missing. – sdgd Nov 19 '17 at 13:41

1 Answers1

1

Would you like to try below change

From:

def findFilenameClosure =
{
   if (filePattern.matcher(it.name).find())
   {
      def jsoname = it.name
      def jsoname1 = jsoname.reverse().take(9).reverse()
      list1.add(jsoname1.substring(1,4))
      String listAsString =  "[\'${list1.join("', '")}\']"
      println "return"+listAsString
   }

}
directory.eachFileRecurse(findFilenameClosure)

To:

directory.eachFileRecurse {
       if (filePattern.matcher(it.name).find()) {
          def jsoname = it.name
          def jsoname1 = jsoname.reverse().take(9).reverse()
          list1.add(jsoname1.substring(1,4))      
       } 
    }
    return list1
Rao
  • 18,922
  • 9
  • 46
  • 66
  • you are not printing the output to fetch the contents – sdgd Nov 19 '17 at 13:50
  • What do you have in the directory `JSONTest`? – Rao Nov 19 '17 at 13:52
  • `JSONTest` contains files with respective to the first parameter from which i parse it and get the values for the second parameter. – sdgd Nov 19 '17 at 16:00
  • I've tried with your suggestions. i still dont see the values populating for the second parameter. I think Groovy script doesn't get executed there in that section. – sdgd Nov 19 '17 at 16:11
  • @Dev, thank you for you patience, what do you see if the changed script is executed outside the jenkins? – Rao Nov 19 '17 at 16:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/159339/discussion-between-dev-and-rao). – sdgd Nov 19 '17 at 16:17