Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make the path that jenkins is accessible on configureable #539

Merged
merged 4 commits into from
Aug 2, 2021
Merged
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
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Advanced arguments:
This option is considered safe for the Vanilla package with the default plugin set.
* `--httpPort` - Port for exposing the web server and Jenkins Web UI from Jenkinsfile Runner.
Disabled by default.
* `--httpPath` - The root path/prefix for expositng the web server and Jenkins Web UI from Jenkinsfile Runner.
* `--agentPort` - Port for connecting inbound Jenkins agents (over JNLP or WebSockets).
Disabled by default.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public class JenkinsLauncherOptions {
description = "Port for exposing the web server and Jenkins Web UI from Jenkinsfile Runner. Disabled by default")
public Integer httpPort;

@CheckForNull
@CommandLine.Option(names = "--httpPath",
description = "Path (including leading / but no trailing /) for exposing the web server and Jenkins Web UI from Jenkinsfile Runner. Root path (/) by default")
public String httpPath;

@CheckForNull
@CommandLine.Option(names = "--agentPort",
description = "Port for connecting inbound Jenkins agents (over JNLP or WebSockets). Disabled by default")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import jenkins.model.Jenkins;
import jenkins.model.JenkinsLocationConfiguration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.server.Server;
Expand Down Expand Up @@ -119,6 +120,14 @@ public abstract class JenkinsEmbedder implements RootAction {
protected int localPort;
protected Server server;

/**
* Where in the {@link Server} is Jenkins deployed?
* <p>
* Just like {@link javax.servlet.ServletContext#getContextPath()}, starts with '/' but doesn't end with '/'.
*/
@CheckForNull
protected String contextPath;

/**
* {@link Runnable}s to be invoked at {@link #after()} .
*/
Expand Down Expand Up @@ -383,7 +392,7 @@ public Launcher.LocalLauncher createLocalLauncher() {
* URL ends with '/'.
*/
public URL getURL() throws IOException {
return new URL("http://localhost:" + localPort + "/");
return new URL("http://localhost:" + localPort + StringUtils.defaultString(contextPath) + "/");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jenkins.model.Jenkins;
import jenkins.util.SystemProperties;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.security.AbstractLoginService;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.server.Server;
Expand Down Expand Up @@ -77,7 +78,8 @@ protected ServletContext createWebServer() throws Exception {
? new Server(launcherOptions.httpPort)
: new Server(queuedThreadPool);

WebAppContext context = new WebAppContext(launcherOptions.warDir.getPath(), null);
contextPath = validateHttpPath(launcherOptions.httpPath);
WebAppContext context = new WebAppContext(launcherOptions.warDir.getPath(), contextPath);
context.setClassLoader(getClass().getClassLoader());
context.setConfigurations(new Configuration[]{new WebXmlConfiguration()});
context.addBean(new NoListenerConfiguration(context));
Expand Down Expand Up @@ -107,6 +109,15 @@ protected ServletContext createWebServer() throws Exception {
return context.getServletContext();
}

private String validateHttpPath(String httpPath) throws IllegalArgumentException {
if (StringUtils.endsWith(httpPath, "/")) {
throw new IllegalArgumentException("httpPath must not end with a trailing /");
} else if ("".equals(httpPath)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if ("".equals(httpPath)) {
} else if (StringUtils.isBlank(httpPath)) {

Copy link
Author

@agentgonzo agentgonzo Jul 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this case. null is a valid input (what you get when the user does not set --httpPath) (and StringUtils.isBlank(null) returns true)

throw new IllegalArgumentException("httpPath must not be empty. Omit this parameter to use the root path");
}
return httpPath;
}

//TODO: add support of timeout
/**
* Launches the Jenkins instance, without any time out or output message.
Expand Down