问题描述
我正在运行一个自托管代理 (Windows Server),并且我正在尝试通过缓存我的 node_modules 来减少我的管道构建时间。但是,恢复 node_modules 缓存所需的时间与从头安装软件包一样长。此外,查看日志给我的印象是它正在外部下载/上传缓存,而不是将缓存保留在 VM 上。如果这是真的,那么我对 node_modules 的缓存将导致每次构建时传输约 1GB 的数据。
我做错了什么?
我的目标是在我的自托管代理上的构建之间简单地维护/保留 node_modules,原因如下:
- 防止每次都安装 node_modules
- 为 computational caching 目的保留 node_modules/.cache 文件夹
我的管道 YML 文件:
trigger:
- develop
- master
variables:
nodeModulesCache: $(System.DefaultWorkingDirectory)/node_modules
stages:
- stage: client_qa
displayName: Client code QA
dependsOn: []
pool:
name: Default
jobs:
- job:
displayName: Lint & test client code
steps:
# Use NodeJS.
- task: UseNode@1
inputs:
version: "12.x"
# Restore cache.
- task: Cache@2
inputs:
key: 'npm | "$(Agent.OS)" | client/package-lock.json'
restoreKeys: |
npm | "$(Agent.OS)"
path: $(nodeModulesCache)
displayName: Cache Node modules
# Install dependencies.
- script: npm install
workingDirectory: client
displayName: "Install packages"
# Lint affected code.
- script: npm run lint:affected:ci
workingDirectory: client
displayName: "Lint affected code"
# Test affected code.
- script: npm run test:affected:ci
workingDirectory: client
displayName: "Run affected unit tests"
解决方法
您将 node_modules
缓存到 $(System.DefaultWorkingDirectory)/node_modules
,路径应为 _\agent_work\1\s\node_modules
。自托管代理将在获取之前运行 execute git clean -ffdx && git reset --hard HEAD
,它每次都会删除文件夹 node_modules
并安装 node_modules
。查看此doc了解更多详情。
我们需要在步骤级别添加代码 - checkout: self clean: false
。
YAML 定义
trigger:
- develop
- master
variables:
nodeModulesCache: $(System.DefaultWorkingDirectory)/node_modules
stages:
- stage: client_qa
displayName: Client code QA
dependsOn: []
pool:
name: Default
jobs:
- job:
displayName: Lint & test client code
steps:
- checkout: self
clean: false
# Use NodeJS.
- task: UseNode@1
inputs:
version: "12.x"
# Restore cache.
- task: Cache@2
inputs:
key: 'npm | "$(Agent.OS)" | client/package-lock.json'
restoreKeys: |
npm | "$(Agent.OS)"
path: $(nodeModulesCache)
displayName: Cache Node modules
# Install dependencies.
- script: npm install
workingDirectory: client
displayName: "Install packages"
# Lint affected code.
- script: npm run lint:affected:ci
workingDirectory: client
displayName: "Lint affected code"
# Test affected code.
- script: npm run test:affected:ci
workingDirectory: client
displayName: "Run affected unit tests"