首页 > 解决方案 > npm install 使用 node-gyp 从 CLI 工作,从 TeamCity 失败

问题描述

我在 TeamCity 中遇到了不可靠的构建。

  1. npm install为我的项目执行间接用于node-gyp编译解决方案的项目时,至关重要的是,它仅在从 TeamCity 代理执行时才会失败。

  2. [回答后编辑] 执行的上下文来自 Build Step,它执行一个自定义build.ps1的自动化构建过程 - 涵盖 c#、npm 项目、执行单元测试等。也就是说,它不是 TeamCity 中的一堆离散步骤,而是可以在任何环境中执行的单个 PowerShell 命令。

  3. 如果我从同一服务器上的命令行执行 TeamCity 代理运行的同一用户,它工作正常。

这些是来自构建代理的错误...

MSBuild: C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe
Executing Build
configuration: debug
Restoring npm for src\my-corp-app

> scrypt@6.0.3 preinstall D:\IR\my-corp-app\TcBuild\src\my-corp-app\node_modules\scrypt
> node node-scrypt-preinstall.js


> keccak@1.4.0 install D:\IR\my-corp-app\TcBuild\src\my-corp-app\node_modules\keccak
> npm run rebuild || echo "Keccak bindings compilation fail. Pure JS implementation will be used."


> keccak@1.4.0 rebuild D:\IR\my-corp-app\TcBuild\src\my-corp-app\node_modules\keccak
> node-gyp rebuild


D:\IR\my-corp-app\TcBuild\src\my-corp-app\node_modules\keccak>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild ) 
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
D:\IR\my-corp-app\TcBuild\src\my-corp-app\node_modules\keccak\build\keccak.vcxproj(21,3): error MSB4019: The imported project "D:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
gyp ERR! build error 
gyp ERR! stack Error: `msbuild` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:262:23)
gyp ERR! stack     at emitTwo (events.js:126:13)
gyp ERR! stack     at ChildProcess.emit (events.js:214:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
gyp ERR! System Windows_NT 10.0.14393
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd D:\IR\my-corp-app\TcBuild\src\my-corp-app\node_modules\keccak
gyp ERR! node -v v8.12.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! keccak@1.4.0 rebuild: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the keccak@1.4.0 rebuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\teamcity_user\AppData\Roaming\npm-cache\_logs\2019-02-13T11_38_48_872Z-debug.log
"Keccak bindings compilation fail. Pure JS implementation will be used."

解决方法是执行命令行,它成功,然后再次在 TeamCity 中执行,命令行执行的最新工件使它跳过这个 node-gyp 进程并且构建成功。这种解决方法越来越令人厌烦。

我没有成功地遵循各种谷歌搜索的修复"C:\Microsoft.Cpp.Default.props" was not found错误的建议:

在执行构建时还比较了我的 CLI 提示符和 TeamCity Agent 之间的环境变量,但唯一的区别是 TeamCity JRE 和内部版本号的预期添加。

我目前正试图确定是什么魔法使安装从交互式 CLI 而不是作为 Windows 服务运行的 TeamCity 代理工作

上述日志中提到的完整日志具有以下详细信息

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'rebuild' ]
2 info using npm@6.4.1
3 info using node@v8.12.0
4 verbose run-script [ 'prerebuild', 'rebuild', 'postrebuild' ]
5 info lifecycle keccak@1.4.0~prerebuild: keccak@1.4.0
6 info lifecycle keccak@1.4.0~rebuild: keccak@1.4.0
7 verbose lifecycle keccak@1.4.0~rebuild: unsafe-perm in lifecycle true
8 verbose lifecycle keccak@1.4.0~rebuild: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;D:\IR\my-corp-app\TcBuild\src\my-corp-app-node\node_modules\keccak\node_modules\.bin;D:\IR\my-corp-app\TcBuild\src\my-corp-app-node\node_modules\.bin;C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;D:\IR\my-corp-app\TcBuild\src\my-corp-app-node\node_modules\keccak\node_modules\.bin;D:\IR\my-corp-app\TcBuild\src\my-corp-app-node\node_modules\.bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Amazon\cfn-bootstrap\;C:\ProgramData\chocolatey\bin;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\;C:\Program Files\dotnet\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\;D:\apps\jdbc;C:\Program Files\Git\cmd;D:\apps\OpenSSL-Win64\bin;C:\Program Files\nodejs\;C:\Users\teamcity_user\AppData\Local\Microsoft\WindowsApps;C:\Users\teamcity_user\AppData\Roaming\npm;C:\Users\teamcity_user\.dotnet\tools
9 verbose lifecycle keccak@1.4.0~rebuild: CWD: D:\IR\my-corp-app\TcBuild\src\my-corp-app-node\node_modules\keccak
10 silly lifecycle keccak@1.4.0~rebuild: Args: [ '/d /s /c', 'node-gyp rebuild' ]
11 silly lifecycle keccak@1.4.0~rebuild: Returned: code: 1  signal: null
12 info lifecycle keccak@1.4.0~rebuild: Failed to exec rebuild script
13 verbose stack Error: keccak@1.4.0 rebuild: `node-gyp rebuild`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:301:16)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at EventEmitter.emit (events.js:214:7)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at ChildProcess.emit (events.js:214:7)
13 verbose stack     at maybeClose (internal/child_process.js:915:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid keccak@1.4.0
15 verbose cwd D:\IR\my-corp-app\TcBuild\src\my-corp-app-node\node_modules\keccak
16 verbose Windows_NT 10.0.14393
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "rebuild"
18 verbose node v8.12.0
19 verbose npm  v6.4.1
20 error code ELIFECYCLE
21 error errno 1
22 error keccak@1.4.0 rebuild: `node-gyp rebuild`
22 error Exit status 1
23 error Failed at the keccak@1.4.0 rebuild script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

标签: npmteamcitynode-gyp

解决方案


最后通过添加一个额外的(和冗余的)PowerShell构建步骤解决了这个问题:

  • 跑步者类型:PowerShell
  • 脚本:SourceCode
  • 工作目录:<the obvious folder>
  • 脚本来源:npm install

它神奇地起作用了。

现在,我当然已经npm install在我的构建自动化中从深处执行了build.ps1

出于某种原因,上述方法有效,而埋葬的则无效。我无法解释为什么。确认执行文件夹是正确的,并且get-command npm在两个上下文中都使用过,并且两者的结果是相同的:

CommandType     Name     Version    Source
-----------     ----     -------    ------
Application     npm.cmd  0.0.0.0    C:\Program Files\nodejs\npm.cmd 

推荐阅读