安装jenkins,需要先安装jdk,根据自己的项目来可能还会用到 git, maven等
安装 引自(https://www.cnblogs.com/lfxiao/p/9877853.html)
1.yum的repo中默认没有Jenkins,需要先将Jenkins存储库添加到yum repos,执行下面的命令:
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
2. 导包
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
3.安装
yum install -y jenkins
4. 启动jenkins
service jenkins start
出现这个说明Jenkins安装启动完成
image.png
5.停止jenkins
service jenkins stop
出现这个说明Jenkins停止完成
image.png
6.修改Jenkins默认端口(8080)
Jenkins默认端口是8080,可以进行修改
vi /etc/sysconfig/jenkins
找到 JENKINS_PORT
修改即可
配置
1. 首次进入时,先输入指定位置的密码
2. 插件一般按照推荐安装即可
3. 建立任务
新建任务按照自己的项目分类来建即可,若新建任务中没有mave项目,那么你就需要到插件管理中去安装Maven Integration plugin即可image.png
image.png
配置源码地址
image.png
配置mavenbuild指令和构建后处理器,选择执行shell
image.png
若部署服务器和Jenkins安装服务器不在同一台,则需要安装插件 Publish Over SSH, 选择Send files or execute commands over SSH
image.png
我的start脚本:
source /etc/profile
#!/bin/bash
echo "Restarting Application"
pid=`ps -ef | grep test-app.jar | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
kill -9 $pid
echo "closed process"$pid
fi
echo "remove old file"
cd backup
rm -rf test-app.jar
echo "copy file"
cd ..
if [ -f "test-app.jar" ];then
mv test-app.jar /root/app/backup
fi
cp /var/lib/jenkins/workspace/genrate-app/target/genrate-app.jar /root/app
mv genrate-app.jar test-app.jar
echo "grant auth to test-app.jar"
chmod 777 test-app.jar
export BUILD_ID=dontKillMe
nohup java -jar test-app.jar --spring.config.location=./application.yml > /dev/null 2> /dev/null &
sleep 10
出现的问题
- spring boot jar包shell命令无法启动
解决办法:参见
在启动jar包前增加export BUILD_ID=dontKillMe
,在启动脚本后增加sleep 10
如:
export BUILD_ID=dontKillMe
nohup java -jar test-app.jar --spring.config.location=./application.yml > /dev/null 2> /dev/null &
sleep 10
- 执行shell脚本时提示没有权限或者无法访问某文件夹
解决办法:
jenkins默认使用Jenkins用户启动,修改为root用户启动即可(vi /ect/sysconfig/jenkins 找到JENKINS_USER修改 参见),或者将jenkins用户授予root权限