首页 > 解决方案 > Azure Pipelines 缓存任务和 ng 无法识别

问题描述

我正在尝试缓存我的 node_modules 以提高构建性能。

这是yaml:

pool:
  vmImage: 'windows-latest'

variables:
  ngWorkingDir: 'src\XXX\client'
  npm_config_cache: $(Pipeline.Workspace)/.npm
  CacheRestored: False

steps:
- task: Cache@2
  inputs:
    key: 'npm | "$(Agent.OS)" | $(ngWorkingDir)/package-lock.json'
    restoreKeys: |
       npm | "$(Agent.OS)"
    path: '$(npm_config_cache)'     
    cacheHitVar: CacheRestored
  displayName: Cache npm

- task: Npm@1
  displayName: 'install node modules'
  inputs:
    workingDir: '$(ngWorkingDir)'
    verbose: false
  condition: eq(variables['CacheRestored'], False)

- task: Npm@1
  displayName: ng lint
  inputs:
    command: 'custom'
    workingDir: '$(ngWorkingDir)'
    verbose: false
    customCommand: 'run lint'

这是输出: 在此处输入图像描述

我无法让 ng lint 命令正常工作。作为第一步,我尝试全局安装 angular cli。但这也无济于事。

标签: npmcachingazure-devopsazure-pipelinesnode-modules

解决方案


$(Pipeline.Workspace)/.npm没有你的 npm 模块。这是 npm 缓存,但您的模块仍然安装在您的项目附近。

请检查这个

# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- master

strategy:
  matrix:
    linux 16 Uncached:    
      VM_IMAGE: ubuntu-16.04
      ENABLE_CACHE: 0
    linux 18 Uncached:
      VM_IMAGE: ubuntu-18.04
      ENABLE_CACHE: 0
    windows 2017 Uncached:
      VM_IMAGE: vs2017-win2016
      ENABLE_CACHE: 0
    linux 16:    
      VM_IMAGE: ubuntu-16.04
    linux 18:
      VM_IMAGE: ubuntu-18.04
    windows 2017:
      VM_IMAGE: vs2017-win2016
pool:
  vmImage: $(VM_IMAGE)

variables:
  # ignore this
  skipComponentGovernanceDetection: true
  
  # override where npm will use the cache per https://www.npmjs.com/package/config-cache
  npm_config_cache: $(Pipeline.Workspace)/.npm

  # the node_modules (and maybe the npm cache) cannot be reused across versions
  # This is probably not strictly needed because we are using the NodeTool installer to lock on a specific version.
  node_version_file: $(Pipeline.Workspace)/node.version.txt

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '10.x'
  displayName: 'Install Node.js'

# only look for the exact match for node_modules
- script: node --version > $(node_version_file)
- task: Cache@2
  inputs:
    key: '"node_modules $(VM_IMAGE)" | $(node_version_file) | node.js-with-npm/package.json | node.js-with-npm/package-lock.json'
    path: 'node.js-with-npm/node_modules'
    cacheHitVar: NODE_MODULES_RESTORED
  displayName: 'Cache node_modules'
  condition: ne(variables.ENABLE_CACHE, '0')
  
# if we didn't get an match for node_modules, see if we have a recent copy of the .npm cache folder
- task: Cache@2
  inputs:
    key: '".npm $(VM_IMAGE)" | $(node_version_file) | node.js-with-npm/package.json | node.js-with-npm/package-lock.json'
    restoreKeys: |
      ".npm $(VM_IMAGE)" | $(node_version_file) | node.js-with-npm/package.json
      ".npm $(VM_IMAGE)" | $(node_version_file)
    path: $(npm_config_cache)
  displayName: 'Cache .npm'
  condition: and(ne(variables.NODE_MODULES_RESTORED, 'true'), ne(variables.ENABLE_CACHE, '0'))

# at this point either:
# 1. we have an exact node_modules folder and NODE_MODULES_RESTORED=='true'
# or
# 2. we have no node_modules folder and NODE_MODULES_RESTORED!='true'
- script: |
    cd node.js-with-npm
    npm ci
  displayName: 'npm ci'
  condition: ne(variables.NODE_MODULES_RESTORED, 'true')

它实际上做了两种缓存:

  • npm 模块
  • npm 缓存

而且由于您仅恢复 npm 缓存,因此您没有在主机代理上找到 ng 安装(因为我假设您在本地安装)。


推荐阅读