Skip to content

pass information on Jenkins user to container launch script #48

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -75,6 +75,16 @@ public class DockerBuildWrapper extends BuildWrapper {

private final boolean noCache;

/**
* 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 @@ -189,7 +199,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 @@ -206,10 +216,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 @@ -256,6 +270,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