Azure DevOps 构建管道“npm install”在 node-gyp 上失败

Aar*_* P. 5 yaml node.js npm azure-devops

我们有一个 CD/CI Azure DevOps 构建管道,该管道已开始针对nodejs/node-gyp.

  • error gyp info using node-gyp@3.8.0
  • error gyp info using node@16.13.0 | win32 | x64

我们的构建管道没有任何变化,我们使用的池是:

pool:
  vmImage: 'windows-latest'
Run Code Online (Sandbox Code Playgroud)

仍映射到windows-2019 https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml

当我们在 YAML 文件中执行以下任务时,管道失败:

- task: Npm@1
  displayName: 'npm install'
  inputs:
    command: 'install'
    workingDir: 'FrontEnd'
    verbose: true
Run Code Online (Sandbox Code Playgroud)

日志档案:

error gyp ERR! UNCAUGHT EXCEPTION
error gyp ERR! stack Error: spawn C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\15.0\Bin\MSBuild.exe ENOENT
error gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
error gyp ERR! stack     at onErrorNT (node:internal/child_process:477:16)
error gyp ERR! stack     at processTicksAndRejections (node:internal/process/task_queues:83:21)
error gyp ERR! System Windows_NT 10.0.17763
error gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\a\\1\\s\\Presentation\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
error gyp ERR! cwd D:\a\1\s\FrontEnd\node_modules\node-sass
error gyp ERR! node -v v16.13.0
error gyp ERR! node-gyp -v v3.8.0
error gyp ERR! This is a bug in `node-gyp`.
error gyp ERR! Try to update node-gyp and file an Issue if it does not help:
error gyp ERR!     <https://github.com/nodejs/node-gyp/issues>
error Build failed with error code: 7
verbose exit 1

##[error]Error: Npm failed with return code: 1
Run Code Online (Sandbox Code Playgroud)

Aar*_* P. 6

Azure 自动更新了它正在使用的节点版本,也将所需的 Node-gyp 版本向前推进,这导致我们所有的构建失败。它推送的版本是:

  • npm@8.1.0
  • 节点@v16.13.0

添加task: NodeTool@0将节点版本设置为最后传递的版本,对我们来说是:

  • npm@6.14.15
  • 节点@14.18.1

最终代码如下所示:

- task: NodeTool@0
  inputs:
    versionSpec: '14.x'
    
- task: Npm@1
  displayName: 'npm install'
  inputs:
    command: 'install'
    workingDir: 'FrontEnd'
    verbose: true
Run Code Online (Sandbox Code Playgroud)

重新运行管道后,它会再次工作并产生工件。