安装Jenkins

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
version: '3.1'
services:
jenkins:
restart: always
image: jenkins/jenkins:2.395
container_name: jenkins
user: root
ports:
# 发布端口
- 12012:8080
# 基于 JNLP 的 Jenkins 代理通过 TCP 端口 50000 与 Jenkins master 进行通信
#- 50000:50000
environment:
- "TZ=Asia/Shanghai"
- "PATH=$PATH:$HOME/bin:/var/local/apache-maven-3.6.3/bin"
volumes:
# jenkins clone下来的项目会在`/var/jenkins_home/workspace`下
- ./data:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker


启动命令docker-compose up -d

更新Jenkins版本

首先,以root用户进入容器

1
2
3
[root@iZ8vbi9mx98t2q4hbta56aZ war]# docker cp jenkins.war jenkins:/root/
[root@iZ8vbi9mx98t2q4hbta56aZ war]# docker exec -u root -it jenkins bash
其次,执行命令查看java进程
1
2
3
4
5
[root@47faa9548aa0 /]# cd /root/
[root@47faa9548aa0 ~]# ps -ef|grep java
jenkins 7 1 0 Mar12 ? 00:04:22 java -Duser.home=/var/jenkins_home -Djenkins.model.Jenkins.slaveAgentPort=50000 -jar /usr/share/jenkins/jenkins.war
root 1012 994 0 16:35 pts/0 00:00:00 grep --color=auto java
这里就能发现,其实容器里面就是使用java启动了jenkins.[war包](https://so.csdn.net/so/search?q=war包&spm=1001.2101.3001.7020),那么我们升级/usr/share/jenkins/jenkins.war就可以了。

下载最新的war包

进入官网查看下载jenkins download
最新版本链接就是:http://mirrors.jenkins.io/war-stable/latest/jenkins.war
本地下载后上传服务器,并拷贝到容器中

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@iZ8vbi9mx98t2q4hbta56aZ war]# docker cp jenkins.war jenkins:/root/
[root@iZ8vbi9mx98t2q4hbta56aZ war]# docker exec -u root -it jenkins bash
[root@47faa9548aa0 /]# cd /root/
[root@47faa9548aa0 ~]# ps -ef|grep java
jenkins 7 1 0 Mar12 ? 00:04:22 java -Duser.home=/var/jenkins_home -Djenkins.model.Jenkins.slaveAgentPort=50000 -jar /usr/share/jenkins/jenkins.war
root 1012 994 0 16:35 pts/0 00:00:00 grep --color=auto java
[root@47faa9548aa0 ~]# cd /usr/share/jenkins/
[root@47faa9548aa0 jenkins]# cp jenkins.war jenkins.war.22.3.17.bak
[root@47faa9548aa0 jenkins]# ls
jenkins.war jenkins.war.22.3.17.bak ref
[root@47faa9548aa0 jenkins]# mkdir other-version
[root@47faa9548aa0 jenkins]# cd other-version/
[root@47faa9548aa0 other-version]# cp /root/jenkins.war .

删除原war包,使用新的替换即可

1
2
3
4
5
6
7
8
9
[root@47faa9548aa0 other-version]# cd /usr/share/jenkins
[root@47faa9548aa0 jenkins]# ls
jenkins.war jenkins.war.22.3.17.bak other-version ref
[root@47faa9548aa0 jenkins]# rm -f jenkins.war
[root@47faa9548aa0 jenkins]# cd other-version
[root@47faa9548aa0 other-version]# mv jenkins.war ../
[root@47faa9548aa0 other-version]# cd ..
[root@47faa9548aa0 jenkins]# ls
jenkins.war jenkins.war.22.3.17.bak other-version ref

为避免遇到如下问题,可将Role strategy hpi plugin拷贝下来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
java.lang.IllegalStateException: An attempt to save the global configuration was made before it was loaded
at jenkins.model.Jenkins.save(Jenkins.java:3519)
at jenkins.model.Jenkins.saveQuietly(Jenkins.java:3546)
at jenkins.model.Jenkins.setSecurityRealm(Jenkins.java:2743)
at jenkins.model.Jenkins$15.run(Jenkins.java:3481)
at org.jvnet.hudson.reactor.TaskGraphBuilder$TaskImpl.run(TaskGraphBuilder.java:175)
at org.jvnet.hudson.reactor.Reactor.runTask(Reactor.java:305)
at jenkins.model.Jenkins$5.runTask(Jenkins.java:1156)
at org.jvnet.hudson.reactor.Reactor$2.run(Reactor.java:222)
at org.jvnet.hudson.reactor.Reactor$Node.run(Reactor.java:121)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused: org.jvnet.hudson.reactor.ReactorException
at org.jvnet.hudson.reactor.Reactor.execute(Reactor.java:291)
at jenkins.InitReactorRunner.run(InitReactorRunner.java:49)
at jenkins.model.Jenkins.executeReactor(Jenkins.java:1191)
at jenkins.model.Jenkins.<init>(Jenkins.java:981)
at hudson.model.Hudson.<init>(Hudson.java:86)
at hudson.model.Hudson.<init>(Hudson.java:82)
at hudson.WebAppMain$3.run(WebAppMain.java:247)
Caused: hudson.util.HudsonFailedToLoad
at hudson.WebAppMain$3.run(WebAppMain.java:264)

下载hpi文件,并将其放到JENKINS_HOME/plugins文件夹。jenkins plugin文件夹一般在下面位置:docker/jenkins/data/plugins

Role strategy hpi plugin的下载链接为

1
https://updates.jenkins.io/latest/role-strategy.hpi

重启

1
2
docker-compose stop
docker-compose up -d