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_HOME
private 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方法源码
1 /** 2 * Determines the home directory for Jenkins. 3 * 4 * <p> 5 * We look for a setting that affects the smallest scope first, then bigger ones later. 6 * 7 * <p> 8 * People makes configuration mistakes, so we are trying to be nice 9 * with those by doing {@link String#trim()}. 10 * 11 * <p> 12 * @return the File alongside with some description to help the user troubleshoot issues 13 */ 14 public FileAndDescription getHomeDir(ServletContextEvent event) { 15 // check JNDI for the home directory first 16 for (String name : HOME_NAMES) { 17 try { 18 InitialContext iniCtxt = new InitialContext(); 19 Context env = (Context) iniCtxt.lookup("java:comp/env"); 20 String value = (String) env.lookup(name); 21 if(value!=null && value.trim().length()>0) 22 return new FileAndDescription(new File(value.trim()),"JNDI/java:comp/env/"+name); 23 // look at one more place. See issue #1314 24 value = (String) iniCtxt.lookup(name); 25 if(value!=null && value.trim().length()>0) 26 return new FileAndDescription(new File(value.trim()),"JNDI/"+name); 27 } catch (NamingException e) { 28 // ignore 29 } 30 } 31 32 // next the system property 33 for (String name : HOME_NAMES) { 34 String sysProp = System.getProperty(name); 35 if(sysProp!=null) 36 return new FileAndDescription(new File(sysProp.trim()),"System.getProperty(\""+name+"\")"); 37 } 38 39 // look at the env var next 40 for (String name : HOME_NAMES) { 41 String env = EnvVars.masterEnvVars.get(name); 42 if(env!=null) 43 return new FileAndDescription(new File(env.trim()).getAbsoluteFile(),"EnvVars.masterEnvVars.get(\""+name+"\")"); 44 } 45 46 // otherwise pick a place by ourselves 47 48 String root = event.getServletContext().getRealPath("/WEB-INF/workspace"); 49 if(root!=null) { 50 File ws = new File(root.trim()); 51 if(ws.exists()) 52 // Hudson <1.42 used to prefer this before ~/.hudson, so 53 // check the existence and if it's there, use it. 54 // otherwise if this is a new installation, prefer ~/.hudson 55 return new FileAndDescription(ws,"getServletContext().getRealPath(\"/WEB-INF/workspace\")"); 56 } 57 58 File legacyHome = new File(new File(System.getProperty("user.home")),".hudson"); 59 if (legacyHome.exists()) { 60 return new FileAndDescription(legacyHome,"$user.home/.hudson"); // before rename, this is where it was stored 61 } 62 63 File newHome = new File(new File(System.getProperty("user.home")),".jenkins"); 64 return new FileAndDescription(newHome,"$user.home/.jenkins"); 65 }
此外,在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);