From 185d6ebcd77c13e24a4046195063d27afa25f3ea Mon Sep 17 00:00:00 2001 From: Jim Carrothers Date: Fri, 8 Apr 2016 18:32:09 -0700 Subject: [PATCH] pass information on Jenkins user to container launch script 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. --- .../docker_build_env/DockerBuildWrapper.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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 fe16b23..45013de 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,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 volumes, String group, String command, @@ -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"); } @@ -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 links = new HashMap(); @@ -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 {