How can I use passwords injected in the build as e

2020-03-26 08:43发布

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!

2条回答
聊天终结者
2楼-- · 2020-03-26 09:08

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.

查看更多
Summer. ? 凉城
3楼-- · 2020-03-26 09:14

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.

查看更多
登录 后发表回答