3

我正在尝试在管道中缓存 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()  

但是在第二次运行中,我得到了以下信息,似乎缓存没有按预期工作,

信息,存在缓存未命中。

4

1 回答 1

1

信息,存在缓存未命中。

随着设计,缓存完成,同时,它也会在你第一次构建后为这个缓存生成相应的密钥(指纹) 。Cache

key是基于文件内容的唯一标识:对文件路径/文件模式标识的文件内容进行hash ,生成对应的key。

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

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


如果这 2 个构建都是基于同一个分支构建的,那么唯一可能的情况是,文件模式标识的文件有一些更改。

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

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

像往常一样,在这种情况下,您会发现缓存的指纹并不相同。

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

你可以加入这 2 个问题讨论:#1#2

于 2019-12-26T20:48:16.547 回答