jenkins安装使用流程(结合springbootdemo)

首先转载这位大哥的流程:

https://www.cnblogs.com/rmxd/p/11609983.html
非常详细

补充一些细节:

1安装插件时
	要是很多下载不了,不要慌,continue,然后进操作界面之后再下载,有可能你得版本比较老,jenkins可以在线更新,看起来很慢,等你没耐心了你可以重启一下试试,可能就下好了
jenkins重启方法:1.ip:8080/restart
			   2.服务器上 service jenkins restart
2查找服务器git路经	which git
3ssh配置时,没有或者不知道密钥(我就不知道)		
		注意:下面有账号密码登录的方式
		然后点击test测试一下
4构建触发器的时候,github和gitee不一样,后者wbhook要密码,前者不要,不要的好像方便点,
这边钩子函数要是出错了好像不会自动拉代码,但是我爆红了一样能跑,天佑之人
5构建环境的时候
		生成的jar包最好和脚本文件在一个文件下面方便有问题测试
6最重要的就是脚本了,网上版本太多了,分享两个版本

脚本一
启动代码
sh 脚本.sh [APP_NAME] [start|stop|restart|status]

#!/bin/sh

# 应用名称:命令行输入的第一个参数
APP_NAME=$1
# 日志文件路径
LOG_NAME=./info.log
 
# 使用说明,用来提示输入参数
usage() {
    echo "Usage: sh 脚本.sh [APP_NAME] [start|stop|restart|status]"
    exit 1
}

# 检查程序是否在运行
is_exist(){
        # 获取PID
        PID=$(ps -ef |grep ${APP_NAME} | grep -v $0 |grep -v grep |awk '{print $2}')

        # -z "${pid}"判断pid是否存在,如果不存在返回1,存在返回0
        if [ -z "${PID}" ]; then
                # 如果进程不存在返回1
                return 1
        else
                # 进程存在返回0
                return 0
        fi
}
 
# 定义启动程序函数
start(){
        is_exist
        if [ $? -eq "0" ]; then
                echo "${APP_NAME} is already running, PID=${PID}"
        else
                # 真正的启动命令,如果有其他参数,可以在此添加
                nohup java -jar ${APP_NAME} >${LOG_NAME} 2>&1 &
                PID=$(echo $!)
                echo "${APP_NAME} start success, PID=$!"
        fi
}
 
# 停止进程函数
stop(){
        is_exist
        if [ $? -eq "0" ]; then
                # 强制关机
                kill -9 ${PID}
                # actuator提供的优雅关机:/actuator/shutdown,以下为自定义路径
                #curl -X POST http://127.0.0.1:40000/MyActuator/shutdown
                echo "${APP_NAME} process stop, PID=${PID}"
        else    
                echo "There is not the process of ${APP_NAME}"
        fi
}
 
# 重启进程函数 
restart(){
        stop
        start
}
 
# 查看进程状态
status(){
        is_exist
        if [ $? -eq "0" ]; then
                echo "${APP_NAME} is running, PID=${PID}"
        else    
                echo "There is not the process of ${APP_NAME}"
        fi
}
 
case $2 in
"start")
        start
        ;;
"stop")
        stop
        ;;
"restart")
        restart
        ;;
"status")
       status
        ;;
  *)
  usage
  ;;
esac
exit 0

脚本二
启动代码
sh restart.sh [start,stop,restart]
改一下JAR_NAME,main.log换成你的项目文件

#!/bin/bash
# 定义变量
# 要运行的jar包路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到
JAR_NAME="/pick/jenkins-0.0.1-SNAPSHOT.jar"
# 日志路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到
LOG_PATh=/pick/main.log


# 如果输入格式不对,给出提示!
tips() {
        echo ""
        echo "WARNING!!!......Tips, please use command: sh auto_deploy.sh [start|stop|restart|status].   For example: sh auto_deploy.sh start  "
        echo ""
        exit 1
}


# 启动方法
start() {
        # 重新获取一下pid,因为其它操作如stop会导致pid的状态更新
        pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
        # -z 表示如果$pid为空时执行
        if [ -z $pid ]; then
        nohup /pick/jdk1.8.0_131/bin/java -jar $JAR_NAME > /dev/null 2>&1 &
        pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
                echo ""
        echo "Service ${JAR_NAME} is starting!pid=${pid}"
                echo "........................Here is the log.............................."
                echo "....................................................................."
        tail -f $LOG_PATh
                echo "........................Start successfully!........................."
        else
                echo ""
                echo "Service ${JAR_NAME} is already running,it's pid = ${pid}. If necessary, please use command: sh auto_deploy.sh restart."
                echo ""
        fi
}

# 停止方法
stop() {
                # 重新获取一下pid,因为其它操作如start会导致pid的状态更新
        pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
        # -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
        if [ -z $pid ]; then
                echo ""
        echo "Service ${JAR_NAME} is not running! It's not necessary to stop it!"
                echo ""
        else
                kill -9 $pid
                echo ""
                echo "Service stop successfully!pid:${pid} which has been killed forcibly!"
                echo ""
        fi
}

# 输出运行状态方法
status() {
        # 重新获取一下pid,因为其它操作如stop、restart、start等会导致pid的状态更新
        pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
        # -z 表示如果$pid为空时执行。注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
        if [ -z $pid ];then
                echo ""
        echo "Service ${JAR_NAME} is not running!"
                echo ""
        else
                echo ""
        echo "Service ${JAR_NAME} is running. It's pid=${pid}"
                echo ""
        fi
}

# 重启方法
restart() {
        echo ""
        echo ".............................Restarting.............................."
        echo "....................................................................."
                # 重新获取一下pid,因为其它操作如start会导致pid的状态更新
        pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
        # -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
        if [ ! -z $pid ]; then
                kill -9 $pid
        fi
        start
        echo "....................Restart successfully!..........................."
}

# 根据输入参数执行对应方法,不输入则执行tips提示方法
case "$1" in
   "start")
     start
     ;;
   "stop")
     stop
     ;;
   "status")
     status
     ;;
   "restart")
     restart
     ;;
   *)
     tips
     ;;
esac

7脚本文件对应的java环境都得换成绝对路径 /jdk/bin/java -jar 这种,另外jenkins填写脚本命令最好是restart
8构建中没有maven版本时,全局配置的最下面,maven新增,看看是不是没有配置
9项目测试的时候,最好是先用脚本运行一下jar包,看看能不能运行,脚本能运行但是jenkins不能一般是jdk写的相对路径,看第七条
10注意服务器端口有没有打开,
centos7
检查端口:lsof -i:8082
重新开启端口命令:firewall-cmd --zone=public --add-port=8082/tcp --permanent
刷新:firewall-cmd --reload
先这样

关于wbhook无法自动推送的问题:
建议看下这篇大佬的文章
亲测有效,不过自动构建慢了些,明天看看原因

https://juejin.im/post/6872696963939516424#comment
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值