在 Azure 管道中缓存 NPM 依赖项以获取内置的 Windows 最新映像

Kes*_*ham 5 npm azure-devops azure-pipelines cypress npm-cache

我正在尝试在管道中缓存 npm 依赖项,下面是 yaml 代码

jobs:
- job: Cypress_e2e_tests
  pool:
    vmImage: 'windows-latest'
  variables:
     npm_config_cache: C:\Users\VssAdministrator\AppData\Local\npm-cache  


  steps:
    - task: NodeTool@0
      inputs:
        versionSpec: '10.x'
    - task: CacheBeta@1
      inputs:
        key: npm | $(Agent.OS) | package-lock.json
        path: $(npm_config_cache)
        restoreKeys: npm | $(Agent.OS) | package-lock.json
      displayName: Cache NPM packages  

- task: CacheBeta@1
  inputs:
    key: 'cypress | $(Agent.OS) | package-lock.json'
    path: 'C:\Users\VssAdministrator\AppData\Local\Cypress'
    restoreKeys: 'cypress | $(Agent.OS) | package-lock.json'
  displayName: Cache cypress binary

- script: npm cache verify
  displayName: 'NPM verify'

- script: npm ci
  displayName: 'Install NPM dependencies'

- script: npm run cy:verify
  displayName: 'Cypress verify'

- script: |
    npx cypress run --browser chrome
  displayName: 'Run Cypress tests' 
  workingDirectory: $(System.DefaultWorkingDirectory)


- task: PublishPipelineArtifact@0
  displayName: 'Publish Screenshots (Cypress)'
  condition: failed()
  inputs:
      artifactName: 'screenshots'
      targetPath: '$(Build.SourcesDirectory)/cypress/screenshots'

- task: PublishPipelineArtifact@0
  displayName: 'Publish Videos (Cypress)'
  condition: failed()
  inputs:
      artifactName: 'videos'
      targetPath: '$(Build.SourcesDirectory)/cypress/videos'


- task: PublishTestResults@2
  inputs:
    testResultsFormat: 'JUnit'
    testResultsFiles: '*.xml'
    failTaskOnFailedTests: true
    testRunTitle: 'Cypress Test Results'
    publishRunAttachments: true
  condition: succeededOrFailed()  
Run Code Online (Sandbox Code Playgroud)

但在第二次运行中,我收到以下信息,并且缓存似乎未按预期工作,

信息,存在缓存未命中。

Mer*_*SFT 2

信息,存在缓存未命中。

按照设计,缓存完成的同时,在第一次构建后,也会生成该缓存对应的Key(指纹) 。Cache

该密钥是基于文件内容的唯一标识:将由文件路径/文件模式标识的文件内容进行散列,然后产生相应的密钥。

由文件路径或文件模式标识的任何文件的内容都经过哈希处理以生成动态缓存密钥。当您的项目具有唯一标识正在缓存的内容的文件时,这非常有用。

因此,根据消息There is a cache miss,第二个构建不应使用第一个构建生成的指纹。


如果这两个构建都是基于同一个分支构建的,则唯一可能的情况是,文件模式标识的文件发生了一些更改。

例如,如果这个识别的文件是package-lock.json,并且我在第一次构建完成后对其进行了一些修改。

此时,YAML管道会自动触发(第二次构建) -> 重新缓存 -> 为其重新生成一个新的key:因为文件内容更新,文件的hash重新计算了。

通常,在这种情况下,您可能会发现缓存的指纹不相同。

因此,由于第二次构建时的指纹不相同,因此消息There is a cache miss是预期的操作。这是设计使然,因为我们用它hash来表示缓存的内容,它对于内容来说是唯一的,并且不受任何东西的影响。

您可以加入讨论的第 2 期:#1、#2