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

Feature/filter environment variables #73

Open
wants to merge 4 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 @@ -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;
Expand All @@ -47,8 +48,9 @@ public class Docker implements Closeable {
private final boolean privileged;
private final AbstractBuild build;
private EnvVars envVars;
private List<String> 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);
Expand All @@ -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);
}


Expand Down Expand Up @@ -95,6 +98,13 @@ private EnvVars getEnvVars() throws IOException, InterruptedException {
return envVars;
}

private List<String> 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);
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Volume> 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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ THE SOFTWARE.
<f:entry field="cpu" title="CPU shares">
<f:textbox/>
</f:entry>
<f:entry field="filterEnvVariables" title="Filter Environment Variables">
<f:textbox/>
</f:entry>
</f:advanced>

</f:nested>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.<Volume>emptyList(), null, "cat", false, "bridge", null, null, false)
"", new DockerServerEndpoint("", ""), "", true, false, Collections.<Volume>emptyList(), null, "cat", false, "bridge", null, null, "PATH", false)
);
project.getBuildersList().add(new Shell("lsb_release -a"));

Expand All @@ -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.<Volume>emptyList(), null, "cat", false, "bridge", null, null, true)
new DockerfileImageSelector(".", "$WORKSPACE/Dockerfile"),
"", new DockerServerEndpoint("", ""), "", true, false, Collections.<Volume>emptyList(), null, "cat", false, "bridge", null, null, "PATH", true)
);
project.getBuildersList().add(new Shell("lsb_release -a"));

Expand All @@ -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.<Volume>emptyList(), null, "cat", false, "bridge", null, null, true)
"", new DockerServerEndpoint("", ""), "", true, false, Collections.<Volume>emptyList(), null, "cat", false, "bridge", null, null, "PATH", true)
);
project.getBuildersList().add(new Shell("python -V"));

Expand Down