Jenkins获取JENKINS_HOME过程

Stella981
• 阅读 628

JENKINS_HOME是Jenkins的主目录。

在Jenkins上查看JENKINS_HOME:

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

Jenkins获取JENKINS_HOME过程

如上图帮助文档所示:

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

  1. 使用你Web容器的管理工具设置JENKINS_HOME环境参数.

  2. 在启动Web容器之前设置JENKINS_HOME环境变量.

  3. (不推荐)更改Jenkins.war(或者在展开的Web容器)内的web.xml配置文件.

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

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

Jenkins获取JENKINS_HOME过程

查看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方法源码

   /**
     * 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);
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
2年前
Jenkins下Vue自动部署(二)
1Jenkins配置获取首次密码sudodockerexecjenkinscat/var/jenkins\_home/secrets/initialAdminPassword!(https://oscimg.oschina.net/oscnet/6d95b76432470893e66feea9d48b9ef130c.png)2!
Stella981 Stella981
2年前
Centos 6 系统下修改Jenkins的主目录
     由于jenkins默认根目录是建立在安装用户(我是使用root)的根目录下,一般系统盘的空间分区都不会分太大,硬盘空间就很容易被占满了,会导致系统都会不正常,所以考虑要迁移修改jenkins根目录。  一般使用jenkins.war包部署最方便简捷,非安装系统服务方式。      服务启动运行后,Jenkins主目录默认在/r
Wesley13 Wesley13
2年前
1. 容器化部署一套云服务 第一讲 Jenkins(Docker + Jenkins + Yii2 + 云服务器))
容器化部署一套云服务系列1\.容器化部署一套云服务之Jenkins(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.cnblogs.com%2Fjackson0714%2Fp%2Fdeploy1.html)一、购买服务器服务器!caeef00
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Jenkins
提示“反向代理设置有误”的背景1.将jenkins.war放在tomcat容器中运行2.访问Jenkins系统管理,会提示“反向代理设置有误”如何解决在tomcat上部署jenkins的确有这一问题,可以直接点击【不再显示】就可以了,不会有什么影响也可以换成javajar
Stella981 Stella981
2年前
Jenkins插件下载镜像加速
转:https://www.cnblogs.com/zhuochong/p/10082498.html可供选择的jenkins2插件镜像列表:Jenkins所有镜像列表:http://mirrors.jenkinsci.org/status.html比如日本的镜像:http://mirror.esuni.jp/jenkins/,ht
Stella981 Stella981
2年前
Jenkins 插件开发之旅:两天内从 idea 到发布(上篇)
本文首发于:Jenkins中文社区(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fjenkinszh.cn)!huashan(https://oscimg.oschina.net/oscnet/f499d5b4f76f20cf0bce2a00af236d10265.jpg)
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这