在 Azure Pipelines 中缓存 node_modules 比安装它们需要更长的时间
Caching node_modules in Azure Pipelines takes longer than installing them
我是 运行 自托管代理(Windows 服务器),我试图通过缓存我的 node_modules 来减少我的管道构建时间。但是,恢复 node_modules 缓存所花的时间与从头开始安装软件包所花的时间一样长。另外,查看日志给我的印象是它是 downloading/uploading 外部缓存,而不是将缓存保留在 VM 上。如果这是真的,那么我对 node_modules 的缓存将导致在每次构建时传输约 1GB 的数据。
我做错了什么?
我的目标是简单地 maintain/keep 在我的自托管代理上构建之间的 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\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"
我是 运行 自托管代理(Windows 服务器),我试图通过缓存我的 node_modules 来减少我的管道构建时间。但是,恢复 node_modules 缓存所花的时间与从头开始安装软件包所花的时间一样长。另外,查看日志给我的印象是它是 downloading/uploading 外部缓存,而不是将缓存保留在 VM 上。如果这是真的,那么我对 node_modules 的缓存将导致在每次构建时传输约 1GB 的数据。
我做错了什么?
我的目标是简单地 maintain/keep 在我的自托管代理上构建之间的 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\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"