背景:
使用git+jenkins+maven自动触发流水线任务,在Pipeline脚本中打包并调用服务器脚本重启项目(如下):
问题:
构建任务正常完成,脚本也执行了,但用ps -ef|grep java查看,没看到进程。
问题解决过程:
一开始:
因为之前用过jenkins,脑子里有个印象:jenkins会杀进程。于是沿着这个思路,百度了一串答案,都是在项目启动脚本中加入BUILD_ID=dontKillMe或者source /etc/profile,都没用(万念俱灰)。
后来:
看到一位老哥特别牛气地说:别的乱七八糟的都没用,还得是这个withEnv([‘JENKINS_NODE_COOKIE=dontkillme]’])。于是我又垂死挣扎了一下,“果然”没卵用。
最后:
我突然想起来去看一下项目启动的错误日志。不看不知道,一看吓一跳:原来因为项目使用了ehcache缓存,需要/tmp文件夹的读写等权限,但jenkins的权限不够。
于是给jenkins用户加了权限(chown -R jenkins /tmp),在服务器直接手动起项目,发现还是提示Permission denied。
于是又给**/tmp文件加了读写执行的权限( chmod -R 777 /tmp**,因为对linux的权限也不是特别熟悉,所以使用了777,各位请谨慎),这次手动启动终于起来了。至此,权限就问题解决了。再次使用jenkins构建,也成功了。
总结:
先手动启动确定项目启动没问题,再使用withEnv([‘JENKINS_NODE_COOKIE=dontkillme]’])。我的jenkins Pipeline脚本是这样式儿的:
pipeline {
agent any
tools {
// Install the Maven version configured as "M3" and add it to the path.
maven "M3"
}
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git branch: 'master', url: 'git@代码服务器:root/lishe.git'
// Run Maven on a Unix agent.
sh "mvn -Dmaven.test.skip=true clean package"
// To run Maven on a Windows agent, use
// bat "mvn -Dmaven.test.failure.ignore=true clean package"
}
post {
// If Maven was able to run the tests, even if some of the test
// failed, record the test results and archive the jar file.
success {
archiveArtifacts '*/target/*.jar'
withEnv(['JENKINS_NODE_COOKIE=dontkillme]']) {
sh '''
cd /usr/local/javaApp/项目路径
./start.sh
'''
}
}
}
}
}
}