我正在开发 GitHub Actions 工作流程,该工作流程使用 package.json 中定义的 npm 脚本执行测试。但是,我注意到在这种情况下传递时,某些选项似乎会被忽略。例如,我需要将 --test-shard=${{ matrix.shard }}/6 传递给我的脚本,但由于 npm 命令结构需要在测试文件的路径之前指定选项,因此它最终会被忽略。
$ node -h
Usage: node [options] script.js [arguments]
这是我的 npm 脚本的示例:
"scripts": {
"test-hello": "node --test 'test/**/*.test.js'"
}
当我在 github 操作工作流程中像这样运行它时:
- run: xvfb-run npm run test-hello -- --test-shard=${{ matrix.shard }}/4
参数 --test-shard=${{ matrix.shard }}/4 添加在文件路径后面并被忽略。
如何设置工作流程以正确接受这些选项,以及如何确保它们在执行过程中按预期进行处理?任何有关在 GitHub Actions 中管理命令行参数的见解或最佳实践将不胜感激!
在package.json中:
"scripts": {
"test-hello": "node --test $TEST_SHARD 'test/**/*.test.js'"
}
在 GitHub 操作中:
run: |
export TEST_SHARD="--test-shard=${{ matrix.shard }}/4"
xvfb-run npm run test-hello
此设置之所以有效,是因为: 环境变量 TEST_SHARD 在调用 npm 之前在 shell 中设置。 当 npm 运行脚本时,它会生成一个新的 shell 进程来执行命令。 这个新 shell 继承了父 shell 的环境变量,包括 TEST_SHARD。