• vue配置多个服务端请求地址并实现scp2自动部署


    前言:通常情况下,一个项目会有开发环境、内网环境、测试环境、生产环境等多个环境,通过请求不同的服务器来获取数据,如果可不修改代码,通过执行不同命令就能着急部署不同环境,我们的效率会得到很大提升。

    实现:

    一、配置多个服务器端请求地址

    • 在serve文件夹(随便取名)下新建一个config.js文件;

    • 在config.js中不同服务的地址都配置出来;
    1. const getServerApi = () => {
    2. let info = {};
    3. // 当node内置的环境变量 process.env.NODE_ENV存在时进入
    4. switch (process.env.NODE_ENV) {
    5. case 'test1': //开发环境
    6. info.baseUrl = '192.168.xx.xx:1001/test1'; //基础地址
    7. info.portalUrl = '139.xxx.xx.xx:8091'; //登录地址
    8. break;
    9. case 'test2': //内网环境
    10. info.baseUrl = '192.168.xx.xx:1002/test2';
    11. info.portalUrl = '139.xxx.xx.xx:8091';
    12. break;
    13. case 'test3': //测试环境
    14. info.baseUrl = '192.168.xx.xx:1003/test3';
    15. info.portalUrl = '139.xxx.xx.xx:8091';
    16. break;
    17. case 'production': //生产环境
    18. info.baseUrl = '218.xx.xx.xxx:8090';
    19. info.portalUrl = '218.xx.xx.xxx:8088';
    20. break;
    21. default: //默认
    22. info.baseUrl = '192.168.xx.xx:1001/test1';
    23. info.portalUrl = '139.xxx.xx.xx:8091';
    24. break;
    25. }
    26. return info;
    27. };
    28. //默认导出基础地址和登录地址
    29. export default {
    30. baseUrl: `//${getServerApi().baseUrl}`,
    31. portal: `//${getServerApi().portal}`,
    32. };
    • 在封装了axios的http.js文件中使用相关地址;
    1. import axios from 'axios';
    2. import config from './config' //引入配置的服务地址
    3. const BASE_URI = config.baseUrl; //使用基础路径

    二、scp2自动化部署项目至服务器实现

    • 安装scp2
    npm install scp2 --save-dev
     npm i --save-dev cross-env cross-env 
    
    • 项目根目录下创建deploy文件夹,在deploy文件夹下创建index.js和products.js文件;

    •  使用scp2库,创建自动化部署脚本。index.js文件内容如下:
    1. // deploy/index.js里面
    2. const scpClient = require('scp2');
    3. const ora = require('ora'); //一个优雅的终端微调器
    4. const chalk = require('chalk'); //颜色插件
    5. const server = require('./products');
    6. const spinner = ora('正在发布到' + server.name + '服务器...');
    7. var Client = require('ssh2').Client; //连接远程服务器
    8. var conn = new Client();
    9. conn.on('ready', function() {
    10. // rm 删除dist文件,\n 是换行 换行执行 重启nginx命令 我这里是用docker重启nginx
    11. conn.exec('cd ' + server.path + '\nrm -rf css img js fonts index.html', function(err, stream) {
    12. if (err) throw err;
    13. stream
    14. .on('close', function(code, signal) {
    15. // 在执行shell命令后,把开始上传部署项目代码放到这里面
    16. spinner.start();
    17. scpClient.scp(
    18. './dist',
    19. {
    20. host: server.host,
    21. port: server.port,
    22. username: server.username,
    23. password: server.password,
    24. path: server.path,
    25. },
    26. function(err) {
    27. spinner.stop();
    28. if (err) {
    29. console.log(chalk.red('发布失败.\n'));
    30. throw err;
    31. } else {
    32. console.log(chalk.green('Success! 成功发布到' + server.name + '服务器! \n'));
    33. }
    34. }
    35. );
    36. conn.end();
    37. })
    38. .on('data', function(data) {
    39. console.log('STDOUT: ' + data);
    40. })
    41. .stderr.on('data', function(data) {
    42. console.log('STDERR: ' + data);
    43. });
    44. });
    45. })
    46. .on('error', function(err) {
    47. console.log('xxx' + err);
    48. })
    49. .connect({
    50. host: server.host,
    51. port: server.port,
    52. username: server.username,
    53. password: server.password,
    54. });
    • products.js文件内容如下:
    1. /*
    2. *定义多个服务器账号 及 根据 SERVER_ID 导出当前环境服务器账号
    3. */
    4. const SERVER_LIST = [{
    5. id: 'text1',
    6. name: '开发环境',
    7. host: '139.xx.xx.xxx', // 服务器ip
    8. port: 22, // 登录服务器端口,一般为22 或 21
    9. username: 'root', // 登录服务器的账号
    10. password: 'xxxxxx', // 登录服务器的账号
    11. path: '/usr/local/nginx/html/xxx' // 项目上传到服务器的路径,要服务器上的绝对路径
    12. },
    13. {
    14. id: 'test2',
    15. name: '内网环境',
    16. host: '139.xxx.xxx.xxx',
    17. port: 22,
    18. username: 'root',
    19. password: 'xxxxxx',
    20. path: '/etc/nginx/html/test2/xxx'
    21. },
    22. {
    23. id: 'test3',
    24. name: '测试环境',
    25. host: '139.xxx.xxx.xxx',
    26. port: 22,
    27. username: 'root',
    28. password: 'xxxxxx',
    29. path: '/etc/nginx/html/test3/xxx'
    30. },
    31. {
    32. id: 'production',
    33. name: '生产环境',
    34. host: '218.xxx.xxx.xxx',
    35. port: 22,
    36. username: 'root',
    37. password: 'xxxxxx',
    38. path: '/etc/nginx/dist'
    39. },
    40. ];
    41. /**
    42. *读取env环境变量
    43. */
    44. const getServer = () => {
    45. return SERVER_LIST.find(item => {
    46. return item.id == process.env.NODE_ENV
    47. })
    48. }
    49. module.exports = getServer();
    •   配置 script 脚本,执行npm run deploy:test1则表示运行生产环境,其他环境根据对应的选择执行不同命令。
    1. "scripts": {
    2. "serve": "vue-cli-service serve",
    3. "build": "vue-cli-service build",
    4. "lint": "vue-cli-service lint",
    5. "deploy:test1": "cross-env NODE_ENV=test1 cnpm run build && cross-env NODE_ENV=test1 node ./deploy",
    6. "deploy:test2": "cross-env NODE_ENV=test2 cnpm run build && cross-env NODE_ENV=test2 node ./deploy",
    7. "deploy:test3": "cross-env NODE_ENV=test3 cnpm run build && cross-env NODE_ENV=test3 node ./deploy",
    8. "build:production": "cross-env NODE_ENV=production vue-cli-service build"
    9. },
    • 测试是否生效:运行命令 npm run deploy:test2 部署到测试环境,如下提示则表示部署成功。

     

  • 相关阅读:
    【Flask使用】全知识md文档,4大部分60页第3篇:状态cookie和session保持
    Anaconda配置pip源
    [附源码]java毕业设计校园爱心支愿管理系统
    Springboot
    window下VS2022封装静态库以及调用静态库
    《UnityShader入门精要》学习1
    jmeter接口压力测试-(二)
    Qt界面容器:Widget、 Frame、分组框、滚动区、工具箱、选项卡小部件、堆叠小部件控件精讲
    企业电子招标采购系统源码Spring Boot + Mybatis + Redis + Layui + 前后端分离 构建企业电子招采平台之立项流程图
    5- FreeRTOS任务通知
  • 原文地址:https://blog.csdn.net/weixin_47978760/article/details/126656921