Jenkins 自动化(CI/DI)部署
Posted march74
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins 自动化(CI/DI)部署相关的知识,希望对你有一定的参考价值。
1. 背景介绍
在实际开发中,经常要一边开发一边测试,经常为了一个功能而不断更新版本,这些情况都会涉及到频繁的打包、部署;手动打包的涉及到项目上传、打包、发布等很多重复工作;
那么,有一种工具能够实现,将代码提交到git后就自动打包部署,替换手动认为打包,答案是肯定的:Jenkins ,当然除了Jenkins以外,也还有其他的工具可以实现自动化部署;
2. Jenkins服务器搭建于基本配置
2.1. Jenkins自动化/持续集成部署实现原理
2.2. Python3.0自动化发布脚本
DevOps_remote.py文件
1 #!/usr/bin/env python 2 #coding=utf-8 3 4 import paramiko 5 6 import sys 7 8 9 10 #shell 传入参数 11 12 sourcePath = sys.argv[1]; #源目录 13 14 targetPath = sys.argv[2]; #目标目录 15 16 jarName = sys.argv[3]; #文件名 17 18 runPort = sys.argv[4]; #运行端口 19 20 21 22 #部署服务器配置 23 24 serveHost = ["192.168.208.131","192.168.208.134","192.168.208.135"]; #目标服务器IP 25 26 serveKey = {"192.168.208.131":"?","192.168.208.133":"?","192.168.208.134":"?","192.168.208.135":"?"}; #目标服务器密码 27 28 serveUser = {"192.168.208.131":"march","192.168.208.133":"march","192.168.208.134":"march","192.168.208.135":"march"}; #目标服务器登录用户名 29 30 serveSSHPort = {"192.168.208.131":5321,"192.168.208.133":5321,"192.168.208.134":5321,"192.168.208.135":5321}; #目标服务器SSH 端口 31 32 33 34 #监听invoke_shell 交互动态结果 35 36 #cmd 交互对象,endParam 结尾对象 37 38 def mutualResult(cmd,endParam): 39 40 buff = ""; 41 42 while not buff.endswith(endParam): 43 44 line = cmd.recv(9999); 45 46 try: 47 48 #进行异常捕捉,如果解码有问题,则换一种解码方式 49 50 buff += line.decode("utf8"); 51 52 except Exception : 53 54 buff += line.decode("gb18030"); 55 56 return buff; 57 58 59 60 #kill 运行端口 61 62 def killPort(ssh,port): 63 64 print ("run cmd >> " + "netstat -tunlp | grep "+port+" | awk ‘{print $7}‘ | awk -F‘/‘ ‘{print $1}‘"); 65 66 stdin, stdout, stderr = ssh.exec_command("netstat -tunlp | grep "+port+" | awk ‘{print $7}‘ | awk -F‘/‘ ‘{print $1}‘"); 67 68 pid = stdout.read().decode(); 69 70 print(pid) 71 72 if pid: 73 74 print ("run cmd >> 关闭 java进程 进程ID: "+pid); 75 76 ssh.exec_command("kill -9 "+pid); 77 78 79 80 def deploy(): 81 82 print("开始部署"+jarName); 83 84 for ip in serveHost: 85 86 print ("连接服务器IP:"+ip); 87 88 #连接服务器 89 90 ssh =paramiko.SSHClient(); 91 92 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()); 93 94 ssh.connect(hostname=ip,port=serveSSHPort[ip],username=serveUser[ip],password=serveKey[ip]); 95 96 cmd = ssh.invoke_shell(); 97 98 #连接交互 99 100 print (mutualResult(cmd,"$ ")); 101 102 print ("开始复制"+jarName+" >> 服务:"+ip); 103 104 print ("run cmd >> scp -r -P 5321 march@192.168.208.133:/var/lib/jenkins/workspace/" +sourcePath+ "/target/"+jarName+" "+ targetPath +" "); 105 106 cmd.send("scp -r -P 5321 march@192.168.208.133:/var/lib/jenkins/workspace/" +sourcePath+ "/target/"+jarName+" "+ targetPath +" "); 107 108 #监听交互并输入密码 109 110 mutualResult(cmd,"‘s password: "); 111 112 cmd.send("? "); 113 114 mutualResult(cmd,"$ "); 115 116 cmd.close(); 117 118 print("run cmd >> scp suceess"); 119 120 121 122 #kill运行进程 123 124 killPort(ssh,runPort); 125 126 127 128 #启动服务 129 130 cmd = ssh.invoke_shell(); 131 132 mutualResult(cmd,"$ "); 133 134 print ("run cmd >> " + "nohup java -jar " + targetPath + "/" + jarName +" --server.port=" +runPort +" > " + targetPath + "/" + jarName + ".log 2>&1 & "); 135 136 print ("等待服务器:" + ip + "启动"); 137 138 cmd.send("nohup java -jar " + targetPath + "/" + jarName +" --server.port=" +runPort +" > " + targetPath + "/" + jarName + ".log 2>&1 & "); 139 140 mutualResult(cmd,"$ "); 141 142 print ("核心服务:" + ip + "发布成功"); 143 144 145 146 cmd.close(); 147 148 ssh.close(); 149 150 151 152 if __name__ =="__deploy__": 153 154 deploy() 155 156 deploy()
DevOps_remote.sh脚本文件
1 #!/bin/bash 2 3 python3 /var/lib/jenkins/workspace/dev_remote.py $1 $2 $3 $4
2.3. Jenkins 环境插件
2.3.1. 这里建议选择推荐安装,保证基本常用功能可以使用
2.3.2. 全局工具配置(Global Tool Configuration)
2.3.3. 系统配置
配置发布通知邮箱
2.3.4. 插件安装
3. Jenkins自动化部署
3.1. Springboot项目发布部署(git)
3.2. 构建完成后自动打Tag
3.3. Tag项目发布部署
使用参数构建:
3.4. 静态代码审查之fireline
配置:
代码审查结果:
3.5. 静态代码审查之checkstyle
pom.xml配置:
<dependency> <groupId>com.puppycrawl.tools</groupId> <artifactId>checkstyle</artifactId> <version>8.33</version> </dependency> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.1.1</version> <configuration> <consoleOutput>true</consoleOutput> <outputFileFormat>xml</outputFileFormat> <!-- <configLocation>sun_checks.xml</configLocation>--> <configLocation>google_checks.xml</configLocation> <linkXRef>false</linkXRef> </configuration> <dependencies> <dependency> <groupId>com.puppycrawl.tools</groupId> <artifactId>checkstyle</artifactId> <version>8.33</version> </dependency> </dependencies> </plugin> <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.1.1</version> <configuration> <configLocation>google_checks.xml</configLocation> </configuration> </plugin> </plugins> </reporting>
Jenkins配置:
代码审查结果:
3.6. WebHook自动触发构建
GITHub自动推送
3.7. 循环构建远程服务完成提示
以上是关于Jenkins 自动化(CI/DI)部署的主要内容,如果未能解决你的问题,请参考以下文章
再见 Jenkins!几行脚本搞定自动化部署,这款神器有点厉害