Skip to content
Permalink
Browse files
Don't remove empty build vars
By utilizing the EnvVars.overrideAll(Map) method, it internally removes keys where the value is null or an empty string. This results in those parameters not being rewritten to empty properties as expected and the properties instead have the jenkins replacement string in them instead.

[FIXES JENKINS-45300]
  • Loading branch information
wolfs committed Aug 13, 2017
1 parent c078d22 commit e64b4dd9e90fe365fa5f190f05f587d5f0ac0a9b
@@ -170,16 +170,16 @@ public void setPassAllAsSystemProperties(boolean passAllAsSystemProperties) {
public String getProjectProperties() {
return projectProperties;
}
@DataBoundSetter

@DataBoundSetter
public void setProjectProperties(String projectProperties) {
this.projectProperties = projectProperties;
}

public boolean isPassAllAsProjectProperties() {
return passAllAsProjectProperties;
}

@DataBoundSetter
public void setPassAllAsProjectProperties(boolean passAllAsProjectProperties) {
this.passAllAsProjectProperties = passAllAsProjectProperties;
@@ -194,12 +194,16 @@ public GradleInstallation getGradle() {
return null;
}

/** Turns a null string into a blanck string. */
/**
* Turns a null string into a blanck string.
*/
private static String null2Blank(String input) {
return input != null ? input : "";
}

/** Appends text to a possibly null string. */
/**
* Appends text to a possibly null string.
*/
private static String append(String input, String textToAppend) {
if (StringUtils.isBlank(input)) {
return null2Blank(textToAppend);
@@ -228,26 +232,25 @@ private boolean performTask(boolean dryRun, AbstractBuild<?, ?> build, Launcher
gradleLogger.info("Launching build.");

EnvVars env = build.getEnvironment(listener);
env.overrideAll(build.getBuildVariables());
final VariableResolver<String> resolver = new VariableResolver.ByMap<>(env);
VariableResolver.Union<String> resolver = new VariableResolver.Union<>(new VariableResolver.ByMap<>(env), build.getBuildVariableResolver());

//Switches
String normalizedSwitches = getNormalized(switches, env, "GRADLE_EXT_SWITCHES");
String normalizedSwitches = getNormalized(switches, resolver, "GRADLE_EXT_SWITCHES");

//Add dry-run switch if needed
if (dryRun) {
normalizedSwitches = normalizedSwitches + " --dry-run";
}

//Tasks
String normalizedTasks = getNormalized(tasks, env, "GRADLE_EXT_TASKS");
String normalizedTasks = getNormalized(tasks, resolver, "GRADLE_EXT_TASKS");

FilePath normalizedRootBuildScriptDir = getNormalizedRootBuildScriptDir(build, env);
FilePath normalizedRootBuildScriptDir = getNormalizedRootBuildScriptDir(build, resolver);

//Build arguments
ArgumentListBuilder args = new ArgumentListBuilder();
if (useWrapper) {
FilePath gradleWrapperFile = findGradleWrapper(normalizedRootBuildScriptDir, build, launcher, listener, env);
FilePath gradleWrapperFile = findGradleWrapper(normalizedRootBuildScriptDir, build, launcher, listener, resolver);
if (gradleWrapperFile == null) {
return false;
}
@@ -294,7 +297,7 @@ private boolean performTask(boolean dryRun, AbstractBuild<?, ?> build, Launcher
args.addTokenized(normalizedSwitches);
args.addTokenized(normalizedTasks);
if (StringUtils.isNotBlank(buildFile)) {
String buildFileNormalized = env.expand(buildFile.trim());
String buildFileNormalized = Util.replaceMacro(buildFile.trim(), resolver);
args.add("-b");
args.add(buildFileNormalized);
}
@@ -350,8 +353,8 @@ private boolean performTask(boolean dryRun, AbstractBuild<?, ?> build, Launcher
}
}

private FilePath findGradleWrapper(FilePath normalizedRootBuildScriptDir, AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, EnvVars env) throws IOException, InterruptedException {
List<FilePath> possibleWrapperLocations = getPossibleWrapperLocations(build, launcher, env, normalizedRootBuildScriptDir);
private FilePath findGradleWrapper(FilePath normalizedRootBuildScriptDir, AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, VariableResolver<String> resolver) throws IOException, InterruptedException {
List<FilePath> possibleWrapperLocations = getPossibleWrapperLocations(build, launcher, resolver, normalizedRootBuildScriptDir);
String execName = (launcher.isUnix()) ? GradleInstallation.UNIX_GRADLE_WRAPPER_COMMAND : GradleInstallation.WINDOWS_GRADLE_WRAPPER_COMMAND;
FilePath gradleWrapperFile = null;
for (FilePath possibleWrapperLocation : possibleWrapperLocations) {
@@ -367,34 +370,34 @@ private FilePath findGradleWrapper(FilePath normalizedRootBuildScriptDir, Abstra
return gradleWrapperFile;
}

private FilePath getNormalizedRootBuildScriptDir(AbstractBuild<?, ?> build, EnvVars env) {
private FilePath getNormalizedRootBuildScriptDir(AbstractBuild<?, ?> build, VariableResolver<String> resolver) {
FilePath normalizedRootBuildScriptDir = null;
if (rootBuildScriptDir != null && rootBuildScriptDir.trim().length() != 0) {
String rootBuildScriptNormalized = replaceWhitespaceBySpace(rootBuildScriptDir.trim());
rootBuildScriptNormalized = env.expand(rootBuildScriptNormalized.trim());
rootBuildScriptNormalized = Util.replaceMacro(rootBuildScriptNormalized.trim(), resolver);
normalizedRootBuildScriptDir = new FilePath(build.getModuleRoot(), rootBuildScriptNormalized);
}
return normalizedRootBuildScriptDir;
}

private String getNormalized(String args, EnvVars env, String contributingEnvironmentVariable) {
String extraArgs = env.get(contributingEnvironmentVariable);
private String getNormalized(String args, VariableResolver<String> resolver, String contributingEnvironmentVariable) {
String extraArgs = resolver.resolve(contributingEnvironmentVariable);
String normalizedArgs = append(args, extraArgs);
normalizedArgs = replaceWhitespaceBySpace(normalizedArgs);
normalizedArgs = env.expand(normalizedArgs);
normalizedArgs = Util.replaceMacro(normalizedArgs, resolver);
return normalizedArgs;
}

private String replaceWhitespaceBySpace(String argument) {
return argument.replaceAll("[\t\r\n]+", " ");
}

private List<FilePath> getPossibleWrapperLocations(AbstractBuild<?, ?> build, Launcher launcher, EnvVars env, FilePath normalizedRootBuildScriptDir) throws IOException, InterruptedException {
private List<FilePath> getPossibleWrapperLocations(AbstractBuild<?, ?> build, Launcher launcher, VariableResolver<String> resolver, FilePath normalizedRootBuildScriptDir) throws IOException, InterruptedException {
FilePath moduleRoot = build.getModuleRoot();
if (wrapperLocation != null && wrapperLocation.trim().length() != 0) {
// Override with provided relative path to gradlew
String wrapperLocationNormalized = wrapperLocation.trim().replaceAll("[\t\r\n]+", "");
wrapperLocationNormalized = env.expand(wrapperLocationNormalized.trim());
wrapperLocationNormalized = Util.replaceMacro(wrapperLocationNormalized.trim(), resolver);
return ImmutableList.of(new FilePath(moduleRoot, wrapperLocationNormalized));
} else if (buildFile != null && !buildFile.isEmpty()) {
// Check if the target project is located not at the root dir
@@ -425,7 +428,8 @@ public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}

@Extension @Symbol("gradle")
@Extension
@Symbol("gradle")
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {

@CopyOnWrite
@@ -1,5 +1,13 @@
package hudson.plugins.gradle

import hudson.model.Cause
import hudson.model.FreeStyleBuild
import hudson.model.FreeStyleProject
import hudson.model.ParametersAction
import hudson.model.ParametersDefinitionProperty
import hudson.model.TextParameterDefinition
import hudson.model.TextParameterValue
import hudson.model.queue.QueueTaskFuture
import org.junit.Rule
import org.junit.rules.RuleChain
import org.jvnet.hudson.test.JenkinsRule
@@ -14,4 +22,13 @@ class AbstractIntegrationTest extends Specification {
Map getDefaults() {
[gradleName: gradleInstallationRule.gradleVersion, useWorkspaceAsHome: true, switches: '--no-daemon']
}

static QueueTaskFuture<FreeStyleBuild> triggerBuildWithParameter(FreeStyleProject p, String parameterName, String value) {
p.scheduleBuild2(0, new Cause.UserIdCause(), new ParametersAction(new TextParameterValue(parameterName, value)))
}

static addParameter(FreeStyleProject p, String parameterName) {
p.addProperty(new ParametersDefinitionProperty(new TextParameterDefinition(parameterName, null, null)))
}

}
@@ -150,6 +150,27 @@ class GradlePluginIntegrationTest extends AbstractIntegrationTest {
'build/some/build.gradle' | [rootBuildScriptDir: 'build'] | [null]
}

def "empty parameters are replaced"() {
given:
gradleInstallationRule.addInstallation()
def p = j.createFreeStyleProject()
p.buildersList.add(new CreateFileBuilder('build.gradle', """
task printParameter {
doLast {
println "PASSED_PARAMETER='\$passedParameter'"
}
}
""".stripIndent()))
p.buildersList.add(new Gradle(defaults + [tasks: 'printParameter -PpassedParameter=$PASSED_PARAMETER']))
addParameter(p, 'PASSED_PARAMETER')

when:
def build = triggerBuildWithParameter(p, 'PASSED_PARAMETER', '').get()

then:
getLog(build).contains("PASSED_PARAMETER=''")
}

def "Config roundtrip"() {
given:
gradleInstallationRule.addInstallation()
@@ -2,14 +2,7 @@ package hudson.plugins.gradle

import static org.jvnet.hudson.test.JenkinsRule.getLog

import hudson.model.Cause
import hudson.model.FreeStyleBuild
import hudson.model.FreeStyleProject
import hudson.model.ParametersAction
import hudson.model.ParametersDefinitionProperty
import hudson.model.TextParameterDefinition
import hudson.model.TextParameterValue
import hudson.model.queue.QueueTaskFuture
import hudson.remoting.Launcher
import org.jvnet.hudson.test.CreateFileBuilder
import spock.lang.Unroll
@@ -139,12 +132,4 @@ class PropertyPassingIntegrationTest extends AbstractIntegrationTest {
private static String map2PropertiesString(Map<String, String> properties) {
(properties.collect { k, v -> "${k}=${v}\n" }).join('')
}

private static QueueTaskFuture<FreeStyleBuild> triggerBuildWithParameter(FreeStyleProject p, String parameterName, String value) {
p.scheduleBuild2(0, new Cause.UserIdCause(), new ParametersAction(new TextParameterValue(parameterName, value)))
}

private static addParameter(FreeStyleProject p, String parameterName) {
p.addProperty(new ParametersDefinitionProperty(new TextParameterDefinition(parameterName, null, null)))
}
}

0 comments on commit e64b4dd

Please sign in to comment.