Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-23666] Prefer property files in workspace... #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,11 @@ public Map<String, String> getEnvVarsPropertiesJobProperty(FilePath rootPath,
Map<String, String> propertiesContent,
Map<String, String> infraEnvVarsMaster,
Map<String, String> infraEnvVarsNode) throws EnvInjectException {
final Map<String, String> resultMap = new LinkedHashMap<String, String>();
try {
if (loadFilesFromMaster) {
resultMap.putAll(Hudson.getInstance().getRootPath().act(new PropertiesVariablesRetriever(propertiesFilePath, propertiesContent, infraEnvVarsMaster, logger)));
} else {
resultMap.putAll(rootPath.act(new PropertiesVariablesRetriever(propertiesFilePath, propertiesContent, infraEnvVarsNode, logger)));
}
} catch (IOException e) {
throw new EnvInjectException(e);
} catch (InterruptedException e) {
throw new EnvInjectException(e);
if (loadFilesFromMaster) {
return new PropertiesVariablesRetriever(Hudson.getInstance().getRootPath(), propertiesFilePath, propertiesContent, infraEnvVarsMaster, logger).retrieve();
} else {
return new PropertiesVariablesRetriever(rootPath, propertiesFilePath, propertiesContent, infraEnvVarsNode, logger).retrieve();
}
return resultMap;
}

@Nonnull
Expand All @@ -64,15 +56,7 @@ public Map<String, String> getEnvVarsFileProperty(@Nonnull FilePath rootPath,
String propertiesFilePath,
Map<String, String> propertiesContent,
Map<String, String> currentEnvVars) throws EnvInjectException {
Map<String, String> resultMap = new LinkedHashMap<String, String>();
try {
resultMap.putAll(rootPath.act(new PropertiesVariablesRetriever(propertiesFilePath, propertiesContent, currentEnvVars, logger)));
} catch (IOException e) {
throw new EnvInjectException(e);
} catch (InterruptedException e) {
throw new EnvInjectException(e);
}
return resultMap;
return new PropertiesVariablesRetriever(rootPath, propertiesFilePath, propertiesContent, currentEnvVars, logger).retrieve();
}

public int executeScript(boolean loadFromMaster,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
/**
* @author Gregory Boissinot
*/
public class PropertiesVariablesRetriever implements FilePath.FileCallable<Map<String, String>> {
public class PropertiesVariablesRetriever {

private FilePath basePath;

private String propertiesFilePath;

Expand All @@ -24,61 +26,58 @@ public class PropertiesVariablesRetriever implements FilePath.FileCallable<Map<S

private EnvInjectLogger logger;

public PropertiesVariablesRetriever(String propertiesFilePath, Map<String, String> propertiesContent, Map<String, String> currentEnvVars, EnvInjectLogger logger) {
public PropertiesVariablesRetriever(FilePath basePath, String propertiesFilePath, Map<String, String> propertiesContent, Map<String, String> currentEnvVars, EnvInjectLogger logger) {
this.basePath = basePath;
this.propertiesFilePath = propertiesFilePath;
this.propertiesContent = propertiesContent;
this.currentEnvVars = currentEnvVars;
this.logger = logger;
}

public Map<String, String> invoke(File base, VirtualChannel channel) throws IOException, InterruptedException {
public Map<String, String> retrieve() throws EnvInjectException {
Map<String, String> result = new LinkedHashMap<String, String>();

try {

PropertiesLoader loader = new PropertiesLoader();
//Add the properties file
if (propertiesFilePath != null) {
String propertiesFilePathResolved = Util.replaceMacro(propertiesFilePath, currentEnvVars);
propertiesFilePathResolved = propertiesFilePathResolved.replace("\\", "/");
FilePath remotePropertiesFilePath = new FilePath(basePath, propertiesFilePathResolved);

//Add the properties file
if (propertiesFilePath != null) {
String propertiesFilePathResolved = Util.replaceMacro(propertiesFilePath, currentEnvVars);
propertiesFilePathResolved = propertiesFilePathResolved.replace("\\", "/");
File propertiesFile = getFile(base, propertiesFilePathResolved);
if (propertiesFile == null) {
try {
if (!remotePropertiesFilePath.exists()) {
String message = String.format("The given properties file path '%s' doesn't exist.", propertiesFilePathResolved);
logger.error(message);
String patternMessage = String.format("Missing file path was resolved from pattern '%s' .", propertiesFilePath);
logger.error(patternMessage);
throw new EnvInjectException(message);
}
logger.info(String.format("Injecting as environment variables the properties file path '%s'", propertiesFilePathResolved));
result.putAll(loader.getVarsFromPropertiesFile(propertiesFile, currentEnvVars));
logger.info("Variables injected successfully.");
}

//Add the properties content
if (propertiesContent != null) {
PropertiesGetter propertiesGetter = new PropertiesGetter();
logger.info(String.format("Injecting as environment variables the properties content %n%s%n", propertiesGetter.getPropertiesContentFromMapObject(propertiesContent)));
result.putAll(propertiesContent);
logger.info(String.format("Injecting as environment variables the properties file path '%s'", propertiesFilePathResolved));
result.putAll(remotePropertiesFilePath.act(new FilePath.FileCallable<Map<String, String>>() {
public Map<String, String> invoke(File propertiesFile, VirtualChannel virtualChannel) throws IOException, InterruptedException {
try {
return new PropertiesLoader().getVarsFromPropertiesFile(propertiesFile, currentEnvVars);
} catch (EnvInjectException envEx) {
throw new IOException(envEx.getMessage());
}
}
}));
logger.info("Variables injected successfully.");
} catch (IOException e) {
throw new EnvInjectException(e);
} catch (InterruptedException e) {
throw new EnvInjectException(e);
}

} catch (EnvInjectException envEx) {
throw new IOException(envEx.getMessage());
}

return result;
}

private File getFile(File base, String scriptFilePath) {

File file = new File(scriptFilePath);
if (file.exists()) {
return file;
//Add the properties content
if (propertiesContent != null) {
PropertiesGetter propertiesGetter = new PropertiesGetter();
logger.info(String.format("Injecting as environment variables the properties content %n%s%n", propertiesGetter.getPropertiesContentFromMapObject(propertiesContent)));
result.putAll(propertiesContent);
logger.info("Variables injected successfully.");
}

file = new File(base, scriptFilePath);
return file.exists() ? file : null;
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
package org.jenkinsci.plugins.envinject;

import static org.junit.Assert.assertEquals;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.FreeStyleProject;

import org.junit.Before;
Expand All @@ -34,6 +36,8 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.SingleFileSCM;

import java.io.File;

public class EnvInjectBuilderTest {

@Rule public JenkinsRule j = new JenkinsRule();
Expand All @@ -59,6 +63,26 @@ public class EnvInjectBuilderTest {
assertEquals("new", buildEnvVars().get("VAR"));
}

@Test public void propertyFileInWorkspaceShouldTakePrecedenceOverAbsolutePropertyFile() throws Exception {
File propertyFile = File.createTempFile("test", "properties", new File(System.getProperty("user.dir")));
propertyFile.deleteOnExit();
new FilePath(propertyFile).write("SOURCE=user.dir", "UTF-8");
p.setScm(new SingleFileSCM(propertyFile.getName(), "SOURCE=workspace"));
p.getBuildersList().add(new EnvInjectBuilder(propertyFile.getName(), null));

assertEquals("workspace", buildEnvVars().get("SOURCE"));
}

@Test public void injectPropertiesUsingAbsoluteFileName() throws Exception {
File propertyFile = File.createTempFile("test", "properties");
propertyFile.deleteOnExit();
new FilePath(propertyFile).write("VAR=test", "UTF-8");

p.getBuildersList().add(new EnvInjectBuilder(propertyFile.getAbsolutePath(), null));

assertEquals("test", buildEnvVars().get("VAR"));
}

private EnvVars buildEnvVars() throws Exception {
CaptureEnvironmentBuilder capture = new CaptureEnvironmentBuilder();
p.getBuildersList().add(capture);
Expand Down