From 7e510e9621c7f0e3f348a4041702a123a1b479a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?To=CC=83nis=20Pool?= Date: Fri, 16 Sep 2016 15:12:17 +0300 Subject: [PATCH] Try to retrieve dynamic axis value from Jenkins global properties straight away This makes sure that if the dynamic value comes from Jenkins global variable then the matrix job will have active configurations correctly set / shown right away. Otherwise before the build has run (or after a restart) the active configurations of the matrix job won't reflect the dynamic axis values as they are only computed during the first build. Potentially could expand this to try to load the value from System.getenv() as well. --- .../jenkins/plugins/daxis/DynamicAxis.java | 47 +++++++++++++++---- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/src/main/java/ca/silvermaplesolutions/jenkins/plugins/daxis/DynamicAxis.java b/src/main/java/ca/silvermaplesolutions/jenkins/plugins/daxis/DynamicAxis.java index c2ab686..869a601 100644 --- a/src/main/java/ca/silvermaplesolutions/jenkins/plugins/daxis/DynamicAxis.java +++ b/src/main/java/ca/silvermaplesolutions/jenkins/plugins/daxis/DynamicAxis.java @@ -9,13 +9,19 @@ import hudson.matrix.AxisDescriptor; import hudson.matrix.MatrixBuild; import hudson.model.TaskListener; +import hudson.slaves.EnvironmentVariablesNodeProperty; +import hudson.slaves.NodeProperty; import hudson.util.FormValidation; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.logging.Logger; import java.util.regex.Pattern; +import jenkins.model.Jenkins; import net.sf.json.JSONObject; import org.kohsuke.stapler.DataBoundConstructor; @@ -51,6 +57,7 @@ public DynamicAxis( String name, String varName ) { super( name, varName ); this.varName = varName; + axisValues.addAll(expandVariableIfPresent(getGlobalProperties())); } /** @@ -119,14 +126,7 @@ private void checkForDefaultValues() { // attempt to get the current environment variables final @Nonnull EnvVars vars = context.getBuild().getEnvironment( TaskListener.NULL ); - - // only spaces are supported as separators, as per the original axis value definition - String varValue = vars.get( varName ); - if( varValue != null ) - { - LOGGER.log( Level.FINE, "Variable value is ''{0}''", varValue); - newAxisValues.addAll(Arrays.asList(Util.tokenize(varValue))); - } + newAxisValues.addAll(expandVariableIfPresent(vars)); } catch( Exception e ) { @@ -147,6 +147,31 @@ private void checkForDefaultValues() return newAxisValues; } + private List expandVariableIfPresent( Map vars ) + { + String varValue = vars.get( varName ); + if( varValue != null ) + { + LOGGER.log( Level.FINE, "Variable value is ''{0}''", varValue); + // only spaces are supported as separators, as per the original axis value definition + return Arrays.asList(Util.tokenize(varValue)); + } + return Collections.emptyList(); + } + + private static Map getGlobalProperties() + { + Map globalProperties = new HashMap(); + for( NodeProperty nodeProperty : Jenkins.getInstance().getGlobalNodeProperties() ) + { + if( nodeProperty instanceof EnvironmentVariablesNodeProperty ) + { + globalProperties.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars()); + } + } + return globalProperties; + } + /** * Descriptor for this plugin. */ @@ -198,9 +223,13 @@ public FormValidation doCheckValueString( @QueryParameter return FormValidation.warning( Messages.configPortableName() ); } - // see if it exists in the system; if not we cannot tell if it is valid or not + // see if it exists in the system or global variables; if not we cannot tell if it is valid or not String content = System.getenv( value ); if( content == null ) + { + content = getGlobalProperties().get( value ); + } + if( content == null ) { return FormValidation.warning( Messages.configBuildVariable() ); }