在 Azure Pipelines 中缓存 node_modules 比安装它们花费的时间更长 我的管道 YML 文件:

问题描述

我正在运行一个自托管代理 (Windows Server),并且我正在尝试通过缓存我的 node_modules 来减少我的管道构建时间。但是,恢复 node_modules 缓存所需的时间与从头安装软件包一样长。此外,查看日志给我的印象是它正在外部下载/上传缓存,而不是将缓存保留在 VM 上。如果这是真的,那么我对 node_modules 的缓存将导致每次构建时传输约 1GB 的数据。

我做错了什么?

我的目标是在我的自托管代理上的构建之间简单地维护/保留 node_modules,原因如下:

  1. 防止每次都安装 node_modules
  2. computational caching 目的保留 node_modules/.cache 文件

enter image description here

我的管道 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"

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...