1. 程式人生 > 實用技巧 >Jenkins 自動化(CI/DI)部署

Jenkins 自動化(CI/DI)部署

1.背景介紹

在實際開發中,經常要一邊開發一邊測試,經常為了一個功能而不斷更新版本,這些情況都會涉及到頻繁的打包、部署;手動打包的涉及到專案上傳、打包、釋出等很多重複工作;

那麼,有一種工具能夠實現,將程式碼提交到git後就自動打包部署,替換手動認為打包,答案是肯定的:Jenkins ,當然除了Jenkins以外,也還有其他的工具可以實現自動化部署;

2.Jenkins伺服器搭建於基本配置

2.1.Jenkins自動化/持續整合部署實現原理

2.2.Python3.0自動化釋出指令碼

DevOps_remote.py檔案

 #!/usr/bin/env python
#coding=utf- import paramiko import sys #shell 傳入引數 sourcePath = sys.argv[]; #源目錄 targetPath = sys.argv[]; #目標目錄 jarName = sys.argv[]; #檔名 runPort = sys.argv[]; #執行埠 #部署伺服器配置 serveHost = ["192.168.208.131","192.168.208.134","192.168.208.135"]; #目標伺服器IP serveKey = {"192.168.208.131":"?","192.168.208.133":"?","192.168.208.134":"?","192.168.208.135":"?"}; #目標伺服器密碼 serveUser = {"192.168.208.131":"march","192.168.208.133":"march","192.168.208.134":"march","192.168.208.135":"march"}; #目標伺服器登入使用者名稱 serveSSHPort = {"192.168.208.131":,"192.168.208.133":,"192.168.208.134":,"192.168.208.135":}; #目標伺服器SSH 埠 #監聽invoke_shell 互動動態結果 #cmd 互動物件,endParam 結尾物件 def mutualResult(cmd,endParam): buff = ""; while not buff.endswith(endParam): line = cmd.recv(); try: #進行異常捕捉,如果解碼有問題,則換一種解碼方式 buff += line.decode("utf8"); except Exception : buff += line.decode("gb18030"); return buff; #kill 執行埠 def killPort(ssh,port): print ("run cmd >> " + "netstat -tunlp | grep "+port+" | awk '{print $7}' | awk -F'/' '{print $1}'"); stdin, stdout, stderr = ssh.exec_command("netstat -tunlp | grep "+port+" | awk '{print $7}' | awk -F'/' '{print $1}'"); pid = stdout.read().decode(); print(pid) if pid: print ("run cmd >> 關閉 java程式 程式ID: "+pid); ssh.exec_command("kill -9 "+pid); def deploy(): print("開始部署"+jarName); for ip in serveHost: print ("連線伺服器IP:"+ip); #連線伺服器 ssh =paramiko.SSHClient(); ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()); ssh.connect(hostname=ip,port=serveSSHPort[ip],username=serveUser[ip],password=serveKey[ip]); cmd = ssh.invoke_shell(); #連線互動 print (mutualResult(cmd,"$ ")); print ("開始複製"+jarName+" >> 服務:"+ip); print ("run cmd >> scp -r -P 5321
[email protected]
:/var/lib/jenkins/workspace/" +sourcePath+ "/target/"+jarName+" "+ targetPath +"\n"); cmd.send("scp -r -P 5321 [email protected]:/var/lib/jenkins/workspace/" +sourcePath+ "/target/"+jarName+" "+ targetPath +"\n"); #監聽互動並輸入密碼 mutualResult(cmd,"'s password: "); cmd.send("?\n"); mutualResult(cmd,"$ "); cmd.close(); print("run cmd >> scp suceess"); #kill執行程式 killPort(ssh,runPort); #啟動服務 cmd = ssh.invoke_shell(); mutualResult(cmd,"$ "); print ("run cmd >> " + "nohup java -jar " + targetPath + "/" + jarName +" --server.port=" +runPort +" > " + targetPath + "/" + jarName + ".log 2>&1 & \n"); print ("等待伺服器:" + ip + "啟動"); cmd.send("nohup java -jar " + targetPath + "/" + jarName +" --server.port=" +runPort +" > " + targetPath + "/" + jarName + ".log 2>&1 & \n"); mutualResult(cmd,"$ "); print ("核心服務:" + ip + "釋出成功"); cmd.close(); ssh.close(); if __name__ =="__deploy__": deploy() deploy()

DevOps_remote.sh指令碼檔案

 #!/bin/bash

 python3 /var/lib/jenkins/workspace/dev_remote.py $ $ $ $

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.迴圈構建遠端服務完成提示