Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-23704] Expose port binding as env. variables
  • Loading branch information
vjuranek committed Aug 16, 2014
1 parent 161bcd7 commit bd51023
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Expand Up @@ -8,9 +8,13 @@

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.jenkinsci.plugins.dockerbuildstep.action.EnvInvisibleAction;

import com.github.dockerjava.client.model.ExposedPort;
import com.github.dockerjava.client.model.Ports.Binding;

/**
* This contributor adds various Docker relate variable like container IDs or IP addresses into build environment
* variables.
Expand All @@ -22,6 +26,10 @@
public class DockerEnvContributor extends EnvironmentContributor {

public final String ID_SEPARATOR = ",";
public final String CONTAINER_IDS_ENV_VAR = "DOCKER_CONTAINER_IDS";
public final String CONTAINER_IP_PREFIX = "DOCKER_IP_";
public final String PORT_BINDINGS_ENV_VAR = "DOCKER_HOST_BIND_PORTS";
public final String PORT_BINDING_PREFIX = "DOCKER_HOST_PORT_";

@Override
public void buildEnvironmentFor(@SuppressWarnings("rawtypes") Run r, EnvVars envs, TaskListener listener)
Expand All @@ -32,17 +40,31 @@ public void buildEnvironmentFor(@SuppressWarnings("rawtypes") Run r, EnvVars env
return;
}

String containerIds = envs.get("DOCKER_CONTAINER_IDS", "");
String containerIds = envs.get(CONTAINER_IDS_ENV_VAR, "");
if (!containerIds.equals("")) {
containerIds.concat(ID_SEPARATOR);
}

for (EnvInvisibleAction action : envActions) {
containerIds = containerIds.concat(action.getId()).concat(ID_SEPARATOR);
envs.put("DOCKER_IP_" + action.getHostName(), action.getIpAddress());
envs.put(CONTAINER_IP_PREFIX + action.getHostName(), action.getIpAddress());
exportPortBindings(envs, action.getPortBindings());
}

containerIds = containerIds.substring(0, containerIds.length() - 1);
envs.put("DOCKER_CONTAINER_IDS", containerIds);
envs.put(CONTAINER_IDS_ENV_VAR, containerIds);

}

private void exportPortBindings(EnvVars envs, Map<ExposedPort, Binding> bindings) {
StringBuilder ports = new StringBuilder();
for (ExposedPort hostPort : bindings.keySet()) {
ports.append(hostPort.toString()).append(ID_SEPARATOR);
envs.put(PORT_BINDING_PREFIX + hostPort.getScheme().toUpperCase() + "_" + hostPort.getPort(),
Integer.toString(bindings.get(hostPort).getHostPort()));
}
String bindPorts = ports.substring(0, ports.length() - 1).toString();
envs.put(PORT_BINDINGS_ENV_VAR, bindPorts);
}

}
Expand Up @@ -2,10 +2,14 @@

import hudson.model.InvisibleAction;

import java.util.Map;

import org.jenkinsci.plugins.dockerbuildstep.DockerEnvContributor;
import org.jenkinsci.plugins.dockerbuildstep.cmd.DockerCommand;

import com.github.dockerjava.client.model.ContainerInspectResponse;
import com.github.dockerjava.client.model.ExposedPort;
import com.github.dockerjava.client.model.Ports.Binding;

/**
* Helper invisible action which is used for exchanging information between {@link DockerCommand}s and other object like
Expand Down Expand Up @@ -46,5 +50,9 @@ public String getHostName() {
public String getIpAddress() {
return containerInfo.getNetworkSettings().getIpAddress();
}

public Map<ExposedPort, Binding> getPortBindings() {
return containerInfo.getNetworkSettings().getPorts().getBindings();
}

}

0 comments on commit bd51023

Please sign in to comment.