Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JENKINS-40788 fail if maven settings can’t be found
  • Loading branch information
imod committed Jan 6, 2017
1 parent 515cdd8 commit 999591a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 91 deletions.
Expand Up @@ -42,8 +42,6 @@ public class MvnGlobalSettingsProvider extends GlobalSettingsProvider {

private String settingsConfigId;

private transient boolean failIfSettingsNotFound = false;

/**
* Default constructor used to load class via reflection by the maven-plugin for backward compatibility
*/
Expand All @@ -56,13 +54,6 @@ public MvnGlobalSettingsProvider(String settingsConfigId) {
this.settingsConfigId = settingsConfigId;
}

/*
* currently for testing only
*/
public void setFailIfSettingsNotFound(boolean failIfSettingsNotFound) {
this.failIfSettingsNotFound = failIfSettingsNotFound;
}

public String getSettingsConfigId() {
return settingsConfigId;
}
Expand All @@ -72,29 +63,15 @@ public void setSettingsConfigId(String settingsConfigId) {
}

@Override
public FilePath supplySettings(AbstractBuild<?, ?> originBuild, TaskListener listener) {
public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener) {
if (StringUtils.isNotBlank(settingsConfigId)) {

AbstractBuild<?, ?> build = originBuild.getRootBuild();

Config c = null;
if (build instanceof Item) {
c = ConfigFiles.getByIdOrNull((Item) build, settingsConfigId);
} else if (build.getParent() instanceof Item) {
c = ConfigFiles.getByIdOrNull((Item) build.getParent(), settingsConfigId);
} else if (build instanceof ItemGroup) {
c = ConfigFiles.getByIdOrNull((ItemGroup) build, settingsConfigId);
} else if (build.getParent() instanceof ItemGroup) {
c = ConfigFiles.getByIdOrNull((ItemGroup) build.getParent(), settingsConfigId);
}

Config c = ConfigFiles.getByIdOrNull(build.getRootBuild(), settingsConfigId);

if (c == null) {
String msg = "your Apache Maven build is setup to use a global config with id " + settingsConfigId + " but can not find the config";
listener.getLogger().println("ERROR: " + msg);
if (failIfSettingsNotFound) {
throw new IllegalStateException(msg);
}
throw new IllegalStateException(msg);
} else {

GlobalMavenSettingsConfig config;
Expand All @@ -109,28 +86,33 @@ public FilePath supplySettings(AbstractBuild<?, ?> originBuild, TaskListener lis
if (StringUtils.isNotBlank(config.content)) {
try {

FilePath workDir = ManagedFileUtil.tempDir(build.getWorkspace());
String fileContent = config.content;
FilePath workspace = build.getWorkspace();
if (workspace != null) {
FilePath workDir = ManagedFileUtil.tempDir(workspace);
String fileContent = config.content;

final Map<String, StandardUsernameCredentials> resolvedCredentials = CredentialsHelper.resolveCredentials(build, config.getServerCredentialMappings());
final Boolean isReplaceAll = config.getIsReplaceAll();
final Map<String, StandardUsernameCredentials> resolvedCredentials = CredentialsHelper.resolveCredentials(build, config.getServerCredentialMappings());
final Boolean isReplaceAll = config.getIsReplaceAll();

if (!resolvedCredentials.isEmpty()) {
List<String> tempFiles = new ArrayList<String>();
fileContent = CredentialsHelper.fillAuthentication(fileContent, isReplaceAll, resolvedCredentials, workDir, tempFiles);
for (String tempFile : tempFiles) {
build.addAction(new CleanTempFilesAction(tempFile));
if (resolvedCredentials != null && !resolvedCredentials.isEmpty()) {
List<String> tempFiles = new ArrayList<String>();
fileContent = CredentialsHelper.fillAuthentication(fileContent, isReplaceAll, resolvedCredentials, workDir, tempFiles);
for (String tempFile : tempFiles) {
build.addAction(new CleanTempFilesAction(tempFile));
}
}
}

FilePath configurationFile = build.getWorkspace().createTextTempFile("global-settings", ".xml", fileContent, false);
LOGGER.log(Level.FINE, "Create {0}", new Object[]{configurationFile});
build.getEnvironments().add(new SimpleEnvironment("MVN_GLOBALSETTINGS", configurationFile.getRemote()));
FilePath configurationFile = workspace.createTextTempFile("global-settings", ".xml", fileContent, false);
LOGGER.log(Level.FINE, "Create {0}", new Object[]{configurationFile});
build.getEnvironments().add(new SimpleEnvironment("MVN_GLOBALSETTINGS", configurationFile.getRemote()));

// Temporarily attach info about the files to be deleted to the build - this action gets removed from the build again by
// 'org.jenkinsci.plugins.configfiles.common.CleanTempFilesRunListener'
build.addAction(new CleanTempFilesAction(configurationFile.getRemote()));
return configurationFile;
// Temporarily attach info about the files to be deleted to the build - this action gets removed from the build again by
// 'org.jenkinsci.plugins.configfiles.common.CleanTempFilesRunListener'
build.addAction(new CleanTempFilesAction(configurationFile.getRemote()));
return configurationFile;
} else {
listener.getLogger().println("ERROR: can't supply maven settings, workspace is null / slave seems not contected...");
}
} catch (Exception e) {
throw new IllegalStateException("the global settings.xml could not be supplied for the current build: " + e.getMessage());
}
Expand Down
Expand Up @@ -43,8 +43,6 @@ public class MvnSettingsProvider extends SettingsProvider {

private String settingsConfigId;

private transient boolean failIfSettingsNotFound = false;

/**
* Default constructor used to load class via reflection by the maven-plugin for backward compatibility
*/
Expand All @@ -57,13 +55,6 @@ public MvnSettingsProvider(String settingsConfigId) {
this.settingsConfigId = settingsConfigId;
}

/*
* currently for testing only
*/
public void setFailIfSettingsNotFound(boolean failIfSettingsNotFound) {
this.failIfSettingsNotFound = failIfSettingsNotFound;
}

public String getSettingsConfigId() {
return settingsConfigId;
}
Expand All @@ -73,29 +64,16 @@ public void setSettingsConfigId(String settingsConfigId) {
}

@Override
public FilePath supplySettings(AbstractBuild<?, ?> originBuild, TaskListener listener) {

AbstractBuild<?, ?> build = originBuild.getRootBuild();
public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener) {

if (StringUtils.isNotBlank(settingsConfigId)) {

Config c = null;
if (build instanceof Item) {
c = ConfigFiles.getByIdOrNull((Item) build, settingsConfigId);
} else if (build.getParent() instanceof Item) {
c = ConfigFiles.getByIdOrNull((Item) build.getParent(), settingsConfigId);
} else if (build instanceof ItemGroup) {
c = ConfigFiles.getByIdOrNull((ItemGroup) build, settingsConfigId);
} else if (build.getParent() instanceof ItemGroup) {
c = ConfigFiles.getByIdOrNull((ItemGroup) build.getParent(), settingsConfigId);
}
Config c = ConfigFiles.getByIdOrNull(build.getRootBuild(), settingsConfigId);

if (c == null) {
String msg = "your Apache Maven build is setup to use a config with id " + settingsConfigId + " but can not find the config";
listener.getLogger().println("ERROR: " + msg);
if (failIfSettingsNotFound) {
throw new IllegalStateException(msg);
}
throw new IllegalStateException(msg);
} else {

MavenSettingsConfig config;
Expand All @@ -108,32 +86,36 @@ public FilePath supplySettings(AbstractBuild<?, ?> originBuild, TaskListener lis
listener.getLogger().println("using settings config with name " + config.name);
listener.getLogger().println("Replacing all maven server entries not found in credentials list is " + config.getIsReplaceAll());
if (StringUtils.isNotBlank(config.content)) {
FilePath workDir = ManagedFileUtil.tempDir(build.getWorkspace());

try {

String fileContent = config.content;

final List<ServerCredentialMapping> serverCredentialMappings = config.getServerCredentialMappings();
final Map<String, StandardUsernameCredentials> resolvedCredentials = CredentialsHelper.resolveCredentials(build, serverCredentialMappings);
final Boolean isReplaceAll = config.getIsReplaceAll();

if (!resolvedCredentials.isEmpty()) {
List<String> tempFiles = new ArrayList<String>();
fileContent = CredentialsHelper.fillAuthentication(fileContent, isReplaceAll, resolvedCredentials, workDir, tempFiles);
for (String tempFile : tempFiles) {
build.addAction(new CleanTempFilesAction(tempFile));
FilePath workspace = build.getWorkspace();
if (workspace != null) {
FilePath workDir = ManagedFileUtil.tempDir(workspace);
String fileContent = config.content;

final List<ServerCredentialMapping> serverCredentialMappings = config.getServerCredentialMappings();
final Map<String, StandardUsernameCredentials> resolvedCredentials = CredentialsHelper.resolveCredentials(build, serverCredentialMappings);
final Boolean isReplaceAll = config.getIsReplaceAll();

if (!resolvedCredentials.isEmpty()) {
List<String> tempFiles = new ArrayList<String>();
fileContent = CredentialsHelper.fillAuthentication(fileContent, isReplaceAll, resolvedCredentials, workDir, tempFiles);
for (String tempFile : tempFiles) {
build.addAction(new CleanTempFilesAction(tempFile));
}
}
}

final FilePath f = build.getWorkspace().createTextTempFile("settings", ".xml", fileContent, false);
LOGGER.log(Level.FINE, "Create {0}", new Object[]{f});
build.getEnvironments().add(new SimpleEnvironment("MVN_SETTINGS", f.getRemote()));
final FilePath f = workspace.createTextTempFile("settings", ".xml", fileContent, false);
LOGGER.log(Level.FINE, "Create {0}", new Object[]{f});
build.getEnvironments().add(new SimpleEnvironment("MVN_SETTINGS", f.getRemote()));

// Temporarily attach info about the files to be deleted to the build - this action gets removed from the build again by
// 'org.jenkinsci.plugins.configfiles.common.CleanTempFilesRunListener'
build.addAction(new CleanTempFilesAction(f.getRemote()));
return f;
// Temporarily attach info about the files to be deleted to the build - this action gets removed from the build again by
// 'org.jenkinsci.plugins.configfiles.common.CleanTempFilesRunListener'
build.addAction(new CleanTempFilesAction(f.getRemote()));
return f;
} else {
listener.getLogger().println("ERROR: can't supply maven settings, workspace is null / slave seems not contected...");
}
} catch (Exception e) {
throw new IllegalStateException("the settings.xml could not be supplied for the current build: " + e.getMessage(), e);
}
Expand Down
Expand Up @@ -135,14 +135,31 @@ public void mavenSettingsMustBeFoundInFreestyleProject() throws Exception {
Config c2 = createSetting(globalMavenSettingsConfigProvider);

MvnSettingsProvider s1 = new MvnSettingsProvider(c1.id);
s1.setFailIfSettingsNotFound(true);
MvnGlobalSettingsProvider s2 = new MvnGlobalSettingsProvider(c2.id);
s2.setFailIfSettingsNotFound(true);

Maven m = new Maven("clean", mvnName, null, null, null, false, s1, s2);
p.getBuildersList().add(m);
p.setScm(new ExtractResourceSCM(getClass().getResource("/maven3-project.zip")));

jenkins.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0, new Cause.UserCause()).get());
}

@Test
@Bug(40737)
public void notFoundMavenSettingsMustCauseBuildToFail() throws Exception {
jenkins.jenkins.getInjector().injectMembers(this);

final FreeStyleProject p = jenkins.createFreeStyleProject();

String mvnName = ToolInstallations.configureMaven3().getName();

MvnSettingsProvider s1 = new MvnSettingsProvider("dummyId");
MvnGlobalSettingsProvider s2 = new MvnGlobalSettingsProvider("dummyGlobalId");

Maven m = new Maven("clean", mvnName, null, null, null, false, s1, s2);
p.getBuildersList().add(m);
p.setScm(new ExtractResourceSCM(getClass().getResource("/maven3-project.zip")));

jenkins.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0, new Cause.UserCause()).get());
}
}

0 comments on commit 999591a

Please sign in to comment.