Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
corporateCulture-qd
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王立鹏
corporateCulture-qd
Commits
b2386801
Commit
b2386801
authored
Nov 28, 2025
by
lijiabin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
【需求 17679】 fix: 修改部署脚本 区分 测试和正式
parent
84a098a3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
179 additions
and
2 deletions
+179
-2
deployprod.js
deploy/deployprod.js
+175
-0
deploytest.js
deploy/deploytest.js
+4
-2
No files found.
deploy/deployprod.js
0 → 100644
View file @
b2386801
import
cp
from
'node:child_process'
import
ssh
from
'ssh2'
import
archiver
from
'archiver'
import
fs
from
'node:fs'
import
path
from
'node:path'
const
unzipDirMode
=
{
spawn
:
'npm run build:prod'
,
// 解压文件名
unzipDir
:
'culture/'
,
// 终端定位位置 cd
servicePath
:
'/usr/local/nginx/'
,
// 压缩包存放位置
serviceFilePath
:
'/usr/local/nginx/dist.tar.gz'
,
}
const
__dirname
=
path
.
resolve
()
// 文件所在地
const
distPath
=
path
.
resolve
(
__dirname
,
'dist'
)
// 打包后位置
const
zipPath
=
path
.
resolve
(
__dirname
,
'dist.tar.gz'
)
// 远程服务器存放位置
const
{
spawn
,
servicePath
,
serviceFilePath
,
unzipDir
}
=
unzipDirMode
// 服务器连接信息
const
connectInfo
=
{
host
:
''
,
// 服务器地址
port
:
'22'
,
username
:
'root'
,
password
:
''
,
// 服务器密码
}
//链接服务器
let
conn
=
new
ssh
.
Client
()
async
function
start
()
{
try
{
// 1. 打包
await
build
()
// 2. 压缩zip
await
startZip
()
// 3. 将zip文件传输至远程服务器
await
connect
()
// 4. 部署解压
await
shellCmd
(
conn
)
console
.
log
(
'部署完成'
)
}
catch
(
error
)
{
console
.
error
(
'Error:'
,
error
.
message
)
}
finally
{
// 5. 断开ssh,并删除本地压缩包
conn
.
end
()
delZip
()
}
}
start
()
/**
* 1. 本地构建项目
*/
function
build
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
//对项目进行打包,然后生成压缩文件
let
pro
=
cp
.
spawn
(
spawn
,
{
shell
:
true
,
stdio
:
'inherit'
,
})
pro
.
on
(
'exit'
,
(
code
)
=>
{
//打包完成后 开始链接目标服务器,并自动部署
if
(
code
===
0
)
{
console
.
log
(
'---构建成功---'
)
resolve
()
}
else
{
reject
(
new
Error
(
'构建失败'
))
}
})
})
}
/**
* 2. 将打包后文件压缩zip
* @returns
*/
function
startZip
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
console
.
log
(
'开始打包tar'
)
//定义打包格式和相关配置
const
archive
=
archiver
(
'tar'
,
{
gzip
:
true
,
// 如果需要压缩,可以使用 gzip
gzipOptions
:
{
level
:
9
},
// gzip 压缩级别
}).
on
(
'error'
,
(
err
)
=>
reject
(
err
))
console
.
log
(
'zipPath'
,
zipPath
)
const
output
=
fs
.
createWriteStream
(
zipPath
)
//监听流的打包
output
.
on
(
'close'
,
(
err
)
=>
{
console
.
log
(
'err'
,
err
)
console
.
log
(
'目标打包完成'
)
resolve
(
true
)
})
//开始压缩
archive
.
pipe
(
output
)
// 文件夹压缩
archive
.
directory
(
distPath
,
false
)
archive
.
finalize
()
})
}
/**
* 3. 将zip文件传输至远程服务器
*/
function
connect
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
conn
.
on
(
'ready'
,
()
=>
{
conn
.
sftp
((
err
,
sftp
)
=>
{
if
(
err
)
{
return
reject
(
err
)
}
sftp
.
fastPut
(
zipPath
,
serviceFilePath
,
{},
(
err
,
result
)
=>
{
if
(
err
)
{
return
reject
(
err
)
}
//开始上传
console
.
log
(
'文件上传成功'
)
resolve
()
})
})
})
.
on
(
'error'
,
(
err
)
=>
reject
(
err
))
.
connect
(
connectInfo
)
})
}
/**
* 4. 解压部署操作
* @param {*} conn
*/
async
function
shellCmd
(
conn
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
conn
.
shell
((
err
,
stream
)
=>
{
if
(
err
)
{
return
reject
(
err
)
}
//进入服务器暂存地址
//解压上传的压缩包
//移动解压后的文件到发布目录
//删除压缩包
//退出
const
commands
=
`
cd
${
servicePath
}
&&
mkdir -p
${
unzipDir
}
&&
tar -xzf dist.tar.gz -C
${
unzipDir
}
&&
rm -rf dist.tar.gz &&
exit
`
console
.
log
(
'终端执行命令:'
,
commands
)
stream
.
on
(
'close'
,
()
=>
resolve
())
.
on
(
'data'
,
(
data
)
=>
console
.
log
(
data
.
toString
()))
.
stderr
.
on
(
'data'
,
(
data
)
=>
console
.
error
(
'Error:'
,
data
.
toString
()))
stream
.
end
(
commands
)
})
})
}
/**
* 5. 删除本地的dist.zip
*/
function
delZip
()
{
fs
.
unlink
(
zipPath
,
function
(
err
)
{
if
(
err
)
{
console
.
error
(
'删除文件失败:'
,
err
.
message
)
}
console
.
log
(
'文件:'
+
zipPath
+
'删除成功!'
)
})
}
deploy/deploytest.js
View file @
b2386801
...
...
@@ -5,7 +5,7 @@ import fs from 'node:fs'
import
path
from
'node:path'
const
unzipDirMode
=
{
spawn
:
'
p
npm run build:test'
,
spawn
:
'npm run build:test'
,
// 解压文件名
unzipDir
:
'culture/'
,
// 终端定位位置 cd
...
...
@@ -13,6 +13,7 @@ const unzipDirMode = {
// 压缩包存放位置
serviceFilePath
:
'/usr/local/nginx/dist.tar.gz'
,
}
const
__dirname
=
path
.
resolve
()
// 文件所在地
const
distPath
=
path
.
resolve
(
__dirname
,
'dist'
)
...
...
@@ -86,10 +87,11 @@ function startZip() {
gzip
:
true
,
// 如果需要压缩,可以使用 gzip
gzipOptions
:
{
level
:
9
},
// gzip 压缩级别
}).
on
(
'error'
,
(
err
)
=>
reject
(
err
))
console
.
log
(
'zipPath'
,
zipPath
)
const
output
=
fs
.
createWriteStream
(
zipPath
)
//监听流的打包
output
.
on
(
'close'
,
(
err
)
=>
{
console
.
log
(
'err'
,
err
)
console
.
log
(
'目标打包完成'
)
resolve
(
true
)
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment