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

Active Choice Plugin 2.5.6 Reactive Reference Parameter Formatted HTML cannot access environment variables

XMLWordPrintable

      I want to access environment variables in a Reactive Reference Parameter Formatted HTML parameter.

      I have a MultibranchPipeline Job in which I want to populate a parameter with dynamic values based on the branch name.

      My use case: I have an Azure Container Registry in which I have Docker Images with the following pattern: <branch_name>/<project>. In my multibranch pipeline job, I would like to access only the projects on that branch. 

      I tried to use the env.BRANCH_NAME variable but I couldn't manage to. I guess that value is populated at build time, and I can't access it beforehand.

      My code:

      [
          $class: 'DynamicReferenceParameter',
          choiceType: 'ET_FORMATTED_HTML',
          name: 'Tag',
          omitValueField: true,
          description: 'Select the tag to be deployed.',
          script: [
              $class: 'GroovyScript',
              fallbackScript: [
                  classpath: [],
                  sandbox: false,
                  script: '''
                      return """
                          <label for='value'>Error: The script failed or it is not allowed by Jenkins. Check Script approval from General Settings. Enter the value manually:</label><br>
                          <input type='text' name='value'>
                      """
                  '''
              ], 
              script: [
                  classpath: [],
                  sandbox: false,
                  script: '''
                      def jsonSlurper = new groovy.json.JsonSlurper()
                      def acrCred = jenkins.model.Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].credentials.find { it ->
                          it.getId() == 'my_id'
                      }
                      
                      def acrUsername = acrCred.getUsername()
                      def acrPassword = acrCred.getPassword().getPlainText()
                      
                      def auth = "${acrUsername}:${acrPassword}".bytes.encodeBase64().toString()
                      def tags = ['projA', 'projB'].collect { name ->
                          def req = (HttpURLConnection) new URL("https://${acrUsername}.azurecr.io/acr/v1/${env.BRANCH_NAME}/${name}/_tags").openConnection()
                          req.setRequestProperty('Authorization', "Basic ${auth}")
                          def result = jsonSlurper.parseText(req.inputStream.text)
                          result.tags.sort { a, b -> b.createdTime <=> a.createdTime }.collect { it.name }
                      }
                      
                      tags = tags[0].intersect(tags[1])
                      def html = "<select name='value'>"
                      tags.forEach { tagName ->
                          html += "<option value='${tagName}'>${tagName}</option>"
                      }
      
                      html += "</select>"
                      return html
                  '''
              ]
          ]
      ]

      I'm opening this issue because in your docs you mentioned that we have access to two env variables:

      • jenkinsProject
      • jenkinsBuild

      I tried to use them but I get the following error:

      hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: jenkinsBuild for class: WorkflowScript
      

      That error occurs when I use triple double quotes, i.e.:

      script: """jenkinsBuild.foo"""
      

      If instead, I use triple single quotes my fallback script runs.

       

      My question: Is there any way of getting the branch name in a Reactive Reference Parameter Formatted HTML?

            kinow Bruno P. Kinoshita
            vivere Vivere
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: