Permalink
Browse files

add build wrapper environment variables

prefix all « docker exec » commands with env to inject vars set by build wrappers.
  • Loading branch information...
ndeloof committed Sep 18, 2015
1 parent 06bb716 commit e8dd7e46a6487a3b43c7e38dcedcf0ca0752e9d4
@@ -1,16 +1,10 @@
package com.cloudbees.jenkins.plugins.docker_build_env;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.BuildBadgeAction;
import hudson.model.EnvironmentContributingAction;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.SCMListener;
import hudson.scm.ChangeLogSet;
import hudson.scm.SCM;
import javax.annotation.Nonnull;
import java.io.IOException;
@@ -16,9 +16,12 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
@@ -233,28 +236,45 @@ private String getDocker0Ip(Launcher launcher, String image) throws IOException,
public void executeIn(String container, String userId, Launcher.ProcStarter starter) throws IOException, InterruptedException {
List<String> originalCmds = starter.cmds();
List<String> prefix = dockerCommandArgs();
prefix.add("exec");
prefix.add("--tty");
prefix.add("--user");
prefix.add(userId);
prefix.add(container);
prefix.add("env");
// Build a list of environment, hidding node's one
Set<String> envs = new TreeSet<String>(Arrays.asList(starter.envs()));
for (String key : Computer.currentComputer().buildEnvironment(listener).keySet()) {
envs.remove(key);
}
prefix.addAll(envs);
ArgumentListBuilder args = dockerCommand()
.add("exec", "--tty")
.add("--user", userId)
.add(container);
boolean[] originalMask = starter.masks();
for (int i = 0; i < originalCmds.size(); i++) {
boolean masked = originalMask == null ? false : i < originalMask.length ? originalMask[i] : false;
args.add(originalCmds.get(i), masked);
starter.cmds().addAll(0, prefix);
if (starter.masks() != null) {
boolean[] masks = new boolean[starter.masks().length + prefix.size()];
System.arraycopy(starter.masks(), 0, masks, prefix.size(), starter.masks().length);
starter.masks(masks);
}
starter.cmds(args);
starter.envs(getEnvVars());
}
private ArgumentListBuilder dockerCommand() {
ArgumentListBuilder args = new ArgumentListBuilder()
.add(dockerExecutable);
ArgumentListBuilder args = new ArgumentListBuilder();
for (String s : dockerCommandArgs()) {
args.add(s);
}
return args;
}
private List<String> dockerCommandArgs() {
List<String> args = new ArrayList<String>();
args.add(dockerExecutable);
if (dockerHost.getUri() != null) {
args.add("-H", dockerHost.getUri());
args.add("-H");
args.add(dockerHost.getUri());
}
return args;
}
@@ -6,10 +6,15 @@
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.*;
import hudson.model.listeners.RunListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.remoting.Callable;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
@@ -31,6 +36,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Decorate Launcher so that every command executed by a build step is actually ran inside docker container.
@@ -189,30 +196,12 @@ private String startBuildContainer(BuiltInContainer runInContainer, AbstractBuil
* environment, that may not make any sense inside container (consider <code>PATH</code> for sample).
*/
private EnvVars buildContainerEnvironment(AbstractBuild build, BuildListener listener) throws IOException, InterruptedException {
EnvVars env = new EnvVars();
FilePath ws = build.getWorkspace();
if (ws!=null) // ?
env.put("WORKSPACE", ws.getRemote());
env.putAll(build.getCharacteristicEnvVars());
for (hudson.model.Environment environment : build.getEnvironments()) {
environment.buildEnvVars(env);
EnvVars env = build.getEnvironment(listener);
for (String key : Computer.currentComputer().buildEnvironment(listener).keySet()) {
env.remove(key);
}
final Job job = build.getParent();
if (job instanceof AbstractProject) {
((AbstractProject) job).getScm().buildEnvVars(build, env);
}
for (EnvironmentContributingAction a : build.getActions(EnvironmentContributingAction.class))
a.buildEnvVars(build, env);
for (EnvironmentContributor ec : EnvironmentContributor.all().reverseView())
ec.buildEnvironmentFor(build, env, listener);
LOGGER.log(Level.FINE, "reduced environment: {0}", env);
EnvVars.resolve(env);
return env;
}
@@ -272,6 +261,8 @@ public String call() {
};
private static final Logger LOGGER = Logger.getLogger(DockerBuildWrapper.class.getName());
// --- backward compatibility
private transient boolean exposeDocker;
@@ -1,15 +1,13 @@
package com.cloudbees.jenkins.plugins.docker_build_env;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Proc;
import hudson.model.AbstractBuild;
import hudson.remoting.VirtualChannel;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
@@ -6,18 +6,11 @@
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import jenkins.MasterToSlaveFileCallable;
import org.apache.commons.io.FileUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Collections;
import java.util.Formatter;
/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
@@ -5,7 +5,6 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
/**

0 comments on commit e8dd7e4

Please sign in to comment.