# nodejs-auto-deploy **Repository Path**: lijinbode/nodejs-auto-deploy ## Basic Information - **Project Name**: nodejs-auto-deploy - **Description**: nodejs 代码自动部署脚本 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-30 - **Last Updated**: 2026-02-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # nodejs-auto-deploy nodejs 自动部署脚本 # 快速开始 ## 1.拉取代码 初始化代码并使用 `npm install` 初始化 ## 2.新增服务器配置 在当前项目src/config目录下创建 `serversConfig.js` 服务器配置文件,示例内容如下: ```js /** * 服务器配置 * @type {Object.} */ module.exports = { 'local-253': { host: '192.168.2.253', port: 22, username: 'root', password: '123456' } // ... 更多服务器配置 } ``` ## 3.构建 执行 `npm run build` 构建项目 ## 4.创建部署脚本 在需要部署的项目目录下创建 `deploy.js` 内容如下,然后执行 `node deploy.js` 即可 ```js const path = require('path') const { ssh2Deploy, execLocalCommand } = require('C:/nodejs-auto-deploy/dist/ssh2Deploy.js') ;(async () => { const projectPath = path.resolve(__dirname, '../../') console.time('build') await execLocalCommand('npm run build', projectPath) console.timeEnd('build') await ssh2Deploy({ serverName: 'local-253', filePath: path.join(projectPath, './dist.tar.gz'), deployDir: '/usr/nginx/web', backup: 'none' }) console.log('deploy success') })() ``` ## execLocalCommand 执行本地命令 | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ------------------ | | command | string | 是 | 要执行的命令 | | cwd | string | 是 | 命令执行的工作目录 | ## ssh2Deploy options参数说明 | 参数名 | 类型 | 必填 | 默认值 | 说明 | | ----------- | --------------- | ---- | ------ | ----------------------------------------------------- | | serverName | string | 是 | - | 服务器名称(对应serversConfig中的键名) | | filePath | string | 是 | - | 本地文件/目录路径(如果是目录则会自动压缩) | | deployDir | string/string[] | 是 | - | 服务器上的部署目录,如果是数组则都会部署 | | zipName | string | 是 | - | 压缩文件名(如果是目录则会自动压缩) | | confirm | string | 否 | - | 部署确认(可配置确认输入的文本) | | backup | string | 否 | none | 备份方式 remote 远程备份、local 本地备份、none 不备份 | | connectTest | boolean | 否 | false | 服务器连接部署测试 |