如题

准备工作

Configure System

配置 Publish over SSH

Path to key

指定公匙路径,比如我要以 utomcat 的身份来获取 git 源码,则先创建 utomcat 的公匙,然后指定:/home/utomcat/.ssh/utomcat

Global Tool Configuration

配置 Maven Configuration 和 Git,根据服务器上安装情况配置即可。

配置邮件服务

作用是可以配置一些通知,比如:在每次构建之后,收到构建情况的邮箱通知。

以下以配置 qq 发件邮箱为例。
需要注意的是,发件人和管理员(System Admin e-mail address)邮箱要配置成一个,否则会导致发邮件失败,提示信息类似为:发件人与发件账号必须为同一个。
SMTP server:smtp.qq.com
Default user e-mail suffix:@qq.com
勾选:Use SMTP Authentication
User Name:1614923608@qq.com
Password:#填入邮箱配置的验证码(需要发短信设置的那个,在开启 smtp 配置处),非邮箱登录密码
勾选:Use SSL

创建 jenkins 任务

Source Code Management

选 Git。
Repository URL:ugit@git.apg.com:source # 需要先配置好 ssh 方式访问
Credentials:none
Branch Specifier (blank for ‘any’):*/master # 分支,这里用默认值

Pre Steps

Build:pom.xml # 指定项目根目录下的 pom.xml,对于多 module 的项目,如果指向子目录下的 pom.xml,可能不能通过 mvn 编译成功。
Goals and options:clean package -Dmaven.test.skip=true # 这是 maven 编译项目的指令

Post Steps

选:Run only if build succeeds
Command:sh /home/utomcat/shell-script/autoDeploy.sh bootProjectName-1.0.0

执行的脚本 autoDeploy.sh,请参考:CentOS 7.5 war 包发布到 tomcat web 服务器,自动化发布脚本

通过 Jenkins 远程执行停止 tomcat 的脚本,报错

1
2
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program

解决:在 shutdown.sh 和 startup.sh 的最前面加上下面的脚本

1
export JAVA_HOME=/usr/jdk1.8.0_171

还有更好的方式,在脚本 autoDeploy.sh 前面添加:

1
source /etc/profile

Jenkins 连接 git 时无权访问的问题

提示信息

1
2
3
4
5
6
7
8
9
Failed to connect to repository : Command "/usr/bin/git ls-remote -h ugit@git.apg.com:source HEAD" returned status code 128:
stdout:
stderr: Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

原因

这是因为通过 ssh 方式访问 git 服务没有权限导致。

解决

以 jenkins 启动用户的身份登录服务器,执行 ssh-keygen -t rsa -C “userName”,将公钥 id_rsa.pub 添加到 git 服务器。

  1. 如果是 gitee.com,则添加项目公钥或者添加用户公钥均可。
  2. 如果是 gitolite 搭建的 git 服务,则将上面生成的公钥添加到 gitolite-admin 项目的 keydir 目录下并改名为 jenkins 的启动用户(也可以自定义),同时在 gitolite.conf 中添加该用户对项目的访问权限。

在 Jenkins job 中调用脚本来启动 tomcat,但是并未真正启动

现象

其它脚本都执行成功了,比如:获取项目最新代码、打包项目、停止 tomcat、删除旧文件、解压新生成的 war 包至 web 目录,copy 配置文件至 WEB-INF/classes/。
只是最后一步启动 tomcat 之后,调用的地方显示:

1
2
3
Tomcat started.
```
tomcat 日志停留在下面就不动了

26-Jul-2018 15:29:27.926 WARNING [localhost-startStop-1] org.apache.catalina.webresources.Cache.getResource Unable to add the resource at [/WEB-INF/classes/web/wx/shoppingCart.html] to the cache for web application [] because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
26-Jul-2018 15:29:27.926 WARNING [localhost-startStop-1] org.apache.catalina.webresources.Cache.getResource Unable to add the resource at [/WEB-INF/classes/web/wx/shoppingCartEmpty.html] to the cache for web application [] because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
26-Jul-2018 15:29:27.926 WARNING [localhost-startStop-1] org.apache.catalina.webresources.Cache.getResource Unable to add the resource at [/WEB-INF/classes/apg/boot] to the cache for web application [] because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
26-Jul-2018 15:29:27.926 WARNING [localhost-startStop-1] org.apache.catalina.webresources.Cache.getResource Unable to add the resource at [/WEB-INF/classes/apg/boot/controller] to the cache for web application [] because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
26-Jul-2018 15:29:27.926 WARNING [localhost-startStop-1] org.apache.catalina.webresources.Cache.getResource Unable to add the resource at [/WEB-INF/classes/apg/boot/config] to the cache for web application [] because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
26-Jul-2018 15:29:27.926 WARNING [localhost-startStop-1] org.apache.catalina.webresources.Cache.getResource Unable to add the resource at [/WEB-INF/classes/apg/boot/common] to the cache for web application [] because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
26-Jul-2018 15:29:27.934 WARNING [localhost-startStop-1] org.apache.catalina.webresources.Cache.getResource Unable to add the resource at [/WEB-INF/classes/] to the cache for web application [] because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache

1
2
3
4
5
6
7
8
9
10
11

手动执行发布脚本是正常的。

百度关键词:after evicting expired cache entries - consider increasing the maximum size of the cache
网上的解决办法
在tomcat的/conf/context.xml中的<Context>中添加
```xml
<Resources
cachingAllowed="true"
cacheMaxSize="100000" ---->这里单位是 KB
/>

当然,这个办法解决的是内存分配的问题,我这里的问题与内存分配无关。
这里的真正原因是 Jenkins 在执行任务结束的时候会关掉所有子进程,导致 tomcat 启动过程中被强制停掉,才会出现上面的现象。

最开始我是以 root 的身份启动 Jenkins,在执行脚本的时候,切换到 utomcat 下去执行,执行脚本如下:

1
su - utomcat -lc "sh /home/utomcat/shell/autoDeploy.sh"

这种方式,没有问题,因为执行进程已经切换至另外一个用户了。

后来我把 Jenkins 的启动用户以及相应的目录权限全部设置为 utomcat 之后,才有这种现象。

解决

在 autoDeploy.sh 前面添加:

1
export BUILD_ID=dontKillMe # for jenkins

另外,网上有添加配置节点来解决的,没有深入研究。

原因分析

Jenkins 默认会在 Build 结束后 Kill 掉所有的衍生进程。