在 Azure 管道中缓存 NPM 依赖项以获得内置 windows-最新图像
Caching NPM dependencies in Azure pipeline for in-built windows-latest image
我正在尝试在管道中缓存 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()
但是在第二个 运行 中,我得到了以下信息并且缓存似乎没有按预期工作,
Information, There is a cache miss.
Information, There is a cache miss.
作为设计,缓存完成的同时,在你第一次构建Cache
之后,也会为这个缓存生成对应的Key(fingerprint)。
这个key是一个唯一根据文件内容来识别的:Hash由文件 path/file 模式,然后 产生 相应的密钥。
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.
因此,根据消息There is a cache miss
,第二个构建应该不使用第一个构建生成的指纹。
如果这 2 个构建都是基于同一个分支构建的,唯一可能的是,文件模式标识的文件有一些更改。
例如,如果这个标识的文件是package-lock.json
,我在第一次构建完成后对其进行了一些修改。
此时会自动触发YAML pipeline(second build) -> recache -> 为它重新生成一个新的key:因为文件的hash由于文件内容已更新,已重新计算。
正常情况下,在这种情况下,您会发现缓存的指纹不是同一个。
因此,由于第二次构建时的指纹不相同,There is a cache miss
的消息是预期的操作。这是设计使然,因为我们使用 hash
来表示缓存的内容,内容是唯一的,不受任何影响。
我正在尝试在管道中缓存 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()
但是在第二个 运行 中,我得到了以下信息并且缓存似乎没有按预期工作,
Information, There is a cache miss.
Information, There is a cache miss.
作为设计,缓存完成的同时,在你第一次构建Cache
之后,也会为这个缓存生成对应的Key(fingerprint)。
这个key是一个唯一根据文件内容来识别的:Hash由文件 path/file 模式,然后 产生 相应的密钥。
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.
因此,根据消息There is a cache miss
,第二个构建应该不使用第一个构建生成的指纹。
如果这 2 个构建都是基于同一个分支构建的,唯一可能的是,文件模式标识的文件有一些更改。
例如,如果这个标识的文件是package-lock.json
,我在第一次构建完成后对其进行了一些修改。
此时会自动触发YAML pipeline(second build) -> recache -> 为它重新生成一个新的key:因为文件的hash由于文件内容已更新,已重新计算。
正常情况下,在这种情况下,您会发现缓存的指纹不是同一个。
因此,由于第二次构建时的指纹不相同,There is a cache miss
的消息是预期的操作。这是设计使然,因为我们使用 hash
来表示缓存的内容,内容是唯一的,不受任何影响。