前端指令碼自動化部署(非jenkins)

2021-09-27 10:28:41 字數 4446 閱讀 4935

前端自動化部署原理:

首先從git伺服器拉取前端專案**,然後把拉取來的**打包成dist檔案,把打包好的檔案上傳linux中的nginx伺服器,重啟nginx伺服器。該過程用到git版本控制工具拉取**,用到前端打包命令(需要npm技術)所以要安裝node,需要用到從本地window系統上傳檔案到遠端linux系統用到了putty技術,另外要自動化啟動用到了python技術,最後要啟動nginx伺服器,用到nginx啟動和停止的命令。

可以參考

可以參考

1).linux環境下安裝nginx

(2).離線純淨版redhet6.8安裝nginx

3).vue專案打包部署到tomcat或nginx伺服器

4). vue前端專案用nginx做負載均衡部署

1)在window下的python環境中新增依賴

2)把site-packages的解壓包放在c:\python\lib目錄下

在restart_nginx專案中編寫python**

1) 編寫遠端刪除靜態檔案的**

import paramiko

import os

import time

class linux2(object):

def __init__(self, ip, username, password, timeout=30):

self.ip = ip

self.username = username

self.password = password

self.timeout = timeout

self.t = ''

self.chan = ''

self.try_times = 3

def run_command(self, cmd_line):

ssh = paramiko.sshclient()

ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())

ssh.connect(self.ip, '22', self.username, self.password)

stdin, stdout, stderr = ssh.exec_command(cmd_line)

for line in stdout:

print(line.strip("\n"))

ssh.close()

if __name__ == "__main__":

my_delete= linux2('10.45.4.220', 'ouos', 'ouos')

my_delete.run_command('cd /usr/local/nginx/html;rm -rf ouos')

time.sleep(5)

my_delete.run_command('cd /usr/local/nginx/html;mkdir ouos')

2) 編寫遠端啟動nginx指令碼的編碼
import paramiko

import os

import time

class linux(object):

def __init__(self,ip,username,password,timeout=30):

self.ip=ip

self.username=username

self.password=password

self.timeout=timeout

self.t=''

self.chan=''

self.try_times=3

def run_command(self,cmd_line):

ssh=paramiko.sshclient()

ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())

ssh.connect(self.ip,'22',self.username,self.password)

stdin,stdout,stderr=ssh.exec_command(cmd_line)

for line in stdout:

print(line.strip("\n"))

ssh.close()

if __name__=="__main__":

my_restart=linux('10.45.4.220','ouos','ouos')

my_restart.run_command('cd /usr/local/nginx/sbin;./nginx -s stop')

time.sleep(10)

my_restart.run_command('cd /usr/local/nginx/sbin;./nginx')

1)專案配置檔案sit.env.js
'use strict'

module.exports =

2)總環境啟動指令碼frontend_devops.bat
echo "pull code from git"

call c:\devops\frontend_devops\pull_code.bat

echo "modiify config"

call c:\devops\frontend_devops\modiify_config.bat

echo "start compile..."

call c:\devops\frontend_devops\compile.bat

echo "change dist to ouos"

call c:\devops\frontend_devops\change_ouos.bat

echo "delete_source_file"

call c:\devops\frontend_devops\delete_source_file.bat

echo "transfor file to linux"

call c:\devops\frontend_devops\transfor_file.bat

echo "restart nginx"

call c:\devops\frontend_devops\restart_nginx.bat

3)git拉取**指令碼pull_code.bat
cd c:\devops\code\orientsecuritiesweb

git checkout .

git pull

注意:此步驟是git記住密碼後的操作,如果沒配置記住密碼指令碼啟動時會彈出輸入密碼,阻礙指令碼執行

如果git未記住密碼請參考:

4)修改專案配置指令碼modiify_config.bat

copy /y c:\devops\frontend_devops\sit.env.js c:\devops\code\orientsecuritiesweb\config\sit.env.js
5)專案打包指令碼compile.bat
cd c:\devops\code\orientsecuritiesweb

npm run build

timeout /t 10

6)刪除伺服器中靜態資源
python c:\devops\restart_nginx\venv\delete_source_file.py
7)上傳打包檔案到伺服器指令碼transfor_file.bat
cd c:\program files (x86)\putty  pscp -pw "ouos" -r  c:/devops/code/orientsecuritiesweb/

ouos/ [email protected]:/usr/local/nginx/html/ouos

8)遠端控制nginx啟動restart_nginx.bat
python c:\devops\restart_nginx\venv\operate_linux.py
說明:以上步驟是自動指令碼單節點部署如果是集群需要另外重寫乙份指令碼,也可以繼續重構指令碼**,如果用集群需要用第9步指令碼**,已經寫了乙份複製品,可以按照該方法修改

9)集群啟動指令碼

echo "start frontend_devops"

call c:\devops\frontend_devops\frontend_devops.bat

echo "start frontend_devops2"

call c:\devops\frontend_devops2\frontend_devops\frontend_devops.bat

pause

說明:以上指令碼**對應放置的路徑需要自行對照。

只需要雙擊frontend_devops.bat即可啟動單個nginx伺服器

如果集群環境請使用start_all.bat

啟動指令碼如果遇到git拉取**時需要**認證,參考:

自動化部署指令碼

usr bin env python coding utf 8 import sys,os import subprocess import re import shlex import time filename sys.argv 0 absdir os.path.abspath os.path....

部署python自動化指令碼 搭建自動化指令碼執行環境

年前把平台的指令碼寫好了,準備在虛擬機器中全部執行一遍。沒想到在虛擬機器中配置了很長時間,才把環境搭好。為了不再犯這配置環境的苦,在這簡單說說環境的搭建吧。因為寫的指令碼是在win7上實現的,所以虛擬機器我安裝的也是win7系統。1 安裝python 2 配置環境變數 python安裝成功後,將安裝...

Jenkins SVN 前端自動化部署

jenkins是什麼?昨天之前我也不知道。習慣上班之前刷刷新聞或技術相關知識 了解到jenkins。其實就是乙個自動打包自動部署的乙個工具,不過感覺挺好玩的。我比較菜好了一天多的時間才弄好。如果你也想用那就用吧反正一聽自動化就感覺挺高大上的 jenkins svn win vue專案 和別的軟體一樣...