Jenkins获取JENKINS_HOME过程
JENKINS_HOME是Jenkins的主目录。在Jenkins上查看JENKINS_HOME:系统管理→系统设置→主目录(<JENKINS_URL>/configure页面)如上图帮助文档所示:Jenkins储存所有的数据文件在这个目录下. 你可以通过以下几种方式更改:[*]使用你Web容器的管理工具设置JENKINS_HOME环境参数.
[*]在启动Web容器之前设置JENKINS_HOME环境变量.
[*](不推荐)更改Jenkins.war(或者在展开的Web容器)内的web.xml配置文件.
这个值在Jenkins运行时是不能更改的. 其通常用来确保你的配置是否生效.查看Jenkins的WEB-INF/web.xml,可以得知Jenkins主对象为hudson.WebAppMain:查看WebAppMain.java的源码,getHomeDir方法即用来确定Jenkins的主目录,其逻辑如下:鉴于Hudson是Jenkins的前身,所以为了兼容Jenkins主目录的名称有:JENKINS_HOME或HUDSON_HOMEprivate static final String[] HOME_NAMES = {"JENKINS_HOME","HUDSON_HOME"};
[*]首先,会在JNDI(可在web.xml配置文件中配置)中查找JENKINS_HOME或HUDSON_HOME
[*]其次会在系统属性中查找JENKINS_HOME或HUDSON_HOME
[*]接着会在环境变量中查找JENKINS_HOME或HUDSON_HOME
[*]最后,如果上述都找不到,会默认选择 $user.home/.jenkins为JENKINS_HOME($user.home/.hudson为HUDSON_HOME)
附:WebAppMain.java的getHomeDir方法源码/**
* Determines the home directory for Jenkins.
*
* <p>
* We look for a setting that affects the smallest scope first, then bigger ones later.
*
* <p>
* People makes configuration mistakes, so we are trying to be nice
* with those by doing {@link String#trim()}.
*
* <p>
* @return the File alongside with some description to help the user troubleshoot issues
*/
public FileAndDescription getHomeDir(ServletContextEvent event) {
// check JNDI for the home directory first
for (String name : HOME_NAMES) {
try {
InitialContext iniCtxt = new InitialContext();
Context env = (Context) iniCtxt.lookup("java:comp/env");
String value = (String) env.lookup(name);
if(value!=null && value.trim().length()>0)
return new FileAndDescription(new File(value.trim()),"JNDI/java:comp/env/"+name);
// look at one more place. See issue #1314
value = (String) iniCtxt.lookup(name);
if(value!=null && value.trim().length()>0)
return new FileAndDescription(new File(value.trim()),"JNDI/"+name);
} catch (NamingException e) {
// ignore
}
}
// next the system property
for (String name : HOME_NAMES) {
String sysProp = System.getProperty(name);
if(sysProp!=null)
return new FileAndDescription(new File(sysProp.trim()),"System.getProperty(\""+name+"\")");
}
// look at the env var next
for (String name : HOME_NAMES) {
String env = EnvVars.masterEnvVars.get(name);
if(env!=null)
return new FileAndDescription(new File(env.trim()).getAbsoluteFile(),"EnvVars.masterEnvVars.get(\""+name+"\")");
}
// otherwise pick a place by ourselves
String root = event.getServletContext().getRealPath("/WEB-INF/workspace");
if(root!=null) {
File ws = new File(root.trim());
if(ws.exists())
// Hudson <1.42 used to prefer this before ~/.hudson, so
// check the existence and if it's there, use it.
// otherwise if this is a new installation, prefer ~/.hudson
return new FileAndDescription(ws,"getServletContext().getRealPath(\"/WEB-INF/workspace\")");
}
File legacyHome = new File(new File(System.getProperty("user.home")),".hudson");
if (legacyHome.exists()) {
return new FileAndDescription(legacyHome,"$user.home/.hudson"); // before rename, this is where it was stored
}
File newHome = new File(new File(System.getProperty("user.home")),".jenkins");
return new FileAndDescription(newHome,"$user.home/.jenkins");
}
此外,在Tomcat/logs/stdout_YYYYMMDD.log中有如下日志:Jenkins home directory: F:\JENKINS_HOME found at: EnvVars.masterEnvVars.get("JENKINS_HOME")
这与getHomeDir方法的相应源码对应:System.out.println("Jenkins home directory: "+home+" found at: "+describedHomeDir.description);
支持
页:
[1]