Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-40864

Cannot execute Git branch list command in order to populate dynamically a choice pipeline property component

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not A Defect
    • Icon: Blocker Blocker
    • pipeline
    • None

      At the first stage of a pipeline groovy code, is not possible to execute a git command in order to list, for example, the branches from a repository without @NonCPS or out of sandbox, for example when you're using a SCM code.

      Example:

      String getBrancesList(String projectSSH) {
          def command = "git ls-remote -h ${projectSSH}"
          def proc = command.execute()
      
          proc.waitFor()
      
          if ( proc.exitValue() != 0 ) {
             println "Error, ${proc.err.text}"
             return 1;
          }
      
          def branches = proc.in.text.readLines().collect { 
              it.replaceAll(/[a-z0-9]*\trefs\/heads\//, '') 
          }
      
          int index = branches.indexOf("master");
          branches.removeAt(index);
          branches.add(0, "master");
      
          return branches.join("\n")
      }
      
      ///////////////////////////////////////////////////////////////
      
      node('my_node') {
          stage('job property') {
              def branchesList = getBrancesList("my@repo.git")
      
              properties(
                  [
                      parameters(
                          choice(
                                  choices: branchesList,
                                  description: 'Select the branch.',
                                  name: 'repository_branch'
                              )
                      )
                  ]
              }
          }
      }
      

      Using @NonCPS pasting the pipeline code in the job configuration works but it doesn't matter if you trying retrieving via SCM.

            Unassigned Unassigned
            idemax Marcelo Filho
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: