diff --git a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/Docker.java b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/Docker.java index dd842c3..30df25b 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/Docker.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/Docker.java @@ -26,6 +26,7 @@ import java.net.InterfaceAddress; import java.net.NetworkInterface; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; @@ -47,8 +48,9 @@ public class Docker implements Closeable { private final boolean privileged; private final AbstractBuild build; private EnvVars envVars; + private List environmentFilters; - public Docker(DockerServerEndpoint dockerHost, String dockerInstallation, String credentialsId, AbstractBuild build, Launcher launcher, TaskListener listener, boolean verbose, boolean privileged) throws IOException, InterruptedException { + public Docker(DockerServerEndpoint dockerHost, String dockerInstallation, String credentialsId, AbstractBuild build, Launcher launcher, TaskListener listener, boolean verbose, boolean privileged, String filterEnvVariables) throws IOException, InterruptedException { this.dockerHost = dockerHost; this.dockerExecutable = DockerTool.getExecutable(dockerInstallation, Computer.currentComputer().getNode(), listener, build.getEnvironment(listener)); this.registryEndpoint = new DockerRegistryEndpoint(null, credentialsId); @@ -57,6 +59,7 @@ public Docker(DockerServerEndpoint dockerHost, String dockerInstallation, String this.build = build; this.verbose = verbose | debug; this.privileged = privileged; + this.environmentFilters = getEnvFilters(filterEnvVariables); } @@ -95,6 +98,13 @@ private EnvVars getEnvVars() throws IOException, InterruptedException { return envVars; } + private List getEnvFilters(String envFilters){ + if(envFilters != null && !envFilters.equals("")) { + return Arrays.asList(envFilters.split(",")); + } + return Arrays.asList(); + } + public boolean pullImage(String image) throws IOException, InterruptedException { ArgumentListBuilder args = dockerCommand() .add("pull", image); @@ -332,7 +342,17 @@ public EnvVars getEnv(String container, Launcher launcher) throws IOException, I EnvVars env = new EnvVars(); LineIterator it = new LineIterator(new StringReader(out.toString())); while (it.hasNext()) { - env.addLine(it.nextLine()); + boolean found = false; + String envV = it.nextLine(); + for(String filter: environmentFilters) { + if(!filter.equals("") && envV.contains(filter + "=")) { + found = true; + break; + } + } + if(!found) { + env.addLine(envV); + } } return env; } diff --git a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper.java b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper.java index fec43d3..a6841f7 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper.java @@ -73,13 +73,15 @@ public class DockerBuildWrapper extends BuildWrapper { private String cpu; + private String filterEnvVariables; + private final boolean noCache; @DataBoundConstructor public DockerBuildWrapper(DockerImageSelector selector, String dockerInstallation, DockerServerEndpoint dockerHost, String dockerRegistryCredentials, boolean verbose, boolean privileged, List volumes, String group, String command, boolean forcePull, - String net, String memory, String cpu, boolean noCache) { + String net, String memory, String cpu, String filterEnvVariables, boolean noCache) { this.selector = selector; this.dockerInstallation = dockerInstallation; this.dockerHost = dockerHost; @@ -94,6 +96,7 @@ public DockerBuildWrapper(DockerImageSelector selector, String dockerInstallatio this.memory = memory; this.cpu = cpu; this.noCache = noCache; + this.filterEnvVariables = filterEnvVariables; } public DockerImageSelector getSelector() { @@ -142,13 +145,17 @@ public boolean isForcePull() { public String getCpu() { return cpu;} + public String getFilterEnvVariables() { + return filterEnvVariables; + } + public boolean isNoCache() { return noCache; } @Override public Launcher decorateLauncher(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws IOException, InterruptedException, Run.RunnerAbortedException { - final Docker docker = new Docker(dockerHost, dockerInstallation, dockerRegistryCredentials, build, launcher, listener, verbose, privileged); + final Docker docker = new Docker(dockerHost, dockerInstallation, dockerRegistryCredentials, build, launcher, listener, verbose, privileged, filterEnvVariables); final BuiltInContainer runInContainer = new BuiltInContainer(docker); build.addAction(runInContainer); diff --git a/src/main/resources/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper/config.jelly b/src/main/resources/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper/config.jelly index 42a7a78..7dcf69f 100644 --- a/src/main/resources/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper/config.jelly +++ b/src/main/resources/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper/config.jelly @@ -70,6 +70,9 @@ THE SOFTWARE. + + + diff --git a/src/main/resources/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper/help-filterEnvVariables.html b/src/main/resources/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper/help-filterEnvVariables.html new file mode 100644 index 0000000..08f9127 --- /dev/null +++ b/src/main/resources/com/cloudbees/jenkins/plugins/docker_build_env/DockerBuildWrapper/help-filterEnvVariables.html @@ -0,0 +1,2 @@ +Please provide the environment variables you want not to be overridden in the docker container. Please list the variables +comma separated. Example: PATH,BUILD_URL \ No newline at end of file diff --git a/src/test/java/com/cloudbees/jenkins/plugins/docker_build_env/FunctionalTests.java b/src/test/java/com/cloudbees/jenkins/plugins/docker_build_env/FunctionalTests.java index 1bd42a7..c364aa5 100644 --- a/src/test/java/com/cloudbees/jenkins/plugins/docker_build_env/FunctionalTests.java +++ b/src/test/java/com/cloudbees/jenkins/plugins/docker_build_env/FunctionalTests.java @@ -33,7 +33,7 @@ public void run_inside_pulled_container() throws Exception { project.getBuildWrappersList().add( new DockerBuildWrapper( new PullDockerImageSelector("ubuntu:14.04"), - "", new DockerServerEndpoint("", ""), "", true, false, Collections.emptyList(), null, "cat", false, "bridge", null, null, false) + "", new DockerServerEndpoint("", ""), "", true, false, Collections.emptyList(), null, "cat", false, "bridge", null, null, "PATH", false) ); project.getBuildersList().add(new Shell("lsb_release -a")); @@ -51,8 +51,8 @@ public void run_inside_built_container() throws Exception { project.getBuildWrappersList().add( new DockerBuildWrapper( - new DockerfileImageSelector(".", "Dockerfile"), - "", new DockerServerEndpoint("", ""), "", true, false, Collections.emptyList(), null, "cat", false, "bridge", null, null, true) + new DockerfileImageSelector(".", "$WORKSPACE/Dockerfile"), + "", new DockerServerEndpoint("", ""), "", true, false, Collections.emptyList(), null, "cat", false, "bridge", null, null, "PATH", true) ); project.getBuildersList().add(new Shell("lsb_release -a")); @@ -77,7 +77,7 @@ public void run_inside_built_python_pip_container() throws Exception { project.getBuildWrappersList().add( new DockerBuildWrapper( new DockerfileImageSelector(".", "Dockerfile"), - "", new DockerServerEndpoint("", ""), "", true, false, Collections.emptyList(), null, "cat", false, "bridge", null, null, true) + "", new DockerServerEndpoint("", ""), "", true, false, Collections.emptyList(), null, "cat", false, "bridge", null, null, "PATH", true) ); project.getBuildersList().add(new Shell("python -V"));