前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jenkins获取JENKINS_HOME过程

Jenkins获取JENKINS_HOME过程

作者头像
donghui
发布2019-04-19 14:56:28
4.1K0
发布2019-04-19 14:56:28
举报
文章被收录于专栏:donghui的博客donghui的博客

JENKINS_HOME是Jenkins的主目录。

在Jenkins上查看JENKINS_HOME:

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

如上图帮助文档所示:

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

  1. 使用你Web容器的管理工具设置JENKINS_HOME环境参数.
  2. 在启动Web容器之前设置JENKINS_HOME环境变量.
  3. (不推荐)更改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"};

  1. 首先,会在JNDI(可在web.xml配置文件中配置)中查找JENKINS_HOME或HUDSON_HOME
  2. 其次会在系统属性中查找JENKINS_HOME或HUDSON_HOME
  3. 接着会在环境变量中查找JENKINS_HOME或HUDSON_HOME
  4. 最后,如果上述都找不到,会默认选择 $user.home/.jenkins为JENKINS_HOME($user.home/.hudson为HUDSON_HOME)

附:WebAppMain.java的getHomeDir方法源码

代码语言:javascript
复制
   /**
     * 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方法的相应源码对应:

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

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015/05/23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档