Groovy Script failing in Jenkins job but runs fine from command line










0














I have the following Managed Jenkinsfile for Pipeline Job stripped of the Stages for brevity.



#!groovy
import groovy.json.JsonSlurperClassic

def json = new File("TEST_JSON.json").text
def data = new JsonSlurperClassic().parseText(json)

def string_1 = data.test

properties([
parameters([
string(defaultValue: string_1, description: 'STRING 1', name: 'STRING_1', trim: false),
string(defaultValue: string_1, description: 'STRING 2', name: 'STRING_2', trim: false),
[$class: 'ChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'MPSS Flavor',
filterLength: 1, filterable: true,
name: 'MPSS_FLAVOR', randomName: 'choice-parameter-10980926894589',
script: [
$class: 'GroovyScript',
fallbackScript: [classpath: , sandbox: false, script: ''],
script: [
classpath: , sandbox: false,
script: '''
import groovy.json.JsonSlurperClassic
def json = new File("TEST_JSON.json").text
def data = new JsonSlurperClassic().parseText(json)
mpss_flavors =
for (option in data.options)
println option
mpss_flavors.add(option.mpss_flavor)

return mpss_flavors
'''
]
]
],
[$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'TARGET',
filterLength: 1, filterable: true,
name: 'TARGET', randomName: 'choice-parameter-10980967122105',
referencedParameters: 'MPSS_FLAVOR',
script: [
$class: 'GroovyScript',
fallbackScript: [classpath: , sandbox: false, script: ''],
script: [
classpath: , sandbox: false,
script: '''
import groovy.json.JsonSlurperClassic
def json = new File("TEST_JSON.json").text
def data = new JsonSlurperClassic().parseText(json)
targets =
for (option in data.options)
if (option.mpss_flavor == MPSS_FLAVOR)
targets.add(option.target);


return targets;
'''
]
]
],
[$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'Build Command',
filterLength: 1, filterable: true,
name: 'BUILD_CMD', randomName: 'choice-parameter-11980967122105',
referencedParameters: 'TARGET,MPSS_FLAVOR',
script: [
$class: 'GroovyScript',
fallbackScript: [classpath: , sandbox: false, script: 'return ["ERROR"]'],
script: [
classpath: , sandbox: false,
script: '''
import groovy.json.JsonSlurperClassic;
def json = new File("TEST_JSON.json").text;
def data = new JsonSlurperClassic().parseText(json);
build_cmds = ;
for (option in data.options)
if ((option.mpss_flavor == MPSS_FLAVOR) && (option.target == TARGET))
build_cmds.addAll(option.build_commands);


return build_cmds;
'''
]
]
]
])
])


And the following JSON File containg the configurations



 
"test": "TEST STRING FROM JSON",
"options": [

"mpss_flavor": "MPSS1",
"target": "TARGET1",
"build_commands": [
"BUILD_COMMAND_1"
]
,

"mpss_flavor": "MPSS2",
"target": "TARGET2",
"build_commands": [
"BUILD_COMMAND_2",
"BUILD_COMMAND_3"
]

]



I would like to configure the Parameters for the Job automatically when the JOSN file is updated (I am fully aware that the first Job run after the JSON Update will not contain the intended changes, but that is Ok for us). MPSS_FLAVOR and TARGET are showing the Values as intended. However the BUILD_CMD Choice parameter is returning an error. When I run the groovy script code with Statically defined MPSS_FLAVOR and TARGET in command line the Script works fine and the returned build_cmds are as expected. However Through Jenkins UI It is showing as ERROR (Fallback Script)



I have tried several iterations without any success. I am sure there is a minor mistake and I Could Figure out.



My Question is there any way to see the print Logs of the script that is used for choice parameter to isolate the issue.



Update
Same code works fine for Jenkins file defined within the Job. issue seems to be specific to Managed Jenkins file










share|improve this question




























    0














    I have the following Managed Jenkinsfile for Pipeline Job stripped of the Stages for brevity.



    #!groovy
    import groovy.json.JsonSlurperClassic

    def json = new File("TEST_JSON.json").text
    def data = new JsonSlurperClassic().parseText(json)

    def string_1 = data.test

    properties([
    parameters([
    string(defaultValue: string_1, description: 'STRING 1', name: 'STRING_1', trim: false),
    string(defaultValue: string_1, description: 'STRING 2', name: 'STRING_2', trim: false),
    [$class: 'ChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'MPSS Flavor',
    filterLength: 1, filterable: true,
    name: 'MPSS_FLAVOR', randomName: 'choice-parameter-10980926894589',
    script: [
    $class: 'GroovyScript',
    fallbackScript: [classpath: , sandbox: false, script: ''],
    script: [
    classpath: , sandbox: false,
    script: '''
    import groovy.json.JsonSlurperClassic
    def json = new File("TEST_JSON.json").text
    def data = new JsonSlurperClassic().parseText(json)
    mpss_flavors =
    for (option in data.options)
    println option
    mpss_flavors.add(option.mpss_flavor)

    return mpss_flavors
    '''
    ]
    ]
    ],
    [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'TARGET',
    filterLength: 1, filterable: true,
    name: 'TARGET', randomName: 'choice-parameter-10980967122105',
    referencedParameters: 'MPSS_FLAVOR',
    script: [
    $class: 'GroovyScript',
    fallbackScript: [classpath: , sandbox: false, script: ''],
    script: [
    classpath: , sandbox: false,
    script: '''
    import groovy.json.JsonSlurperClassic
    def json = new File("TEST_JSON.json").text
    def data = new JsonSlurperClassic().parseText(json)
    targets =
    for (option in data.options)
    if (option.mpss_flavor == MPSS_FLAVOR)
    targets.add(option.target);


    return targets;
    '''
    ]
    ]
    ],
    [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'Build Command',
    filterLength: 1, filterable: true,
    name: 'BUILD_CMD', randomName: 'choice-parameter-11980967122105',
    referencedParameters: 'TARGET,MPSS_FLAVOR',
    script: [
    $class: 'GroovyScript',
    fallbackScript: [classpath: , sandbox: false, script: 'return ["ERROR"]'],
    script: [
    classpath: , sandbox: false,
    script: '''
    import groovy.json.JsonSlurperClassic;
    def json = new File("TEST_JSON.json").text;
    def data = new JsonSlurperClassic().parseText(json);
    build_cmds = ;
    for (option in data.options)
    if ((option.mpss_flavor == MPSS_FLAVOR) && (option.target == TARGET))
    build_cmds.addAll(option.build_commands);


    return build_cmds;
    '''
    ]
    ]
    ]
    ])
    ])


    And the following JSON File containg the configurations



     
    "test": "TEST STRING FROM JSON",
    "options": [

    "mpss_flavor": "MPSS1",
    "target": "TARGET1",
    "build_commands": [
    "BUILD_COMMAND_1"
    ]
    ,

    "mpss_flavor": "MPSS2",
    "target": "TARGET2",
    "build_commands": [
    "BUILD_COMMAND_2",
    "BUILD_COMMAND_3"
    ]

    ]



    I would like to configure the Parameters for the Job automatically when the JOSN file is updated (I am fully aware that the first Job run after the JSON Update will not contain the intended changes, but that is Ok for us). MPSS_FLAVOR and TARGET are showing the Values as intended. However the BUILD_CMD Choice parameter is returning an error. When I run the groovy script code with Statically defined MPSS_FLAVOR and TARGET in command line the Script works fine and the returned build_cmds are as expected. However Through Jenkins UI It is showing as ERROR (Fallback Script)



    I have tried several iterations without any success. I am sure there is a minor mistake and I Could Figure out.



    My Question is there any way to see the print Logs of the script that is used for choice parameter to isolate the issue.



    Update
    Same code works fine for Jenkins file defined within the Job. issue seems to be specific to Managed Jenkins file










    share|improve this question


























      0












      0








      0







      I have the following Managed Jenkinsfile for Pipeline Job stripped of the Stages for brevity.



      #!groovy
      import groovy.json.JsonSlurperClassic

      def json = new File("TEST_JSON.json").text
      def data = new JsonSlurperClassic().parseText(json)

      def string_1 = data.test

      properties([
      parameters([
      string(defaultValue: string_1, description: 'STRING 1', name: 'STRING_1', trim: false),
      string(defaultValue: string_1, description: 'STRING 2', name: 'STRING_2', trim: false),
      [$class: 'ChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'MPSS Flavor',
      filterLength: 1, filterable: true,
      name: 'MPSS_FLAVOR', randomName: 'choice-parameter-10980926894589',
      script: [
      $class: 'GroovyScript',
      fallbackScript: [classpath: , sandbox: false, script: ''],
      script: [
      classpath: , sandbox: false,
      script: '''
      import groovy.json.JsonSlurperClassic
      def json = new File("TEST_JSON.json").text
      def data = new JsonSlurperClassic().parseText(json)
      mpss_flavors =
      for (option in data.options)
      println option
      mpss_flavors.add(option.mpss_flavor)

      return mpss_flavors
      '''
      ]
      ]
      ],
      [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'TARGET',
      filterLength: 1, filterable: true,
      name: 'TARGET', randomName: 'choice-parameter-10980967122105',
      referencedParameters: 'MPSS_FLAVOR',
      script: [
      $class: 'GroovyScript',
      fallbackScript: [classpath: , sandbox: false, script: ''],
      script: [
      classpath: , sandbox: false,
      script: '''
      import groovy.json.JsonSlurperClassic
      def json = new File("TEST_JSON.json").text
      def data = new JsonSlurperClassic().parseText(json)
      targets =
      for (option in data.options)
      if (option.mpss_flavor == MPSS_FLAVOR)
      targets.add(option.target);


      return targets;
      '''
      ]
      ]
      ],
      [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'Build Command',
      filterLength: 1, filterable: true,
      name: 'BUILD_CMD', randomName: 'choice-parameter-11980967122105',
      referencedParameters: 'TARGET,MPSS_FLAVOR',
      script: [
      $class: 'GroovyScript',
      fallbackScript: [classpath: , sandbox: false, script: 'return ["ERROR"]'],
      script: [
      classpath: , sandbox: false,
      script: '''
      import groovy.json.JsonSlurperClassic;
      def json = new File("TEST_JSON.json").text;
      def data = new JsonSlurperClassic().parseText(json);
      build_cmds = ;
      for (option in data.options)
      if ((option.mpss_flavor == MPSS_FLAVOR) && (option.target == TARGET))
      build_cmds.addAll(option.build_commands);


      return build_cmds;
      '''
      ]
      ]
      ]
      ])
      ])


      And the following JSON File containg the configurations



       
      "test": "TEST STRING FROM JSON",
      "options": [

      "mpss_flavor": "MPSS1",
      "target": "TARGET1",
      "build_commands": [
      "BUILD_COMMAND_1"
      ]
      ,

      "mpss_flavor": "MPSS2",
      "target": "TARGET2",
      "build_commands": [
      "BUILD_COMMAND_2",
      "BUILD_COMMAND_3"
      ]

      ]



      I would like to configure the Parameters for the Job automatically when the JOSN file is updated (I am fully aware that the first Job run after the JSON Update will not contain the intended changes, but that is Ok for us). MPSS_FLAVOR and TARGET are showing the Values as intended. However the BUILD_CMD Choice parameter is returning an error. When I run the groovy script code with Statically defined MPSS_FLAVOR and TARGET in command line the Script works fine and the returned build_cmds are as expected. However Through Jenkins UI It is showing as ERROR (Fallback Script)



      I have tried several iterations without any success. I am sure there is a minor mistake and I Could Figure out.



      My Question is there any way to see the print Logs of the script that is used for choice parameter to isolate the issue.



      Update
      Same code works fine for Jenkins file defined within the Job. issue seems to be specific to Managed Jenkins file










      share|improve this question















      I have the following Managed Jenkinsfile for Pipeline Job stripped of the Stages for brevity.



      #!groovy
      import groovy.json.JsonSlurperClassic

      def json = new File("TEST_JSON.json").text
      def data = new JsonSlurperClassic().parseText(json)

      def string_1 = data.test

      properties([
      parameters([
      string(defaultValue: string_1, description: 'STRING 1', name: 'STRING_1', trim: false),
      string(defaultValue: string_1, description: 'STRING 2', name: 'STRING_2', trim: false),
      [$class: 'ChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'MPSS Flavor',
      filterLength: 1, filterable: true,
      name: 'MPSS_FLAVOR', randomName: 'choice-parameter-10980926894589',
      script: [
      $class: 'GroovyScript',
      fallbackScript: [classpath: , sandbox: false, script: ''],
      script: [
      classpath: , sandbox: false,
      script: '''
      import groovy.json.JsonSlurperClassic
      def json = new File("TEST_JSON.json").text
      def data = new JsonSlurperClassic().parseText(json)
      mpss_flavors =
      for (option in data.options)
      println option
      mpss_flavors.add(option.mpss_flavor)

      return mpss_flavors
      '''
      ]
      ]
      ],
      [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'TARGET',
      filterLength: 1, filterable: true,
      name: 'TARGET', randomName: 'choice-parameter-10980967122105',
      referencedParameters: 'MPSS_FLAVOR',
      script: [
      $class: 'GroovyScript',
      fallbackScript: [classpath: , sandbox: false, script: ''],
      script: [
      classpath: , sandbox: false,
      script: '''
      import groovy.json.JsonSlurperClassic
      def json = new File("TEST_JSON.json").text
      def data = new JsonSlurperClassic().parseText(json)
      targets =
      for (option in data.options)
      if (option.mpss_flavor == MPSS_FLAVOR)
      targets.add(option.target);


      return targets;
      '''
      ]
      ]
      ],
      [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'Build Command',
      filterLength: 1, filterable: true,
      name: 'BUILD_CMD', randomName: 'choice-parameter-11980967122105',
      referencedParameters: 'TARGET,MPSS_FLAVOR',
      script: [
      $class: 'GroovyScript',
      fallbackScript: [classpath: , sandbox: false, script: 'return ["ERROR"]'],
      script: [
      classpath: , sandbox: false,
      script: '''
      import groovy.json.JsonSlurperClassic;
      def json = new File("TEST_JSON.json").text;
      def data = new JsonSlurperClassic().parseText(json);
      build_cmds = ;
      for (option in data.options)
      if ((option.mpss_flavor == MPSS_FLAVOR) && (option.target == TARGET))
      build_cmds.addAll(option.build_commands);


      return build_cmds;
      '''
      ]
      ]
      ]
      ])
      ])


      And the following JSON File containg the configurations



       
      "test": "TEST STRING FROM JSON",
      "options": [

      "mpss_flavor": "MPSS1",
      "target": "TARGET1",
      "build_commands": [
      "BUILD_COMMAND_1"
      ]
      ,

      "mpss_flavor": "MPSS2",
      "target": "TARGET2",
      "build_commands": [
      "BUILD_COMMAND_2",
      "BUILD_COMMAND_3"
      ]

      ]



      I would like to configure the Parameters for the Job automatically when the JOSN file is updated (I am fully aware that the first Job run after the JSON Update will not contain the intended changes, but that is Ok for us). MPSS_FLAVOR and TARGET are showing the Values as intended. However the BUILD_CMD Choice parameter is returning an error. When I run the groovy script code with Statically defined MPSS_FLAVOR and TARGET in command line the Script works fine and the returned build_cmds are as expected. However Through Jenkins UI It is showing as ERROR (Fallback Script)



      I have tried several iterations without any success. I am sure there is a minor mistake and I Could Figure out.



      My Question is there any way to see the print Logs of the script that is used for choice parameter to isolate the issue.



      Update
      Same code works fine for Jenkins file defined within the Job. issue seems to be specific to Managed Jenkins file







      jenkins groovy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 15:41







      rcreddy

















      asked Nov 13 '18 at 6:04









      rcreddyrcreddy

      355




      355






















          1 Answer
          1






          active

          oldest

          votes


















          0














          just wrap script with try-catch



          try
          ...your parameter script here
          catch(Throwable t)
          return [t.toString()]



          in this case you'll see error as a parameter value






          share|improve this answer




















          • Daggett, I tried your suggestion and still see empty string. See update for further experiment
            – rcreddy
            Nov 13 '18 at 15:40











          • if you don't see any error - the problem in file+code. could it be that file is in utf-32 format? or contains some special headers (utf-8 BOM for example)
            – daggett
            Nov 13 '18 at 17:04










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274752%2fgroovy-script-failing-in-jenkins-job-but-runs-fine-from-command-line%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          just wrap script with try-catch



          try
          ...your parameter script here
          catch(Throwable t)
          return [t.toString()]



          in this case you'll see error as a parameter value






          share|improve this answer




















          • Daggett, I tried your suggestion and still see empty string. See update for further experiment
            – rcreddy
            Nov 13 '18 at 15:40











          • if you don't see any error - the problem in file+code. could it be that file is in utf-32 format? or contains some special headers (utf-8 BOM for example)
            – daggett
            Nov 13 '18 at 17:04















          0














          just wrap script with try-catch



          try
          ...your parameter script here
          catch(Throwable t)
          return [t.toString()]



          in this case you'll see error as a parameter value






          share|improve this answer




















          • Daggett, I tried your suggestion and still see empty string. See update for further experiment
            – rcreddy
            Nov 13 '18 at 15:40











          • if you don't see any error - the problem in file+code. could it be that file is in utf-32 format? or contains some special headers (utf-8 BOM for example)
            – daggett
            Nov 13 '18 at 17:04













          0












          0








          0






          just wrap script with try-catch



          try
          ...your parameter script here
          catch(Throwable t)
          return [t.toString()]



          in this case you'll see error as a parameter value






          share|improve this answer












          just wrap script with try-catch



          try
          ...your parameter script here
          catch(Throwable t)
          return [t.toString()]



          in this case you'll see error as a parameter value







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 8:16









          daggettdaggett

          7,75721529




          7,75721529











          • Daggett, I tried your suggestion and still see empty string. See update for further experiment
            – rcreddy
            Nov 13 '18 at 15:40











          • if you don't see any error - the problem in file+code. could it be that file is in utf-32 format? or contains some special headers (utf-8 BOM for example)
            – daggett
            Nov 13 '18 at 17:04
















          • Daggett, I tried your suggestion and still see empty string. See update for further experiment
            – rcreddy
            Nov 13 '18 at 15:40











          • if you don't see any error - the problem in file+code. could it be that file is in utf-32 format? or contains some special headers (utf-8 BOM for example)
            – daggett
            Nov 13 '18 at 17:04















          Daggett, I tried your suggestion and still see empty string. See update for further experiment
          – rcreddy
          Nov 13 '18 at 15:40





          Daggett, I tried your suggestion and still see empty string. See update for further experiment
          – rcreddy
          Nov 13 '18 at 15:40













          if you don't see any error - the problem in file+code. could it be that file is in utf-32 format? or contains some special headers (utf-8 BOM for example)
          – daggett
          Nov 13 '18 at 17:04




          if you don't see any error - the problem in file+code. could it be that file is in utf-32 format? or contains some special headers (utf-8 BOM for example)
          – daggett
          Nov 13 '18 at 17:04

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274752%2fgroovy-script-failing-in-jenkins-job-but-runs-fine-from-command-line%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党