SEMS-on-device-server/run.ps1

75 lines
3.4 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Param( [string]$cmd, [string]$version)
If ($cmd -eq "build_docker") {
docker build -t sems-on-device-server:latest .
}
If ($cmd -eq "dev" -or [String]::IsNullOrEmpty($cmd) ) {
If (Test-Path "./.next") {
Remove-Item -Path "./.next" -Recurse
Write-Host "./.next is Removed"
}
docker run --rm -p "22110:22110" -v "C:\SEMS-development\SEMS-on-device-server:/app" -v "C:\tmp:/data" --name sems-on-device-server --env-file "C:\SEMS-development\SEMS-system-guardian\.env" --link sems-model-inference sems-on-device-server:latest sh -c "ln -snf /env/node_modules /app && npm run dev"
}
If ($cmd -eq "prod") {
docker run --rm -p "22110:22110" -v "C:\SEMS-development\SEMS-on-device-server\build\latest:/app" -v "C:\tmp:/data" --name sems-on-device-server sems-on-device-server:latest
}
If ($cmd -eq "remove_docker") {
docker stop sems-on-device-server
}
If ($cmd -eq "release") {
# 提前删除.next文件夹根据实测应该会影响后面的编编译原因未知
# 现象单独在powershell命令行执行docker run没问题但是在脚本中执行完全一样的命令就会报错
# 然后分别从命令行/脚本进入容器内docker run --rm -it -v "C:\SEMS-development\SEMS-on-device-server:/app" --name sems-on-device-server sems-on-device-server:latest sh
# 手动执行npm run build发现仍然时命令行可以编译但是脚本会报错找不到库
# 然后发现在脚本环境下npm run build执行前node_model文件夹正常但执行后发现为空白了node_model文件夹下什么都没了
# 猜测是因为脚本执行有安全限制,限制了资源使用?不是特别确定原因。
# 但发现删除了.next后就正常了
If (Test-Path "./.next") {
Remove-Item -Path "./.next" -Recurse
Write-Host "./.next is Removed"
}
#在docker容器内编译
docker run --rm -v "C:\SEMS-development\SEMS-on-device-server:/app" --name sems-on-device-server-build sems-on-device-server:latest sh -c "ln -snf /env/node_modules /app && npm run build"
#.next/static文件夹存在则证明编译完了
If (Test-Path ".next/static") {
#复制static文件夹
Copy-Item -Path .next/static -Destination .next/standalone/.next -Recurse -Force
Copy-Item -Path ./server.js -Destination .next/standalone -Force
Copy-Item -Path ./scanner.js -Destination .next/standalone -Force
#把.next/standalone文件夹下的所有文件打包但是除了这个目录下的node_modules文件因为他是Linux系统下的软连接会报错。
Get-ChildItem -Path ".next/standalone/" | Where-Object { $_.Name -ne "node_modules" } | Compress-Archive -DestinationPath "./build/SEMS-on-device-server-$version.zip" -Force
Write-Host "Released /build/SEMS-on-device-server-$version.zip"
#直接解压一份,便于后面模拟生产环境测试
If (Test-Path "./build/latest") {
#如果当前目录下build文件夹存在
Remove-Item -Path "./build/latest" -Recurse #递归删除build文件夹
}
Expand-Archive -Path "./build/SEMS-on-device-server-$version.zip" -DestinationPath "./build/latest" -Force
Write-Host "Expanded SEMS-on-device-server-$version.zip to ./build/latest"
}
else {
Write-Host "Build Error, Please retry"
}
}