一、前言

    前面我们已经简介过Jenkins的安装和 利用pipeline完成docker镜像的制作,并且发布到kubernetes,点击查看详情,接下来将介绍一下Jenkins如何发布springboot 项目。

二、准备工作

1  安装jenkins

       点击查看安装步骤

2  服务器环境初始化

  • 创建目录
  mkdir /opt/ops/{new,tmp,project,logs,software,backup,scripts} 
  • 创建python脚本,deploy.py,放在/opt/ops/scripts 目录下
#-*- coding:utf-8 -*-
import os
import subprocess
import sys
import time
date = time.strftime("%Y%m%d-%H%M%S",time.localtime())
BASE_DIR="/opt/project"
BACK_DIR="/opt/ops/backup"
NEW_DIR="/opt/ops/new"

ServerDict={
    "jj-jindun-app":{
        "projectname": "jj-jindun-app",
        "packagename": "jj-jindun-app.jar",
        "port": 8088
        },
    "jj-jindun-back":{
        "projectname": "jj-jindun-back",
        "packagename": "jj-jindun-back.jar",
        "port": 8089
        },
}

print "当前启动项目有:\n%s"%ServerDict.keys()

def deploy(project):
    profile=os.path.join(BASE_DIR,ServerDict[project]["packagename"])
    profile_bk=os.path.join(BACK_DIR,ServerDict[project]["packagename"])+"-"+date
    profile_new=os.path.join(NEW_DIR,ServerDict[project]["packagename"])
    print profile_bk,profile,profile_new
    if os.path.exists(profile):
        os.system("cp -ar %s  %s"%(profile,profile_bk))
    else:
        os.system("cp -ar %s  %s"%(profile_new,profile))
    if os.path.exists(profile):
        print("start")
        os.system("cp -af %s  %s"%(profile_new,profile))
        print("done")
        os.system("supervisorctl restart %s"%project)
        time.sleep(40)
        if not os.system("netstat -antpl|grep -w %s"%ServerDict[project]["port"]):
            os.system("supervisorctl status %s"%project)
            print "%s项目上线Successful"%project
        else:
            os.system("cp -ar %s %s"%(profile_bk,profile))
            os.system("supervisorctl restart %s"%project)
            print "%s项目上线Failed"%project
            sys.exit(1)
            
    else:
        print "项目文件不存在,更新失败"


if __name__ == "__main__":
    if len(sys.argv) == 1:
        print "您必须要输入服务名"
    else:
        for i in sys.argv[1:]:
            if i in ServerDict.keys():
                deploy(i)
            else:
                print "该项目不存在当前服务列表里"

deploy.py
deploy.py

相关文章:

  • 2022-12-23
  • 2021-11-13
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-27
  • 2021-12-09
  • 2021-08-05
  • 2021-04-12
相关资源
相似解决方案