1

使用 GitHub CI 云构建和自动部署

 3 years ago
source link: https://innei.ren/posts/technology/github-ci-cd
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
使用 GitHub CI 云构建和自动部署
最近这段时间把拖延已久的重构计划重新规划起来了。在经过一周多的努力终于把原先项目结构非常不砍的小站后端重构了一版。
原先的项目有以下几个痛点:
  • node_modules 过于庞大,动辄 1G+
  • 构建时间长,受限于服务器性能
  • 目录结构混乱
最后一个问题可以通过重构和学习他人项目架构解决。而第一个问题也可以使用 pnpm 管理减少依赖的体积。
问题是如何减少构建时间。解决方式:使用 GitHub 云构建,部署服务器复用云构建产物。
确定了目标之后,有以下几个需求需要解决:
  • 构建完自动部署
  • 部署服务器端摆脱 node_modules 的依赖
一般 node 应用构建使用 tsc、webpack 去打包,但是运行时依旧依赖 node_modules,能否像前端应用一样,打包之后摆脱 node_modules。答案是可以。@vercel/ncc 已经发布 0.31.0(截止文章发布前),在原有的基础上修正了很多打包不全,打包文件缺失的问题,经过测试,该版本已经完全符合需求。
需要注意的是,使用 ncc 打包之后,运行端和打包端运行的系统必须一致,比如 ncc 打包的系统是 Ubuntu,理论上运行构建产物的一方系统也必须是 Ubuntu。因为 ncc 会在打包过程中同时打包其他语言编译而成的二进制文件。
构建流程为:
dist/src/main.js
nest build
ncc build dist/src/main.js -o out
经过验证,node_modules 原本为 350M,ncc 打包之后参数只有 30M+。经过 zip 压缩之后可以控制在 10M 以内。
在 GitHub CI 的流程为:
SSH 连接到服务器
手动打 tag 发布一个 release
CI 监视 release 的发布 开始构建和发布
云构建打包产物 zip 发布到 GitHub Release
执行部署脚本
下载构建产物解压
直接运行或使用 PM2 托管
从而很好的摆脱了 node_modules 的依赖,和大大减少了构建时间。甚至在服务端无需构建。奈斯。
完整的 CI 流程可以参考:
https://github.com/mx-space/server-next/blob/master/.github/workflows/release.yml
https://github.com/mx-space/server-next/blob/master/.github/workflows/release.yml
以后 SSH 之后的部署脚本参考:
https://github.com/mx-space/server-next/blob/master/scripts/deploy.js
https://github.com/mx-space/server-next/blob/master/scripts/deploy.js
完整的 GitHub flow 可以查看:
https://github.com/mx-space/server-next/actions/runs/1223394127
https://github.com/mx-space/server-next/actions/runs/1223394127

文章标题: 使用 GitHub CI 云构建和自动部署

文章作者: Innei

文章链接: https://innei.ren/posts/technology/github-ci-cd [复制]

最后修改时间: Invalid Date


商业转载请联系站长获得授权,非商业转载请注明本文出处及文章链接,未经站长允许不得对文章文字内容进行修改演绎。
本文采用创作共用保留署名-非商业-禁止演绎4.0国际许可证

亲亲留个评论再走呗

正在加载评论区...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK