Uploaded image for project: 'Bamboo Data Center'
  1. Bamboo Data Center
  2. BAM-2933

Ability to substitute to existing system variables for the System Environment field

    • Icon: Suggestion Suggestion
    • Resolution: Fixed
    • 2.1.5, 2.2
    • Builds
    • Our product teams collect and evaluate feedback from a number of different sources. To learn more about how we use customer feedback in the planning process, check out our new feature policy.

      Users can't add to existing system variables while executing a build;

      For instance Path=C:\bea\;%Path% doesn't work, it overrides the existing Path variable.

            [BAM-2933] Ability to substitute to existing system variables for the System Environment field

            Added new feature for variable substitution. All variables of type ${system.XYZ} will be substituted using value of system property XYZ or environment variable XYZ.

            Krystian Brazulewicz added a comment - Added new feature for variable substitution. All variables of type ${system.XYZ} will be substituted using value of system property XYZ or environment variable XYZ.

            You may want to do some more error handling (if a user types something not expected). For example, I had typed in:

            PATH=C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE;%PATH%

            and also tried:

            PATH="C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE";%PATH%

            Both times, the error message I got when I ran the build was:

            java.lang.ArrayIndexOutOfBoundsException: 1
            at com.atlassian.bamboo.plugin.builder.nant.AbstractDotNetBuilder.getEnvironmentSetting(AbstractDotNetBuilder.java:360)
            at com.atlassian.bamboo.command.Command.executeCommand(Command.java:61)
            at com.atlassian.bamboo.builder.AbstractBuilder.runBuild(AbstractBuilder.java:267)
            at com.atlassian.bamboo.builder.AbstractBuilder.executeBuild(AbstractBuilder.java:232)
            at com.atlassian.bamboo.build.pipeline.tasks.ExecuteBuildTask.call(ExecuteBuildTask.java:67)
            ........

            Rafael Campana added a comment - You may want to do some more error handling (if a user types something not expected). For example, I had typed in: PATH=C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE;%PATH% and also tried: PATH="C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE";%PATH% Both times, the error message I got when I ran the build was: java.lang.ArrayIndexOutOfBoundsException: 1 at com.atlassian.bamboo.plugin.builder.nant.AbstractDotNetBuilder.getEnvironmentSetting(AbstractDotNetBuilder.java:360) at com.atlassian.bamboo.command.Command.executeCommand(Command.java:61) at com.atlassian.bamboo.builder.AbstractBuilder.runBuild(AbstractBuilder.java:267) at com.atlassian.bamboo.builder.AbstractBuilder.executeBuild(AbstractBuilder.java:232) at com.atlassian.bamboo.build.pipeline.tasks.ExecuteBuildTask.call(ExecuteBuildTask.java:67) ........

            AjayA added a comment -

            AjayA added a comment - Corresponding forum issue http://forums.atlassian.com/thread.jspa?messageID=257285834&#257285834

            Ross Rowe added a comment -

            Hi Ajay, I've included an interim fix into the NAnt builder (version 2.1.4) to add this functionality for the .NET related builds. Here's the logic I used, it should be pretty straightforward to work it into the com.atlassian.bamboo.command.Command.getEnvironmentSetting() method.

                    private static final String WINDOWS_VARIABLE_REPLACE = "\\%.+\\%";
            	private static final String UNIX_VARIABLE_REPLACE = "\\$\\w+";
            	private static final String UNIX_VARIABLE_REPLACE_MATCH = ".+$*";
            	private static final String WINDOWS_VARIABLE_REPLACE_MATCH = ".+\\%.+";
            
            /**
            	 * Handles the replacement of environment variables. The method invokes the
            	 * superclass implementation first, then iterates over the array. For each
            	 * item, we check to see if the variable is a replacement parameter (ie. on
            	 * Windows, check to see if the string includes %, on Unix, check to see if
            	 * the string includes $).
            	 * 
            	 * If a replacement parameter exists, then the {@link SystemProperty} class
            	 * is used to retrieve the environment variable value for the key, and the
            	 * replacement parameter is replaced with this value.
            	 * 
            	 * @param vmParams list of environment variables specified on the build plan
            	 * @param javaHome JAVA_HOME variable value
            	 * @return array of Strings representing the environment variables to be used
            	 */
            	@Override
            	public String[] getEnvironmentSetting(String vmParams, String javaHome) {
            		String[] results = super.getEnvironmentSetting(vmParams, javaHome);
            		String[] replacedResults = new String[results.length];
            		// iterate over results
            		int current = 0;
            		for (String environmentVariable : results) {
            			// split on =
            			String[] keyValue = environmentVariable.split("=");
            			String key = keyValue[0];
            			String value = keyValue[1];
            			// does value contain replacement parameter?
            			String replacementRegex = (isWindowsPlatform()) ? WINDOWS_VARIABLE_REPLACE_MATCH
            					: UNIX_VARIABLE_REPLACE_MATCH;
            			if (value.matches(replacementRegex)) {
            				// if so, attempt to find property for key
            				SystemProperty property;
            				if (key.equalsIgnoreCase(SystemProperty.PATH.getKey()))
            					property = SystemProperty.PATH;
            				else
            					property = new SystemProperty(key.toUpperCase(), true);
            				if (property.exists()) {
            					// replace replacement parameter with actual value
            					String existingValue = property.getValue("");
            					String variableReplaceRegex = (isWindowsPlatform()) ? WINDOWS_VARIABLE_REPLACE
            							: UNIX_VARIABLE_REPLACE;
            					value = value.replaceAll(variableReplaceRegex, Matcher
            							.quoteReplacement(existingValue));
            				}
            			}
            			replacedResults[current] = key + "=" + value;
            		}
            
            		return replacedResults;
            	}
            

            Ross Rowe added a comment - Hi Ajay, I've included an interim fix into the NAnt builder (version 2.1.4) to add this functionality for the .NET related builds. Here's the logic I used, it should be pretty straightforward to work it into the com.atlassian.bamboo.command.Command.getEnvironmentSetting() method. private static final String WINDOWS_VARIABLE_REPLACE = "\\%.+\\%" ; private static final String UNIX_VARIABLE_REPLACE = "\\$\\w+" ; private static final String UNIX_VARIABLE_REPLACE_MATCH = ".+$*" ; private static final String WINDOWS_VARIABLE_REPLACE_MATCH = ".+\\%.+" ; /** * Handles the replacement of environment variables. The method invokes the * superclass implementation first, then iterates over the array. For each * item, we check to see if the variable is a replacement parameter (ie. on * Windows, check to see if the string includes %, on Unix, check to see if * the string includes $). * * If a replacement parameter exists, then the {@link SystemProperty} class * is used to retrieve the environment variable value for the key, and the * replacement parameter is replaced with this value. * * @param vmParams list of environment variables specified on the build plan * @param javaHome JAVA_HOME variable value * @ return array of Strings representing the environment variables to be used */ @Override public String [] getEnvironmentSetting( String vmParams, String javaHome) { String [] results = super .getEnvironmentSetting(vmParams, javaHome); String [] replacedResults = new String [results.length]; // iterate over results int current = 0; for ( String environmentVariable : results) { // split on = String [] keyValue = environmentVariable.split( "=" ); String key = keyValue[0]; String value = keyValue[1]; // does value contain replacement parameter? String replacementRegex = (isWindowsPlatform()) ? WINDOWS_VARIABLE_REPLACE_MATCH : UNIX_VARIABLE_REPLACE_MATCH; if (value.matches(replacementRegex)) { // if so, attempt to find property for key SystemProperty property; if (key.equalsIgnoreCase(SystemProperty.PATH.getKey())) property = SystemProperty.PATH; else property = new SystemProperty(key.toUpperCase(), true ); if (property.exists()) { // replace replacement parameter with actual value String existingValue = property.getValue(""); String variableReplaceRegex = (isWindowsPlatform()) ? WINDOWS_VARIABLE_REPLACE : UNIX_VARIABLE_REPLACE; value = value.replaceAll(variableReplaceRegex, Matcher .quoteReplacement(existingValue)); } } replacedResults[current] = key + "=" + value; } return replacedResults; }

              kbrazulewicz Krystian Brazulewicz
              asridhar AjayA
              Votes:
              2 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: