Navigation Menu

Skip to content

Commit

Permalink
[FIXED JENKINS-14823] expand environment variables in Target definition
Browse files Browse the repository at this point in the history
  • Loading branch information
imod committed Dec 12, 2012
1 parent a8ce897 commit 7d4e184
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -45,6 +45,11 @@
<artifactId>config-provider-model</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>1.5.1</version>
</dependency>
</dependencies>

<repositories>
Expand Down
Expand Up @@ -50,13 +50,12 @@ public List<ManagedFile> getManagedFiles() {

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
final PrintStream logger = listener.getLogger();

if (build.getWorkspace() == null) {
throw new IllegalStateException("the workspace does not yet exist, can't provision config files - maybe slave is offline?");
}

final Map<ManagedFile, FilePath> file2Path = ManagedFileUtil.provisionConfigFiles(managedFiles, build.getWorkspace(), logger);
final Map<ManagedFile, FilePath> file2Path = ManagedFileUtil.provisionConfigFiles(managedFiles, build, listener);
// 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(file2Path));

Expand Down
Expand Up @@ -63,7 +63,7 @@ public Environment setUp(@SuppressWarnings("rawtypes") AbstractBuild build, Laun
throw new IllegalStateException("the workspace does not yet exist, can't provision config files - maybe slave is offline?");
}

final Map<ManagedFile, FilePath> file2Path = ManagedFileUtil.provisionConfigFiles(managedFiles, build.getWorkspace(), logger);
final Map<ManagedFile, FilePath> file2Path = ManagedFileUtil.provisionConfigFiles(managedFiles, build, listener);
// 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(file2Path));

Expand Down
Expand Up @@ -24,20 +24,23 @@ of this software and associated documentation files (the "Software"), to deal
package org.jenkinsci.plugins.configfiles.buildwrapper;

import hudson.FilePath;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;

public class ManagedFileUtil {

Expand Down Expand Up @@ -67,42 +70,47 @@ public FilePath call() throws IOException {
* @return a map of all the files copied, mapped to the path of the remote location, never <code>null</code>.
* @throws IOException
* @throws InterruptedException
* @throws
*/
public static Map<ManagedFile, FilePath> provisionConfigFiles(List<ManagedFile> managedFiles, FilePath workSpace, final PrintStream logger)
throws IOException, InterruptedException {
public static Map<ManagedFile, FilePath> provisionConfigFiles(List<ManagedFile> managedFiles, AbstractBuild<?, ?> build, BuildListener listener) throws IOException, InterruptedException {

final Map<ManagedFile, FilePath> file2Path = new HashMap<ManagedFile, FilePath>();
logger.println("provisoning config files...");
listener.getLogger().println("provisoning config files...");

for (ManagedFile managedFile : managedFiles) {
ConfigProvider provider = getProviderForConfigId(managedFile.fileId);

if (provider == null) {
throw new IOException(
"not able to resolve a provider responsible for the following file - maybe a config-file-provider plugin got deleted by an administrator: "
+ managedFile);
throw new IOException("not able to resolve a provider responsible for the following file - maybe a config-file-provider plugin got deleted by an administrator: " + managedFile);
}

Config configFile = provider.getConfigById(managedFile.fileId);
if (configFile == null) {
throw new IOException("not able to provide the following file, can't be resolved by any provider - maybe it got deleted by an administrator: "
+ managedFile);
throw new IOException("not able to provide the following file, can't be resolved by any provider - maybe it got deleted by an administrator: " + managedFile);
}

boolean createTempFile = StringUtils.isBlank(managedFile.targetLocation);

FilePath target = null;
if (createTempFile) {
target = ManagedFileUtil.createTempFile(workSpace.getChannel());
target = ManagedFileUtil.createTempFile(build.getWorkspace().getChannel());
} else {
String targetLocation = managedFile.targetLocation;
if (!targetLocation.contains(".")) {
targetLocation = targetLocation + "/" + configFile.name.replace(" ", "_");

String expandedTargetLocation = managedFile.targetLocation;
try {
expandedTargetLocation = TokenMacro.expandAll(build, listener, managedFile.targetLocation);
} catch (MacroEvaluationException e) {
listener.getLogger().println("[ERROR] failed to expand variables in target location '" + managedFile.targetLocation + "' : " + e.getMessage());
expandedTargetLocation = managedFile.targetLocation;
}
target = new FilePath(workSpace, targetLocation);

if (!expandedTargetLocation.contains(".")) {
expandedTargetLocation = expandedTargetLocation + "/" + configFile.name.replace(" ", "_");
}
target = new FilePath(build.getWorkspace(), expandedTargetLocation);
}

logger.println(Messages.console_output(configFile.name, target.toURI()));
listener.getLogger().println(Messages.console_output(configFile.name, target.toURI()));
ByteArrayInputStream bs = new ByteArrayInputStream(configFile.content.getBytes());
target.copyFrom(bs);
file2Path.put(managedFile, target);
Expand Down
Expand Up @@ -32,6 +32,8 @@ of this software and associated documentation files (the "Software"), to deal
import hudson.remoting.VirtualChannel;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Removes the temporarily created files at 'onComplete()' of each build, doing it at this state, ensures the files are available also for publishers.
Expand All @@ -40,6 +42,8 @@ of this software and associated documentation files (the "Software"), to deal
*/
@Extension
public class CleanTempFilesRunListener extends RunListener<AbstractBuild<?, ?>> {

private final static Logger LOGGER = Logger.getLogger(CleanTempFilesRunListener.class.getName());

@Override
public void onCompleted(AbstractBuild<?, ?> build, TaskListener listener) {
Expand All @@ -49,7 +53,7 @@ public void onCompleted(AbstractBuild<?, ?> build, TaskListener listener) {
for (CleanTempFilesAction action : actions) {
try {
for (String remotePath : action.getTempFiles()) {
listener.getLogger().println("remotePath: "+remotePath);
LOGGER.log(Level.FINE, "remotePath: "+remotePath);
try {
final Node builtOn = build.getBuiltOn();
if (builtOn != null) {
Expand Down

0 comments on commit 7d4e184

Please sign in to comment.