如何优化JS/TS编译+S3部署的GitHub Action构建部署速度?
优化GitHub Action中SWC Spack编译打包步骤的耗时方案
问题背景
我有一个GitHub Action,负责使用SWC的Spack模式编译打包JS/TS文件并上传至AWS S3存储桶。当前“build-and-deploy”任务平均耗时60秒,经耗时分析:
- s3-sync-action步骤耗时24秒,暂无法直接优化
- “Build distributable”步骤最高耗时15秒,计划从此环节入手优化
当前配置使用Yarn安装依赖、Node.js v12;SWC Spack模式因需生成多入口输出,暂无法更换。原Action配置如下:
name: Bundle distributable on: push: jobs: # Builds the new distributable files (.js) and commit them to the repo build-and-deploy: runs-on: ubuntu-latest steps: - name: Expose GitHub slug/short variables uses: rlespinasse/github-slug-action@v3.x - uses: actions/checkout@v2 - name: Build distributable run: NODE_ENV=production yarn install --production --frozen-lockfile && yarn build - name: Upload files to AWS S3 and CloudFront and invalidate CF cache if: env.GITHUB_REF_SLUG == 'main' uses: kefranabg/s3-sync-action@02d8eaf34efb8bd76abe2e5614ba3d8028de219e with: args: --acl public-read --follow-symlinks --delete env: AWS_S3_BUCKET: '' AWS_ACCESS_KEY_ID: '' AWS_SECRET_ACCESS_KEY: '' AWS_REGION: '' SOURCE_DIR: 'dist' DEST_DIR: '' DISTRIBUTION_ID: ''
优化方案及修改后的配置
核心优化点
- 升级Node.js至LTS版本(v20),利用新版本的性能提升
- 替换Yarn为pnpm,提升依赖安装速度
- 启用依赖缓存,避免重复下载依赖
- 拆分install与build步骤,便于单独调试和缓存
修改后的Action配置
name: Bundle distributable on: push: jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Expose GitHub slug/short variables uses: rlespinasse/github-slug-action@v3.x - uses: actions/checkout@v4 # 升级到最新版checkout动作 # 设置Node.js环境(v20 LTS) - name: Set up Node.js uses: actions/setup-node@v4 with: node-version: '20' cache: 'pnpm' # 自动配置pnpm缓存 # 安装pnpm包管理器 - name: Install pnpm run: corepack enable pnpm # 安装依赖(使用pnpm) - name: Install dependencies run: NODE_ENV=production pnpm install --production --frozen-lockfile # 执行SWC Spack编译 - name: Build distributable run: pnpm build - name: Upload files to AWS S3 and CloudFront and invalidate CF cache if: env.GITHUB_REF_SLUG == 'main' uses: kefranabg/s3-sync-action@02d8eaf34efb8bd76abe2e5614ba3d8028de219e with: args: --acl public-read --follow-symlinks --delete env: AWS_S3_BUCKET: '' AWS_ACCESS_KEY_ID: '' AWS_SECRET_ACCESS_KEY: '' AWS_REGION: '' SOURCE_DIR: 'dist' DEST_DIR: '' DISTRIBUTION_ID: ''
可选进阶优化
如果想尝试更快的构建速度,可以替换为Bun:
# 替换Node.js设置和依赖安装步骤 - name: Set up Bun uses: oven-sh/setup-bun@v1 with: bun-version: latest - name: Install dependencies run: NODE_ENV=production bun install --production --frozen-lockfile - name: Build distributable run: bun run build
内容的提问来源于stack exchange,提问作者Vadorequest
相关产品推荐
相关产品推荐

