Kesiya Abraham
Kesiya Abraham

Reputation: 853

Caching NPM dependencies in Azure pipeline for in-built windows-latest image

I am trying to cache the npm dependencies in pipeline and below is the yaml code

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()  

But in the second run, I am getting the below information and seems like the cache is not working as expected,

Information, There is a cache miss.

Upvotes: 5

Views: 4542

Answers (1)

Mengdi Liang
Mengdi Liang

Reputation: 18978

Information, There is a cache miss.

As the design, the caching finish and meanwhile, it also generate the corresponding Key(fingerprint) for this cache after you build the Cache at first time.

This key is a uniquely identify which based on the file content: Hash the file contents which identified by the file path/file pattern, and then produce the corresponding key.

The contents of any file identified by a file path or file pattern is hashed to produce a dynamic cache key. This is useful when your project has file(s) that uniquely identify what is being cached.

So, according to the message There is a cache miss, the second build should did not using the fingerprint that the first build generated.


If these 2 builds are all built based on one same branch, the only possible of this is, there has some changes on the file that identified by the file pattern.

For example, if this identified file is package-lock.json, and I did some modify onto it after the first build finished.

At this time, the YAML pipeline will be triggerred automatically(second build) -> recache -> re-generated one new key for it: because the hash of the file has recalculated because of file content updated.

As normal, in this scenario, you could find the fingerprint of cache is not the same one.

So, because of the fingerprint is not same at the second build, the message of There is a cache miss is the expected action. This is by design because we use hash to represent the contents of the cache which unique for contents and not be affected by anything.

You could join into this 2 issue talked: #1, #2

Upvotes: 2

Related Questions