51Testing软件测试论坛

标题: Jenkins获取JENKINS_HOME过程 [打印本页]

作者: 测试积点老人    时间: 2018-12-20 14:05
标题: Jenkins获取JENKINS_HOME过程

JENKINS_HOME是Jenkins的主目录。

在Jenkins上查看JENKINS_HOME:

系统管理→系统设置→主目录  (<JENKINS_URL>/configure页面)

[attach]120277[/attach]

如上图帮助文档所示:

Jenkins储存所有的数据文件在这个目录下. 你可以通过以下几种方式更改:

这个值在Jenkins运行时是不能更改的. 其通常用来确保你的配置是否生效.

查看Jenkins的WEB-INF/web.xml,可以得知Jenkins主对象为hudson.WebAppMain:

[attach]120278[/attach]

查看WebAppMain.java的源码,getHomeDir方法即用来确定Jenkins的主目录,其逻辑如下:

鉴于Hudson是Jenkins的前身,所以为了兼容Jenkins主目录的名称有:JENKINS_HOME或HUDSON_HOME

  1. private static final String[] HOME_NAMES = {"JENKINS_HOME","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.         // next the system property
  32.         for (String name : HOME_NAMES) {
  33.             String sysProp = System.getProperty(name);
  34.             if(sysProp!=null)
  35.                 return new FileAndDescription(new File(sysProp.trim()),"System.getProperty(\""+name+"\")");
  36.         }

  37.         // look at the env var next
  38.         for (String name : HOME_NAMES) {
  39.             String env = EnvVars.masterEnvVars.get(name);
  40.             if(env!=null)
  41.                 return new FileAndDescription(new File(env.trim()).getAbsoluteFile(),"EnvVars.masterEnvVars.get(\""+name+"\")");
  42.         }

  43.         // otherwise pick a place by ourselves

  44.         String root = event.getServletContext().getRealPath("/WEB-INF/workspace");
  45.         if(root!=null) {
  46.             File ws = new File(root.trim());
  47.             if(ws.exists())
  48.                 // Hudson <1.42 used to prefer this before ~/.hudson, so
  49.                 // check the existence and if it's there, use it.
  50.                 // otherwise if this is a new installation, prefer ~/.hudson
  51.                 return new FileAndDescription(ws,"getServletContext().getRealPath(\"/WEB-INF/workspace\")");
  52.         }

  53.         File legacyHome = new File(new File(System.getProperty("user.home")),".hudson");
  54.         if (legacyHome.exists()) {
  55.             return new FileAndDescription(legacyHome,"$user.home/.hudson"); // before rename, this is where it was stored
  56.         }

  57.         File newHome = new File(new File(System.getProperty("user.home")),".jenkins");
  58.         return new FileAndDescription(newHome,"$user.home/.jenkins");
  59.     }
复制代码

此外,在Tomcat/logs/stdout_YYYYMMDD.log中有如下日志:

Jenkins home directory: F:\JENKINS_HOME found at: EnvVars.masterEnvVars.get("JENKINS_HOME")

这与getHomeDir方法的相应源码对应:

  1. System.out.println("Jenkins home directory: "+home+" found at: "+describedHomeDir.description);
复制代码


作者: Miss_love    时间: 2021-1-5 14:55
支持




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2