2

I have passwords defined in the build configuration section titled "Inject passwords to the build as environment variables":

MYVAR pasword injected into build

I want to use MYVAR (unencrypted value) in my Active Choices. Unfortunately, it's not working. The reference to MYVAR fails.

In the example below, for testing, I am just trying to display the value of MYVAR as a choice. You can see it fails and the fallback active choices script is used instead.

Ultimately, I want to use this variable to authenticate to a service to build a list of choices but without being able to even reference it in my script. I'm stuck.

enter image description here

When I try "build with parameters":

enter image description here

Thanks for your help in advance!

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Reece Markowsky
  • 113
  • 2
  • 10
  • What do you exactly mean by "_Its not working_? Any error, unexpected output, etc.? "What do you exactly mean by "_can't find a way to reference MYVAR value in my groovy script _"? Did you already try to logon to your service in your script? There's no such code in your question. – Gerold Broser Apr 23 '16 at 18:16
  • Thanks for your reply! I edited the post. you can see it does not use the script, instead uses the fallback meaning the assignment fails. – Reece Markowsky Apr 23 '16 at 18:32

2 Answers2

2

I think this is not going to work. Why? Well, the Build Environment options of the EnvInject Plugin read:

Inject environment variables to the build process

and

Inject passwords to the build as environment variables

At the time the Active Choices Plugin comes into play the build hasn't started yet and hence the injection hasn't taken place.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • 1
    +1 to Gerold Broser. There is no build yet when the plug-in is filling the parameters. It can reference other fields in the Jenkins configuration page, so in theory perhaps it could reference the envinject build configuration, but that would be an enhancement proposal (and not an easy one, and may not make sense to reference fields that are not parameters per se) – Bruno P. Kinoshita Apr 23 '16 at 20:41
1

Inspired by @Bruno's comment I developed the following to be entered in:

  • ☑ This build is parameterized
    • Active Choices Parameter
      • Script
        • ◉ Groovy Script
          • Script

// From: How can i use passwords injected in the build as environment variables in Active Choices Parameter Groovy Script
//       https://stackoverflow.com/a/36821693/1744774

import static java.lang.System.out
import static java.lang.System.err

import hudson.model.Project

import org.w3c.dom.*;
import javax.xml.parsers.*
import javax.xml.xpath.*

// -----------------------------------------------------------
// Adapt these according to your environment
final String JENKINS_HOME = '< your Jenkins home >'
final String THIS_JOB_NAME = '< your job name >'
// -----------------------------------------------------------

//try (final PrintStream LOG = new PrintStream("${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/activechoices.log")) { // doesn't work
final PrintStream LOG = new PrintStream("${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/activechoices.log")

try {
  System.setOut(LOG)
  System.setErr(LOG)
  out.println("${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/job.log")

  // groovy.lang.MissingPropertyException: No such property: Jenkins for class: Script1
  //final Project THIS_JOB = Jenkins.instance.getItem(THIS_JOB_NAME)
  //final String THIS_JOB_CONFIG = THIS_JOB.getRootDir().getPath() + '/config.xml'

  // static path to job config since the above doesn't work
  final String THIS_JOB_CONFIG = "${JENKINS_HOME}/jobs/${THIS_JOB_NAME}/config.xml"
  out.println(THIS_JOB_CONFIG)

  final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(THIS_JOB_CONFIG)

  final XPathExpression stringExpr = XPathFactory.newInstance().newXPath()
      .compile("//hudson.model.StringParameterDefinition/defaultValue/text()")
  final String STRING_PARAMETER = stringExpr.evaluate(doc, XPathConstants.NODE).getNodeValue()

  final XPathExpression pwdExpr = XPathFactory.newInstance().newXPath()
      .compile("//hudson.model.PasswordParameterDefinition/defaultValue/text()")
  final String PASSWORD_PARAMETER = pwdExpr.evaluate(doc, XPathConstants.NODE).getNodeValue()

  final List parameters = new ArrayList()
  parameters.add('static')
  parameters.add(THIS_JOB_NAME)
  //parameters.add(THIS_JOB)
  parameters.add(STRING_PARAMETER)
  parameters.add(PASSWORD_PARAMETER)
  return parameters
  }
catch (Exception e) {
  e.printStackTrace()
  }
finally {
  LOG.close()
  }

Questions:

  • Why does try-with-resources not work?
  • How to get the Jenkins instance?

Job's config.xml

  <properties>
    ...
    <hudson.model.ParametersDefinitionProperty>
      <parameterDefinitions>
        ...
        <hudson.model.PasswordParameterDefinition>
          <name>Password Parameter</name>
          <description>This is a Password Parameter.</description>
          <defaultValue>q2sZWfVMgQNyIi/pjY6yaE7DT9zRvnPv1mBcbydjlMQ=</defaultValue>
        </hudson.model.PasswordParameterDefinition>
        <hudson.model.StringParameterDefinition>
          <name>String Parameter</name>
          <description>This is a String Parameter.</description>
          <defaultValue>string value</defaultValue>
        </hudson.model.StringParameterDefinition>
        <hudson.model.StringParameterDefinition>
          <name>Another String Parameter</name>
          <description>This is another String Parameter.</description>
          <defaultValue>another string value</defaultValue>
        </hudson.model.StringParameterDefinition>
      </parameterDefinitions>
    </hudson.model.ParametersDefinitionProperty>
  </properties>

Build with Parameters

Active Choices job config

I leave it as a challenge for the reader to iterate over Nodes when using XPathExpression.evaluate(...,XPathConstants.NODESET) in case there is more than one parameter of the same type.

Community
  • 1
  • 1
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • wow thank you. pretty awesome and inspiring as well. i am going to sit down with this and give it a try. looking forward to it. – Reece Markowsky Apr 25 '16 at 20:34