1、查看jenkins旧版本

jenkins页面端提示需要升级jenkins 2.361.1,由于是测试服务器,所以就升级下,然后就有不可描述的事情发生了。

[root@linux-01 ~]# ps -ef |grep jenkins
jenkins    6816      1  0 14:34 ?        00:01:25 /usr/local/jdk1.8/bin/java -Dcom.sun.akuma.Daemon=daemonized -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 --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
root      10645   8043  0 18:47 pts/1    00:00:00 grep --color=auto jenkins

通过服务器上ps的jenkins进程可以查看到jenkins的war包所在目录是:/usr/lib/jenkins/;jdk使用的是8的版本。

2、下载jenkins最新版本2.361.1

[root@linux-01 ~]#  wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.361.1/jenkins.war  --no-check-certificate 
# 省略下载过程

3、替换war包

[root@linux-01 ~]# cd /usr/lib/jenkins/
[root@linux-01 jenkins]#  mv jenkins.war  jenkins.war.bak
[root@linux-01 jenkins]#  mv ~/jenkins.war ./
 [root@linux-01 jenkins]# ll
总用量 156212
-r-------- 1 root root 93504273 97 18:05 jenkins.war
-rw-r--r-- 1 root root 66452488 812 2020 jenkins.war.bak

4、重新启动jenkins出问题

[root@linux-01 jenkins]# /etc/init.d/jenkins stop 
Stopping jenkins (via systemctl):                          [  确定  ]

[root@linux-01 jenkins]# /etc/init.d/jenkins start
Starting jenkins (via systemctl):                          [  确定  ]
[root@linux-01 jenkins]# 
[root@linux-01 jenkins]# 
[root@linux-01 jenkins]# !ps
ps -ef |grep jenkins
root      26750   8043  0 19:04 pts/1    00:00:00 grep --color=auto jenkins
[root@linux-01 jenkins]# ll
总用量 156212

启动jenkins的时候发现无法启动jenkins,然后就查看了jenkins的状态

[root@linux-01 jenkins]# service jenkins status
● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: active (exited) since 一 2022-09-19 19:03:30 CST; 1min 39s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 25966 ExecStop=/etc/rc.d/init.d/jenkins stop (code=exited, status=0/SUCCESS)
  Process: 26141 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=0/SUCCESS)

919 19:03:30 linux-01 jenkins[26141]: java.lang.UnsupportedClassVersionError: 52.0
919 19:03:30 linux-01 jenkins[26141]: at executable.Main.verifyJavaVersion(Main.java:145)
919 19:03:30 linux-01 jenkins[26141]: at executable.Main.main(Main.java:109)
919 19:03:30 linux-01 jenkins[26141]: Jenkins requires Java versions [17, 11] but you are running with Java 1.8 from /usr/local/jdk1.8/jre
919 19:03:30 linux-01 jenkins[26141]: java.lang.UnsupportedClassVersionError: 52.0
919 19:03:30 linux-01 jenkins[26141]: at executable.Main.verifyJavaVersion(Main.java:145)
919 19:03:30 linux-01 jenkins[26141]: at executable.Main.main(Main.java:109)
919 19:03:30 linux-01 runuser[26147]: pam_unix(runuser:session): session closed for user jenkins
919 19:03:30 linux-01 jenkins[26141]: [  确定  ]
919 19:03:30 linux-01 systemd[1]: Started LSB: Jenkins Automation Server.

发现报了一个java的错误:java.lang.UnsupportedClassVersionError: 52.0
去官网查询了下,发现需要升级jdk版本,8的版本无法启用最新的jenkins,可以通过如下图看到升级到2.361.1版本需要java 11或者java 17,我采用了java 17版本。

在这里插入图片描述

5、下载安装新版本的JDK

[root@linux-01 jenkins]# cd 
[root@linux-01 ~]# wget https://mirror.nju.edu.cn/adoptium/17/jdk/x64/linux/OpenJDK17U-jdk_x64_linux_hotspot_17.0.4.1_1.tar.gz

下载省略

jdk8以上的版本貌似就有没有jre目录,需要生成下

[root@linux-01 ~]# tar -xvf OpenJDK17U-jdk_x64_linux_hotspot_17.0.4.1_1.tar.gz -C  /usr/local/
# 解压省略
[root@linux-01 ~]# cd /usr/local/
[root@linux-01 local]# mv jdk-17.0.4.1+1 jdk17

# 修改系统环境变量
[root@linux-01 local]# vim /etc/profile
[root@linux-01 local]# source /etc/profile

export JAVA_HOME=/usr/local/jdk17/
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar


[root@linux-01 local]# source /etc/profile
[root@linux-01 ~]# java --version
openjdk 17.0.4.1 2022-08-12
OpenJDK Runtime Environment Temurin-17.0.4.1+1 (build 17.0.4.1+1)
OpenJDK 64-Bit Server VM Temurin-17.0.4.1+1 (build 17.0.4.1+1, mixed mode, sharing)

jdk17是没有jre目录的,顺变生成下jre目录

[root@linux-01 ~]# ll /usr/local/jdk17/
总用量 24
drwxr-xr-x  2 root root 4096 817 19:56 bin
drwxr-xr-x  5 root root  123 817 19:55 conf
drwxr-xr-x  3 root root  132 817 19:55 include
drwxr-xr-x  2 root root 4096 817 19:55 jmods
drwxr-xr-x 72 root root 4096 817 19:55 legal
drwxr-xr-x  5 root root 4096 817 19:56 lib
drwxr-xr-x  3 root root   18 817 19:55 man
-rw-r--r--  1 root root 2439 817 19:56 NOTICE
-rw-r--r--  1 root root 1566 817 19:55 release

[root@linux-01 ~]# cd $!
cd /usr/local/jdk17/
[root@linux-01 jdk17]#  ./bin/jlink.exe --module-path jmods --add-modules java.desktop --output jre
[root@linux-01 jdk17]#  ll
总用量 24
drwxr-xr-x  2 root root 4096 817 19:56 bin
drwxr-xr-x  5 root root  123 817 19:55 conf
drwxr-xr-x  3 root root  132 817 19:55 include
drwxr-xr-x  2 root root 4096 817 19:55 jmods
drwxr-xr-x  8 root root   94 919 20:24 jre
drwxr-xr-x 72 root root 4096 817 19:55 legal
drwxr-xr-x  5 root root 4096 817 19:56 lib
drwxr-xr-x  3 root root   18 817 19:55 man
-rw-r--r--  1 root root 2439 817 19:56 NOTICE
-rw-r--r--  1 root root 1566 817 19:55 release

6、重新启动jenkins,发现还是有问题

[root@linux-01 local]# /etc/init.d/jenkins start 
Starting jenkins (via systemctl):                          [  确定  ]
[root@linux-01 local]# ps -ef |grep jenkins
root      10850   7462  0 19:39 pts/0    00:00:00 grep --color=auto jenkins
[root@linux-01 local]# 
[root@linux-01 local]# 
[root@linux-01 local]# /etc/init.d/jenkins status
● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: active (exited) since 一 2022-09-19 19:33:27 CST; 6min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 6407 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=0/SUCCESS)

919 19:33:27 linux-01 jenkins[6407]: java.lang.UnsupportedClassVersionError: 62.0
919 19:33:27 linux-01 jenkins[6407]: at Main.verifyJavaVersion(Main.java:174)
919 19:33:27 linux-01 jenkins[6407]: at Main.main(Main.java:142)
919 19:33:27 linux-01 jenkins[6407]: Jenkins requires Java versions [8, 11] but you are running with Java 18 from /usr/local/jdk1.8
919 19:33:27 linux-01 jenkins[6407]: java.lang.UnsupportedClassVersionError: 62.0
919 19:33:27 linux-01 jenkins[6407]: at Main.verifyJavaVersion(Main.java:174)
919 19:33:27 linux-01 jenkins[6407]: at Main.main(Main.java:142)
919 19:33:27 linux-01 runuser[6419]: pam_unix(runuser:session): session closed for user jenkins
919 19:33:27 linux-01 jenkins[6407]: [  确定  ]
919 19:33:27 linux-01 systemd[1]: Started LSB: Jenkins Automation Server.
[root@linux-01 local]# /etc/init.d/jenkins status
● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: active (exited) since 一 2022-09-19 19:33:27 CST; 6min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 6407 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=0/SUCCESS)

6.1 yum升级jenkins

本身jdk版本系统中已经更换为了jdk17,但是jenkins启动的时候无法识别到这个jdk。中间通过命令的方式启动jenkins的,还是会报错。

[root@linux-01 jdk17]# yum list |grep jenkins
https://dl.bintray.com/rabbitmq/rpm/rabbitmq-server/v3.8.x/el/7/repodata/repomd.xml: [Errno 14] HTTPS Error 502 - Bad Gateway
正在尝试其它镜像。
jenkins.noarch                           2.235.4-1.1                   installed
jenkins.noarch                           2.361.1-1.1                   jenkins  
python2-jenkins.noarch                   0.4.16-2.el7                  epel     
python2-jenkins-job-builder.noarch       1.4.0-3.el7                   epel     
python36-jenkins.noarch                  0.4.16-2.el7                  epel    

通过yum list可以查看到有新的jenkins的版本,所以直接通过yum update jenkins升级。

[root@linux-01 jdk17]# yum update jenkins

升级过后需要修改下jenkins启动脚本的jdk参数。

[root@linux-01 jdk17]# cat /usr/lib/systemd/system/jenkins.service 
[Unit]
Description=Jenkins Continuous Integration Server
Requires=network.target
After=network.target
[Service]
Type=notify
NotifyAccess=main
ExecStart=/usr/bin/jenkins
Restart=on-failure
SuccessExitStatus=143
User=jenkins
Group=jenkins
Environment="JENKINS_HOME=/var/lib/jenkins"
WorkingDirectory=/var/lib/jenkins
Environment="JENKINS_WEBROOT=%C/jenkins/war"
Environment="JAVA_HOME=/usr/local/jdk17/"   #修改了此处的jdk信息
Environment="JAVA_OPTS=-Djava.awt.headless=true"
Environment="JENKINS_PORT=8080"
[Install]
WantedBy=multi-user.target

[root@linux-01 jdk17]#  systemctl daemon-reload  

使用这个脚本启动jenkins,发现有进程,端口也启动成功。

[root@linux-01 ~]# systemctl start jenkins
[root@linux-01 ~]# ps -ef |grep jenkins
root     111601  24934  0 20:35 pts/2    00:00:00 systemctl start jenkins
jenkins  111607      1  9 20:35 ?        00:00:08 /usr/local/jdk17//bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=%C/jenkins/war --httpPort=8080
root     114734   7462  0 20:36 pts/0    00:00:00 grep --color=auto jenkins
[root@linux-01 ~]# netstat -lntp 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6403/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      6682/master         
tcp6       0      0 :::8080                 :::*                    LISTEN      107113/java         
tcp6       0      0 :::22                   :::*                    LISTEN      6403/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      6682/master   

7、页面出问题

本以为端口启动成功后,可以正常访问网页,访问网页后发现出了java的报错

请添加图片描述
访问网址:https://www.jenkins.io/redirect/troubleshooting/java.awt.headless

在这里插入图片描述

通过上文可以查看到centos7 需要安装一个fontconfig

[root@linux-01 jdk17]# yum install fontconfig -y

安装省略

8、大功告成,终于可以正常访问

在这里插入图片描述

9、修改init.d下的jenkins脚本

[root@linux-01 ~]# vim /etc/init.d/jenkins 
# 修改candidates的参数为如下,也可以正常启动和关闭jenkins服务
candidates="
/usr/local/jdk17/bin/java
/usr/local/jdk17/jre/bin/java
"
[root@linux-01 ~]# service jenkins stop 
Stopping jenkins (via systemctl):                          [  确定  ]
[root@linux-01 ~]# ps -ef |grep jenkins
root       5719   7462  0 22:57 pts/0    00:00:00 grep --color=auto jenkins
[root@linux-01 ~]# 
[root@linux-01 ~]# 
[root@linux-01 ~]# service jenkins start 
Starting jenkins (via systemctl):                          [  确定  ]
[root@linux-01 ~]# !ps
ps -ef |grep jenkins
jenkins    6040      1 59 22:57 ?        00:00:20 /usr/local/jdk17//bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=%C/jenkins/war --httpPort=8080
root       7293   7462  0 22:58 pts/0    00:00:00 grep --color=auto jenkins

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐