通过缓存机制在azure管道中找不到Yarn模块错误

问题描述 投票:0回答:1

提前感谢您帮我查看这个问题。

我正在努力通过提供的这种缓存机制来改善我们在azure devops管道中的CI构建时间。我还使用链接中提到的缓存恢复布尔函数

步骤摘要如下面的屏幕截图所示enter image description here

步骤总结

  1. 检查纱线包的缓存。
  2. 如果缓存命中,则从缓存中检索包
  3. 跳过纱线安装步骤,该步骤从公共源提取包,如果缓存命中,则需要一些时间。
  4. 运行纱线构建。

该问题具体出现在yarn构建步骤中,它抱怨无法按照下面的错误日志找到node_module。

2020-10-12T21:28:00.1761207Z ##[section]Starting: yarn build
2020-10-12T21:28:00.1884736Z ==============================================================================
2020-10-12T21:28:00.1885119Z Task         : PowerShell
2020-10-12T21:28:00.1885475Z Description  : Run a PowerShell script on Linux, macOS, or Windows
2020-10-12T21:28:00.1885806Z Version      : 2.170.1
2020-10-12T21:28:00.1886094Z Author       : Microsoft Corporation
2020-10-12T21:28:00.1886495Z Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
2020-10-12T21:28:00.1886967Z ==============================================================================
2020-10-12T21:28:01.0882035Z Generating script.
2020-10-12T21:28:01.1294597Z ========================== Starting Command Output ===========================
2020-10-12T21:28:01.1574727Z ##[command]"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\24379d8e-967c-4af6-9674-96aa85df30a4.ps1'"
2020-10-12T21:28:06.5140487Z yarn run v1.22.10
2020-10-12T21:28:06.5712118Z $ yarn run clean:builddir
2020-10-12T21:28:07.7032890Z $ npx rimraf ./Scripts/build
2020-10-12T21:28:31.4416409Z npx: installed 12 in 14.71s
2020-10-12T21:28:31.5265827Z $ npx concurrently --names "app,workspaces" "npx cross-env NODE_OPTIONS=--max-old-space-size=8192 npx webpack --mode production" "yarn wsrun -m --parallel build:prod" --verbose
2020-10-12T21:28:43.6196644Z npx: installed 54 in 11.679s
2020-10-12T21:28:44.5023615Z [workspaces] error Command "wsrun" not found.
2020-10-12T21:28:44.5026888Z [workspaces] info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
2020-10-12T21:28:44.5030627Z [workspaces] yarn wsrun -m --parallel build:prod exited with code 1
2020-10-12T21:28:46.4240020Z [app] npx: installed 7 in 1.967s
2020-10-12T21:29:09.1434285Z [app] C:\npm\prefix\node_modules\webpack-cli\bin\cli.js:93
2020-10-12T21:29:09.1435399Z [app]              throw err;
2020-10-12T21:29:09.1435915Z [app]              ^
2020-10-12T21:29:09.1436363Z [app] 
2020-10-12T21:29:09.1436924Z [app] Error: Cannot find module 'webpack-bundle-analyzer'
2020-10-12T21:29:09.1437490Z [app] Require stack:
2020-10-12T21:29:09.1438065Z [app] - D:\a\1\s\Abstracted.Web.App\webpack.config.js
2020-10-12T21:29:09.1438780Z [app] - C:\npm\prefix\node_modules\webpack-cli\bin\utils\convert-argv.js
2020-10-12T21:29:09.1441594Z [app] - C:\npm\prefix\node_modules\webpack-cli\bin\cli.js
2020-10-12T21:29:09.1442320Z [app] - C:\npm\prefix\node_modules\webpack\bin\webpack.js
2020-10-12T21:29:09.1443090Z [app]     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
2020-10-12T21:29:09.1443973Z [app]     at Function.Module._load (internal/modules/cjs/loader.js:841:27)
2020-10-12T21:29:09.1445082Z [app]     at Module.require (internal/modules/cjs/loader.js:1025:19)
2020-10-12T21:29:09.1446059Z [app]     at require (C:\npm\prefix\node_modules\webpack-cli\node_modules\v8-compile-cache\v8-compile-cache.js:161:20)
2020-10-12T21:29:09.1447302Z [app]     at Object.<anonymous> (D:\a\1\s\Abstracted.Web.App\webpack.config.js:3:30)
2020-10-12T21:29:09.1448348Z [app]     at Module._compile (C:\npm\prefix\node_modules\webpack-cli\node_modules\v8-compile-cache\v8-compile-cache.js:194:30)
2020-10-12T21:29:09.1449351Z [app]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
2020-10-12T21:29:09.1450132Z [app]     at Module.load (internal/modules/cjs/loader.js:985:32)
2020-10-12T21:29:09.1453332Z [app]     at Function.Module._load (internal/modules/cjs/loader.js:878:14)
2020-10-12T21:29:09.1454342Z [app]     at Module.require (internal/modules/cjs/loader.js:1025:19)
2020-10-12T21:29:09.1455342Z [app]     at require (C:\npm\prefix\node_modules\webpack-cli\node_modules\v8-compile-cache\v8-compile-cache.js:161:20)
2020-10-12T21:29:09.1456524Z [app]     at WEBPACK_OPTIONS (C:\npm\prefix\node_modules\webpack-cli\bin\utils\convert-argv.js:114:13)
2020-10-12T21:29:09.1457530Z [app]     at requireConfig (C:\npm\prefix\node_modules\webpack-cli\bin\utils\convert-argv.js:116:6)
2020-10-12T21:29:09.1460155Z [app]     at C:\npm\prefix\node_modules\webpack-cli\bin\utils\convert-argv.js:123:17
2020-10-12T21:29:09.1460996Z [app]     at Array.forEach (<anonymous>)
2020-10-12T21:29:09.1461622Z [app]     at module.exports (C:\npm\prefix\node_modules\webpack-cli\bin\utils\convert-argv.js:121:15)
2020-10-12T21:29:09.1462534Z [app]     at C:\npm\prefix\node_modules\webpack-cli\bin\cli.js:71:45
2020-10-12T21:29:09.1463445Z [app]     at Object.parse (C:\npm\prefix\node_modules\webpack-cli\node_modules\yargs\yargs.js:576:18)
2020-10-12T21:29:09.1464195Z [app]     at C:\npm\prefix\node_modules\webpack-cli\bin\cli.js:49:8
2020-10-12T21:29:09.1465077Z [app]     at Object.<anonymous> (C:\npm\prefix\node_modules\webpack-cli\bin\cli.js:366:3)
2020-10-12T21:29:09.1465949Z [app]     at Module._compile (internal/modules/cjs/loader.js:1137:30)
2020-10-12T21:29:09.1467013Z [app]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
2020-10-12T21:29:09.1467607Z [app]     at Module.load (internal/modules/cjs/loader.js:985:32)
2020-10-12T21:29:09.1468654Z [app]     at Function.Module._load (internal/modules/cjs/loader.js:878:14)
2020-10-12T21:29:09.1469217Z [app]     at Module.require (internal/modules/cjs/loader.js:1025:19)
2020-10-12T21:29:09.1469969Z [app]     at require (internal/modules/cjs/helpers.js:72:18)
2020-10-12T21:29:09.1470800Z [app]     at Object.<anonymous> (C:\npm\prefix\node_modules\webpack\bin\webpack.js:156:2)
2020-10-12T21:29:09.1471434Z [app]     at Module._compile (internal/modules/cjs/loader.js:1137:30)
2020-10-12T21:29:09.1472287Z [app]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
2020-10-12T21:29:09.1473085Z [app]     at Module.load (internal/modules/cjs/loader.js:985:32) {
2020-10-12T21:29:09.1473556Z [app]   code: 'MODULE_NOT_FOUND',
2020-10-12T21:29:09.1474272Z [app]   requireStack: [
2020-10-12T21:29:09.1474736Z [app]     'D:\\a\\1\\s\\Abstracted.Web.App\\webpack.config.js',
2020-10-12T21:29:09.1475804Z [app]     'C:\\npm\\prefix\\node_modules\\webpack-cli\\bin\\utils\\convert-argv.js',
2020-10-12T21:29:09.1476626Z [app]     'C:\\npm\\prefix\\node_modules\\webpack-cli\\bin\\cli.js',
2020-10-12T21:29:09.1477161Z [app]     'C:\\npm\\prefix\\node_modules\\webpack\\bin\\webpack.js'
2020-10-12T21:29:09.1477854Z [app]   ]
2020-10-12T21:29:09.1478179Z [app] }
2020-10-12T21:29:09.2871272Z [app] npx cross-env NODE_OPTIONS=--max-old-space-size=8192 npx webpack --mode production exited with code 1
2020-10-12T21:29:11.8915556Z error Command failed with exit code 1.
2020-10-12T21:29:11.8918295Z info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
2020-10-12T21:29:12.1132424Z ##[error]PowerShell exited with code '1'.
2020-10-12T21:29:12.2192481Z ##[section]Finishing: yarn build

我已经按照下面的结果放置了一个调试步骤,可以看到肯定有一个使用提到的名称恢复的模块。

问题 我想知道问题可能出在哪里,因为我不确定为什么纱线构建步骤无法看到缓存模块,即使我为

YARN_CACHE_FOLDER = $(Pipeline.Workspace)\.yarn
设置了一个值并且我可以在调试中看到构建模块。

我尝试过的事情。

  • 如果我不跳过纱线安装步骤,一切都会正常。
  • 已通过输入
    --cache-folder $(YARN_CACHE_FOLDER)
  • 的纱线构建命令
  • 已设置
    npm_config_cache
    变量指向与纱线缓存文件夹相同的位置,因为我可以看到纱线最终是
    yarn --> npm --> npx
  • 已设定
    npm config set cache $(YARN_CACHE_FOLDER) --global

调试缓存文件夹结果

2020-10-12T21:27:57.9564700Z ##[command]"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\41464daa-068a-4419-b4ee-a22669f1d505.ps1'"
2020-10-12T21:27:58.2263973Z Yarn Cache Folder: D:\a\1\.yarn
2020-10-12T21:27:58.2878796Z 
2020-10-12T21:27:58.2879351Z 
2020-10-12T21:27:58.2882145Z     Directory: D:\a\1\.yarn
2020-10-12T21:27:58.2882732Z 
2020-10-12T21:27:58.2883036Z 
2020-10-12T21:27:58.2889439Z Mode                LastWriteTime         Length Name                                                                  
2020-10-12T21:27:58.2892321Z ----                -------------         ------ ----                                                                  
2020-10-12T21:27:58.2897508Z d-----       10/12/2020   9:14 PM                v6                                                                    
2020-10-12T21:27:58.3052399Z 
2020-10-12T21:27:58.3052981Z 
2020-10-12T21:27:58.3053995Z     Directory: D:\a\1\.yarn\v6
2020-10-12T21:27:58.3054249Z 
2020-10-12T21:27:58.3054668Z 
2020-10-12T21:27:58.3059237Z Mode                LastWriteTime         Length Name                                                                  
2020-10-12T21:27:58.3061399Z ----                -------------         ------ ----                                                                  
2020-10-12T21:27:58.3090817Z d-----       10/12/2020   9:13 PM                npm-webpack-bundle-analyzer-3.8.0-ce6b3f908daf069fd1f7266f692cbb3bded9
2020-10-12T21:27:58.3091604Z                                                  ba16-integrity                                                        
2020-10-12T21:28:00.0136005Z 
2020-10-12T21:28:00.0137465Z 
2020-10-12T21:28:00.0139239Z     Directory: 
2020-10-12T21:28:00.0140339Z     D:\a\1\.yarn\v6\npm-webpack-bundle-analyzer-3.8.0-ce6b3f908daf069fd1f7266f692cbb3bded9ba16-integrity\node_modules
2020-10-12T21:28:00.0141165Z 
2020-10-12T21:28:00.0142124Z 
2020-10-12T21:28:00.0145599Z Mode                LastWriteTime         Length Name                                                                  
2020-10-12T21:28:00.0148653Z ----                -------------         ------ ----                                                                  
2020-10-12T21:28:00.0286731Z d-----       10/12/2020   9:13 PM                webpack-bundle-analyzer                                               
2020-10-12T21:28:00.0320519Z 
2020-10-12T21:28:00.0321138Z 
2020-10-12T21:28:00.0365021Z     Directory: D:\a\1\.yarn\v6\npm-webpack-bundle-analyzer-3.8.0-ce6b3f908daf069fd1f7266f692cbb3bded9ba16-integrity\nod
2020-10-12T21:28:00.0365533Z     e_modules\webpack-bundle-analyzer\.bin
2020-10-12T21:28:00.0365731Z 
2020-10-12T21:28:00.0365869Z 
2020-10-12T21:28:00.0370893Z Mode                LastWriteTime         Length Name                                                                  
2020-10-12T21:28:00.0373787Z ----                -------------         ------ ----                                                                  
2020-10-12T21:28:00.0378379Z -a----       10/12/2020   9:13 PM            309 webpack-bundle-analyzer                                               
2020-10-12T21:28:00.0390041Z -a----       10/12/2020   9:13 PM            186 webpack-bundle-analyzer.cmd                                           
2020-10-12T21:28:00.0964047Z 
2020-10-12T21:28:00.0964303Z 
2020-10-12T21:28:00.1723744Z ##[section]Finishing: debug cache folder

管道定义的抽象YAML

variables:
  - name: System.Debug 
    value: false
  - name: YARN_CACHE_FOLDER
    value: $(Pipeline.Workspace)\.yarn
  - name: npm_config_cache 
    value: $(Pipeline.Workspace)\.yarn\v6

  
stages:
  # Build All Apps
- stage: Stage1
  displayName: Build
  condition: true
  jobs:
    
    - job: BuildCI
      condition: succeeded()
      steps:

      - checkout: self
        fetchDepth: 1
      - task: Cache@2
        displayName: Cache Yarn packages
        # condition: false
        inputs:
          key: 'yarn | "$(Agent.OS)" | CompanyName.Web.App\yarn.lock'
          restoreKeys: |
            yarn | "$(Agent.OS)"
          path: $(YARN_CACHE_FOLDER)
          cacheHitVar: CACHE_RESTORED

      - powershell: |
          Write-Host "Yarn Cache Folder: $(YARN_CACHE_FOLDER)"
          Write-Host "Searching Pipeline.Workspace Folder for WebPack"
          gci $(Pipeline.Workspace) -filter "*webpack-bundle-analyzer*" -recurse | Select FullName
        #condition: false
        displayName: 'debug folder'
  
      - task: Yarn@2
        displayName: 'Yarn Install Web.App'
        # condition: succeeded()
        condition: |
          and
          (
            succeeded(),
            ne(variables.CACHE_RESTORED, 'false')
          )
        inputs:
          ProjectDirectory: 'CompanyName.Web.App'
          Arguments: '--network-timeout 100000'
      
      - powershell: |
          Write-Host "Yarn Cache Folder: $(YARN_CACHE_FOLDER)"
          Write-Host "Searching Pipeline.Workspace Folder for WebPack"
          gci $(Pipeline.Workspace) -filter "*webpack-bundle-analyzer*" -recurse | Select FullName
        #condition: false
        displayName: 'debug folder'

      - powershell: |
          yarn --cwd "CompanyName.Web.App" build:prod --verbose
        displayName: 'yarn build'
caching azure-devops azure-pipelines pipeline yarnpkg
1个回答
2
投票

我发现我需要缓存的目录不是全局的yarn模块,而是我项目的

node_modules
。我更新了我的 yml 管道以包含

variables:
  YARN_CACHE_FOLDER: myproject\node_modules
© www.soinside.com 2019 - 2024. All rights reserved.