You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Pipelines中Bun重复安装问题:如何实现缓存或单次安装?

如何在Azure Pipelines中缓存全局安装的Bun?

你遇到的问题是因为Azure Pipelines的托管代理每次构建都会使用全新的干净环境,全局安装的npm包不会在构建之间保留,所以每次都要重新下载安装Bun。下面提供几种可行的缓存方案:

方案1:缓存全局npm包目录

利用Azure Pipelines的Cache@2任务缓存Bun的全局npm包目录,配合前置检查脚本判断是否需要重新安装:

  1. 检查Bun是否已安装:添加脚本任务判断Bun是否存在,设置变量标记是否需要安装
- script: |
    if ! command -v bun &> /dev/null; then
      echo "##vso[task.setvariable variable=BUN_NEEDS_INSTALL]true"
    fi
  displayName: Check if Bun is installed
  1. 配置缓存任务:缓存Bun的全局npm包目录(Linux环境默认路径为/usr/local/lib/node_modules/bun),用Bun版本+操作系统作为缓存键,确保不同版本/系统的缓存隔离
- task: Cache@2
  inputs:
    key: 'bun | "$(Agent.OS)" | 1.1.17' # 替换为你要使用的Bun版本
    restoreKeys: |
       bun | "$(Agent.OS)"
    path: /usr/local/lib/node_modules/bun
  displayName: Cache Bun global npm package
  1. 仅在需要时安装Bun:通过条件判断,只有当Bun未安装时才执行npm安装命令
- task: Npm@1
  displayName: Setup Bun
  condition: eq(variables.BUN_NEEDS_INSTALL, 'true')
  inputs:
    command: 'custom'
    customCommand: 'install -g bun@1.1.17' # 务必指定版本,避免自动更新触发重新安装
    verbose: true

方案2:缓存Bun二进制文件(更高效)

直接使用Bun官方的安装脚本,缓存二进制文件所在目录,跳过npm的依赖解析步骤:

  1. 缓存Bun二进制目录:根据安装路径配置缓存(默认安装后Bun会在~/.bun/bin或/usr/local/bin)
- task: Cache@2
  inputs:
    key: 'bun-bin | "$(Agent.OS)" | 1.1.17'
    restoreKeys: |
       bun-bin | "$(Agent.OS)"
    path: /home/vsts/.bun # 对应Linux环境的默认安装路径
  displayName: Cache Bun binary
  1. 安装Bun(如果缓存未命中):执行官方安装脚本,并将Bun加入系统PATH
- script: |
    if ! command -v bun &> /dev/null; then
      curl -fsSL https://bun.sh/install | bash
      echo "##vso[task.setvariable variable=PATH]$PATH:/home/vsts/.bun/bin"
    fi
  displayName: Install Bun if not cached

注意事项

  • 指定固定版本:无论哪种方案,都要明确指定Bun的版本号。如果使用latest或不指定版本,每次新版本发布都会触发缓存失效,导致重新安装。
  • 路径适配:不同操作系统的Bun安装路径可能不同,Windows环境需要调整缓存路径(比如C:\Users\VssAdministrator\.bun)。
  • 自托管代理备选:如果你的团队使用自托管代理,可以一次性安装Bun后保留环境,无需每次构建都处理缓存,但这种方案只适用于可控的私有代理环境。

内容的提问来源于stack exchange,提问作者hackerl33t

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 14:56:07