Skip to content

Commit

Permalink
pass information on Jenkins user to container launch script
Browse files Browse the repository at this point in the history
Some container launch scripts may need to know the username and user/group IDs
of the Jenkins user which the actual build steps will run as.  In particular
the container launch script may want to provide a background service, such as
a graphical environment, for use during the build.

This change passes in this information as part of the container's process
environment.
  • Loading branch information
jcarrothers-sap committed Apr 9, 2016
1 parent adaaee4 commit 18bc4ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public class DockerBuildWrapper extends BuildWrapper {

private String cpu;

/**
* Variable name to pass user:group IDs to the container launch script in its environment.
*/
public static final String JENKINS_USERID_VARIABLE = "JENKINS_USER_IDS";

/**
* Variable name to pass username string to the container launch script in its environment.
*/
public static final String JENKINS_USERNAME_VARIABLE = "JENKINS_USER_NAME";

@DataBoundConstructor
public DockerBuildWrapper(DockerImageSelector selector, String dockerInstallation, DockerServerEndpoint dockerHost, String dockerRegistryCredentials, boolean verbose, boolean privileged,
List<Volume> volumes, String group, String command,
Expand Down Expand Up @@ -182,7 +192,7 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, BuildList
}
}

runInContainer.container = startBuildContainer(runInContainer, build, listener);
runInContainer.container = startBuildContainer(runInContainer, build, listener, launcher);
listener.getLogger().println("Docker container " + runInContainer.container + " started to host the build");
}

Expand All @@ -199,10 +209,14 @@ public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOEx



private String startBuildContainer(BuiltInContainer runInContainer, AbstractBuild build, BuildListener listener) throws IOException {
private String startBuildContainer(BuiltInContainer runInContainer, AbstractBuild build, BuildListener listener, final Launcher launcher) throws IOException {
try {
EnvVars environment = buildContainerEnvironment(build, listener);

// provide Jenkins User/Group IDs and User Name to the containter environment
environment.putIfNotNull(JENKINS_USERID_VARIABLE, whoAmI(launcher));
environment.putIfNotNull(JENKINS_USERNAME_VARIABLE, whoAmI_Name(launcher));

String workdir = build.getWorkspace().getRemote();

Map<String, String> links = new HashMap<String, String>();
Expand Down Expand Up @@ -249,6 +263,14 @@ private String whoAmI(Launcher launcher) throws IOException, InterruptedExceptio

}

private String whoAmI_Name(Launcher launcher) throws IOException, InterruptedException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
launcher.launch().cmds("id", "-un").stdout(bos).quiet(true).join();
String userName = bos.toString().trim();

return userName;
}

@Extension
public static class DescriptorImpl extends BuildWrapperDescriptor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ private EnvVars buildContainerEnvironment() throws IOException, InterruptedExcep
this.env = runInContainer.getDocker().getEnv(runInContainer.container, launcher);
}
EnvVars environment = new EnvVars(env);
environment.remove(DockerBuildWrapper.JENKINS_USERID_VARIABLE);
environment.remove(DockerBuildWrapper.JENKINS_USERNAME_VARIABLE);

// Let BuildWrapper customize environment, including PATH
for (Environment e : build.getEnvironments()) {
Expand Down

0 comments on commit 18bc4ff

Please sign in to comment.