NodeJS前端自動(dòng)化部署實(shí)現(xiàn)實(shí)例詳解
手動(dòng)部署
所需工具
xshell:連接服務(wù)器工具
xftp:圖形化界面?zhèn)鬏斘募ぞ?可有可無,減少命令實(shí)現(xiàn)部分工作
打包并部署
- 打包
npm run build
一般是上方這個(gè)項(xiàng)目打包命令,具體看項(xiàng)目配置
- 部署
采用xshell配合xftp手動(dòng)copy文件覆蓋
- 使用xshell連接云服務(wù)器,
- 再使用nginx指向打包文件的地址路徑,然后將打包好的文件放在指定路徑
自動(dòng)部署
天天重復(fù)上邊的工作無非覺得浪費(fèi)時(shí)間了,乍一看nodejs好像可以幫我們實(shí)現(xiàn)連接服務(wù)器并且上傳文件等,可以省去這部分繁瑣工作,讓我們開始吧!~
安裝依賴
"inquirer": "^8.2.0",
"ssh2-sftp-client": "^9.0.4"
Inquirer
是常規(guī)交互式命令行用戶接口的集合,提供給 Node.js 一個(gè)方便嵌入,漂亮的命令行接口,用于命令行提示交互
ssh2-sftp-client
為 node.js 基于 ssh2 封裝的, SFTP客戶端,用于連接操作服務(wù)器
代碼編寫
在項(xiàng)目根目錄下創(chuàng)建文件夾deploy
方便管理
編寫命令行交互文件helper.js
const inquirer = require('inquirer') new inquirer.Separator() const selectTip = 'project name:' const options = [ { type: 'checkbox', name: selectTip, message: `您希望把項(xiàng)目部署到哪個(gè)環(huán)境?`, choices: [] } ] // 顯示選擇提示窗 function showHelper (config) { console.log("config=",config) return new Promise((resolve, reject) => { initHelper(config) // 初始化helper inquirer.prompt(options).then(answers => { console.log(answers,answers[selectTip],selectTip) resolve({ value: findInfoByName(config,["測(cè)試環(huán)境"]) }) // 查找所選配置項(xiàng) }).catch((err) => { reject(console.error(' helper顯示或選擇出錯(cuò)!', err)) }) }) } // 初始化helper function initHelper (config) { for (let item of config) { options[0].choices.push(item.name) } console.log('正在檢查全局配置信息...') // 檢查是否存在相同name if (new Set(options[0].choices).size !== options[0].choices.length) { console.error('請(qǐng)檢查配置信息,存在相同name!') process.exit() } } // 查找符合條件的配置項(xiàng) function findInfoByName (config, nameArr) { console.log("自定義配置",config,"選中的環(huán)境",nameArr) const arrInfo = [] for (let item of config) { for(let name of nameArr) { console.log(name,item) if(item.name === name) { arrInfo.push(item) } } } return arrInfo } module.exports = showHelper
編寫操作服務(wù)器進(jìn)行上傳發(fā)布文件ssh.js
const Client = require('ssh2-sftp-client') const helper = require ('./helper') const config = [ { name: '測(cè)試環(huán)境', enviroment: 'development', ssh: { host: '你的服務(wù)器地址', port: 22,//端口號(hào) username: '服務(wù)器用戶名', password: '服務(wù)器密碼', }, romotePath: '/lvdu/appHtml',// 遠(yuǎn)程地址 localPath:'../dist',// 本地地址 } ] function connect(config) { const sftp = new Client() return sftp .connect(config.ssh) .then(() => { console.log(`正在部署 ${config.name}`) return sftp.uploadDir(config.localPath, config.romotePath) }).finally(() => { sftp.end() }) } async function main() { const ps = [] const table = [] const SELECT_CONFIG = (await helper(config)).value // 所選部署項(xiàng)目的配置信息 console.log(SELECT_CONFIG) for(let config of SELECT_CONFIG) { table.push({ enviroment: config.enviroment, status: 'OK' }) ps.push(() => connect(config)) } const p = Promise.resolve() ps.reduce((p, c) => { return p.then(c) }, p).then(() => { console.log('success completed') console.table(table); }).catch((err) => { console.log(err,'出了點(diǎn)問題,快去看看吧~~') }) }
main()
最后可編寫腳本文件deploy.sh
用于執(zhí)行上方文件,也可手動(dòng)執(zhí)行
echo "正在打包 "
npm run build
node ./ssh.js
如果手動(dòng)執(zhí)行就需要先
npm run build
再 node ssh.js
該方式只適用于本地個(gè)人使用,切勿上傳倉(cāng)庫(kù),防止服務(wù)器賬號(hào)密碼泄露
以上就是NodeJS前端自動(dòng)化部署實(shí)現(xiàn)實(shí)例詳解的詳細(xì)內(nèi)容,更多關(guān)于NodeJS前端自動(dòng)化部署的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Node.js的強(qiáng)大爬蟲 能直接發(fā)布抓取的文章哦
基于Node.js的強(qiáng)大爬蟲能直接發(fā)布抓取的文章哦!本爬蟲源碼基于WTFPL協(xié)議,感興趣的小伙伴們可以參考一下2016-01-01npm dose not support Node.js v10.15
這篇文章主要給大家介紹了關(guān)npm dose not support Node.js v10.15.3的解決方法,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11node.js利用socket.io實(shí)現(xiàn)多人在線匹配聯(lián)機(jī)五子棋
這篇文章主要介紹了node.js利用socket.io實(shí)現(xiàn)多人在線匹配聯(lián)機(jī)五子棋的操作方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05nodejs和php實(shí)現(xiàn)圖片訪問實(shí)時(shí)處理
這篇文章主要為大家詳細(xì)介紹了nodejs和php分別實(shí)現(xiàn)圖片訪問實(shí)時(shí)處理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01從零學(xué)習(xí)node.js之express入門(六)
相信大家都知道Express是一個(gè)簡(jiǎn)潔而靈活的 node.js Web應(yīng)用框架, 提供了一系列強(qiáng)大特性幫助你創(chuàng)建各種 Web 應(yīng)用,和豐富的 HTTP 工具。下面這篇文章主要介紹了node.js中express的入門知識(shí),需要的朋友可以參考下。2017-02-02