服务器上要安装: git、jdk、maven
方法一
- 获取jenkins源文件
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
- 导入jenkins公钥
rpm --import http://pkg.jenkins.io/redhat-stable/jenkins.io.key
- yum安装jenkins
yum install -y jenkins
- 文件目录说明:
/usr/lib/jenkins/ #jenkins安装目录,WAR包会放在这里。
/etc/sysconfig/jenkins #jenkins配置文件
/var/lib/jenkins/ #默认的JENKINS_HOME。
/var/log/jenkins/jenkins.log #日志文件
方法二
-
安装jdk
https://blog.csdn.net/qq_36493822/article/details/105731717 -
下载 jenkins(选择自己要的版本,赋值链接) wget
https://pkg.jenkins.io/redhat-stable/
wget https://pkg.jenkins.io/redhat-stable/jenkins-2.204.1-1.1.noarch.rpm
- 安装(2选1) sudo rpm -ih
sudo rpm -ih https://pkg.jenkins.io/redhat-stable/jenkins-2.204.1-1.1.noarch.rpm
sudo rpm -ih jenkins-2.204.1-1.1.noarch.rpm
配置属性
- 配置 jdk路径
vim /etc/init.d/jenkins
- 启动 、重启、停止
systemctl start jenkins
service jenkins start
service jenkins restart
service jenkins stop
- 出现警告运行:
systemctl daemon-reload
- 默认8080端口,修改端口/角色 JENKINS_PORT / JENKINS_USER
vim /etc/sysconfig/jenkins
JENKINS_USER 必须是root
- 启动不了
a、端口被占用,修改端口
b、第六点
c、linux 防火墙
d、服务器安全规则(阿里云不去配置会访问不到_自行百度) - 访问 ip+端口
- 安装插件
- 等待安装完成,创建用户
-
登陆后配置:系统管理、全局工具配置
-
新增项目
-
项目配置
- 脚本
#!/bin/bash
//传入的war包名称
name=$1
//war包所在目录
path=$2
//上传的war包位置
path_w=$3
//如果项目正在运行就杀死进程
if [ -f "$path/$name" ];then
echo "delete the file $name"
rm -f $path/$name
else
echo "the file $name is not exist"
fi
//把jenkins上传的war包拷贝到我们所在目录
cp $path_w/$name $path/
echo "copy the file $name from $path_w to $path"
//获取该项目正在运行的pid
pid=$(ps -ef | grep java | grep $name | awk '{print $2}')
echo "pid = $pid"
//如果项目正在运行就杀死进程
if [ $pid ];then
kill -9 $pid
echo "kill the process $name pid = $pid"
else
echo "process is not exist"
fi
//要切换到项目目录下才能在项目目录下生成日志
cd $path
//防止被jenkins杀掉进程 BUILD_ID=dontKillMe
BUILD_ID=dontKillMe
//启动项目
nohup java -server -Xms256m -Xmx512m -jar -Dserver.port=20000 $name >> nohup.out 2>&1 &
//判断项目是否启动成功
pid_new=$(ps -ef | grep java | grep $name | awk '{print $2}')
if [ $? -eq 0 ];then
echo "this application $name is starting pid_new = $pid_new"
else
echo "this application $name startup failure"
fi
echo $! > /var/run/myClass.pid
echo "over"
//端口号
port=30000
//war包名称
name=$1
//本项目所在的路径
path=$2
//jenkins打包所在的路径
path_w=$3
//线程pid名称
pid_name="${4}.pid"
//该项目运行的pid(用于杀死线程)
pid="$(cat /var/run/"${pid_name}")"
#$(ps -ef | grep java | grep $name | awk '{print $2}')
echo "s1 = $name"
echo "s2 = $path"
echo "s3 = $path_w"
echo "pid = $pid"
//删除项目文件下面已有的war
#rm the war if exist
echo "$path/$name"
if [ -f "$path/$name" ];then
echo "delete the file $name"
rm -f $path/$name
else
echo "the file $name is not exist"
fi
sleep 1s
//把刚编译的war包复制到项目所在文件夹下
#copy the war
cp $path_w/$name $path/
echo "copy the file $name from $path_w to $path"
//杀死已有的线程
#shundow the process if exist
if [ $pid ];then
kill -9 $pid
echo "kill the process $name pid = $pid"
else
echo "process is not exist"
fi
sleep 1s
#start the application
#要切换到项目目录下才能在这里生成日志
cd $path
#防止被jenkins杀掉进程
BUILD_ID=dontKillMe
nohup java -server -Xms256m -Xmx512m -jar -Dserver.port=$port $name >> nohup.out 2>&1 &
sleep 1s
//查找新的线程pid
pid_new=$(ps -ef | grep java | grep $name | awk '{print $2}')
//
if [ $? -eq 0 ];then
echo "this application $name is starting pid_new = $pid_new and port = $port"
else
echo "this application $name startup failure"
fi
echo $! > /var/run/$pid_name
echo "over"