问题

随着时间推移,Jenkins 占用的空间越来越大,快要占满空间时,Jenkins 会提示:

1
Jenkins: your Jenkins data directory /var/lib/jenkins (aka JENKINS_HOME) is almost full

最开始安装 CentOS 时,使用了默认的配置,导致系统盘只分配了 50GiB 空间,现在很快就用满了。

网上搜索到的方案都是丢弃旧的构建,但这并不可行,需要保留所有构建。正好机器上还有多余的硬盘空间,因此尝试将整个 Jenkins 目录迁移。

环境

  • Jenkins 2.249.1
  • CentOS 7.6.1810

文档

官方文档给出了修改 JENKINS_HOME 环境变量的解决方法,但是并未给出不同安装方式下的配置修改方法:

查找配置

Jenkins 是通过包管理器安装的:

1
yum install jenkins
1
2
3
4
5
6
7
8
9
# systemctl status jenkins
● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: active (running) since Fri 2020-09-18 11:09:06 CST; 3 weeks 0 days ago
     Docs: man:systemd-sysv-generator(8)
  Process: 28850 ExecStop=/etc/rc.d/init.d/jenkins stop (code=exited, status=0/SUCCESS)
  Process: 28875 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/jenkins.service
           └─28899 /etc/alternatives/java -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --httpPort=5000 --debug=5 --...

通过 systemctl status jenkins 的输出可以看到 Jenkins 是从 /etc/rc.d/init.d/jenkins 脚本启动的,并且可以看到当前的 JENKINS_HOME 环境变量的值。

打开脚本,查看逻辑:

1
vi /etc/rc.d/init.d/jenkins
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Check for existence of needed config file and read it
JENKINS_CONFIG=/etc/sysconfig/jenkins

# Read config
[ -f "$JENKINS_CONFIG" ] && . "$JENKINS_CONFIG"

# Set up environment accordingly to the configuration settings
[ -n "$JENKINS_HOME" ] || { echo "JENKINS_HOME not configured in $JENKINS_CONFIG";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }

可以看到 JENKINS_HOME 环境变量优先从配置文件 /etc/sysconfig/jenkins 中读取。

1
vi /etc/sysconfig/jenkins
1
JENKINS_HOME="/var/lib/jenkins"

转移

停止服务

1
2
# systemctl stop jenkins
# systemctl status jenkins

转移前

查看磁盘占用

1
2
3
4
# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G   50G  693M  99% /
/dev/mapper/centos-home  873G  210G  663G  25% /home

修改配置

1
vi /etc/sysconfig/jenkins

找到 JENKINS_HOME 配置并修改,= 号前后不要有空格,这是 Bash 的语法:

1
2
# JENKINS_HOME="/var/lib/jenkins"
JENKINS_HOME="/home/jenkins"

转移

1
2
3
4
5
# time mv /var/lib/jenkins /home

real    13m8.768s
user    0m3.429s
sys    0m53.122s

使用 time 可以统计命令执行了多长时间。

转移后

查看磁盘占用

1
2
3
4
# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G   36G   15G  71% /
/dev/mapper/centos-home  873G  224G  649G  26% /home

启动服务

1
# systemctl start jenkins

确认

完成所有工作后一定要确认是否真的全部完成。

命令行参数

1
2
3
4
5
6
7
8
9
# systemctl status jenkins
● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: active (running) since Fri 2020-10-09 13:13:06 CST; 1min 13s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 5056 ExecStop=/etc/rc.d/init.d/jenkins stop (code=exited, status=0/SUCCESS)
  Process: 6584 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/jenkins.service
           └─6628 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/home/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/wa...

通过 systemctl status jenkins 的输出可以看到 JENKINS_HOME 已更新。

系统信息

打开 Jenkins | 系统管理 | 状态信息 | 系统信息 确认环境变量 JENKINS_HOME 已更新

1
2
JENKINS_HOME 
/home/jenkins 

提示信息

由于剩余空间变多了,Jenkins 不再提示空间快满了。