Jenkins 自動化(CI/DI)部署

非尚發表於2020-07-07

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 +"\n");
105 
106         cmd.send("scp -r -P 5321 march@192.168.208.133:/var/lib/jenkins/workspace/" +sourcePath+ "/target/"+jarName+" "+ targetPath +"\n");
107 
108         #監聽互動並輸入密碼
109 
110         mutualResult(cmd,"'s password: ");
111 
112         cmd.send("?\n");
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 & \n");
135 
136         print ("等待伺服器:" + ip + "啟動");
137 
138         cmd.send("nohup java -jar " + targetPath + "/" + jarName +" --server.port=" +runPort +" > " + targetPath + "/" + jarName + ".log 2>&1 & \n");
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. 迴圈構建遠端服務完成提示

 

 

 

 

 

 

 

相關文章