Skip to content
Browse files

Merge pull request #70 from ikedam/feature/JENKINS-30357_NpeInParamet…


[JENKINS-30357] Fixed NPE and other improvements in ParameterizedBuildSelector
  • Loading branch information
ikedam committed Oct 4, 2015
2 parents f494646 + d7df521 commit b72f3f9c95d656ff72ead97ffce7e94684d38571
@@ -365,12 +365,24 @@ public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace, @Nonn

EnvVars env;
EnvVars env = build.getEnvironment(listener);
if (build instanceof AbstractBuild) {
env = build.getEnvironment(listener);
env.overrideAll(((AbstractBuild)build).getBuildVariables()); // Add in matrix axes..
} else {
env = new EnvVars();
// Abstract#getEnvironment(TaskListener) put build parameters to
// environments, but Run#getEnvironment(TaskListener) doesn't.
// That means we can't retrieve build parameters from WorkflowRun
// as it is a subclass of Run, not of AbstractBuild.
// We need expand build parameters manually.
// See JENKINS-26694, JENKINS-30357 for details.
for(ParametersAction pa: build.getActions(ParametersAction.class)) {
// We have to extract parameters manally as ParametersAction#buildEnvVars
// (overrides EnvironmentContributingAction#buildEnvVars)
// is applicable only for AbstractBuild.
for(ParameterValue pv: pa.getParameters()) {
pv.buildEnvironment(build, env);

PrintStream console = listener.getLogger();
@@ -23,11 +23,16 @@
package hudson.plugins.copyartifact;

import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.Run;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;

@@ -37,6 +42,7 @@
public class ParameterizedBuildSelector extends BuildSelector {
private String parameterName;
private static final Logger LOG = Logger.getLogger(ParameterizedBuildSelector.class.getName());

public ParameterizedBuildSelector(String parameterName) {
@@ -49,8 +55,51 @@ public String getParameterName() {

public Run<?,?> getBuild(Job<?,?> job, EnvVars env, BuildFilter filter, Run<?,?> parent) {
return BuildSelectorParameter.getSelectorFromXml(env.get(parameterName))
.getBuild(job, env, filter, parent);
String xml = resolveParameter(env);
if (xml == null) {
return null;
BuildSelector selector = null;
try {
selector = BuildSelectorParameter.getSelectorFromXml(xml);
} catch (Exception e) {
LOG.log(Level.SEVERE, String.format("Failed to resolve selector: %s", xml), e);
return null;
return selector.getBuild(job, env, filter, parent);

* Expand the parameter and resolve it to a xstream expression.
* <ol>
* <li>Considers an immediate value if contains '&lt;'.
* This is expected to be used in especially in workflow jobs.</li>
* <li>Otherwise, considers a variable expression if contains '$'.
* This is to keep the compatibility of usage between workflow jobs and non-workflow jobs.</li>
* <li>Otherwise, considers a variable name.</li>
* </ol>
* @param env
* @return xstream expression.
private String resolveParameter(EnvVars env) {
if (StringUtils.isBlank(getParameterName())) {
LOG.log(Level.WARNING, "Parameter name is not specified");
return null;
if (getParameterName().contains("<")) {
LOG.log(Level.FINEST, "{0} is considered a xstream expression", getParameterName());
return getParameterName();
if (getParameterName().contains("$")) {
LOG.log(Level.FINEST, "{0} is considered a variable expression", getParameterName());
return env.expand(getParameterName());
String xml = env.get(getParameterName());
if (xml == null) {
LOG.log(Level.WARNING, "{0} is not defined", getParameterName());
return xml;

@@ -2,4 +2,7 @@
Name of the "build selector" parameter. A parameter with this name should be added
in the build parameters section above. There is a special parameter type for choosing
the build selector.
You can pass not only the parameter name, but also the parameter value itself.
This is useful especially used with workflow-plugin.

0 comments on commit b72f3f9

Please sign in to comment.
You can’t perform that action at this time.