我正在尝试使用GitHub Actions在默认的Angular项目上设置CI管道。我可以成功地在本地运行单元测试,但遇到了GitHub操作的问题。我需要更改什么才能使它成功运行?
name: Node.js CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
以下是此步骤的日志输出
> ng test
Compiling @angular/core/testing : es2015 as esm2015
Compiling @angular/platform-browser/testing : es2015 as esm2015
Compiling @angular/compiler/testing : es2015 as esm2015
Compiling @angular/platform-browser-dynamic/testing : es2015 as esm2015
Compiling @angular/common/testing : es2015 as esm2015
Compiling @angular/router/testing : es2015 as esm2015
- Generating browser application bundles...
17 12 2020 20:14:02.566:WARN [karma]: No captured browser, open http://localhost:9876/
17 12 2020 20:14:02.708:INFO [karma-server]: Karma v5.1.1 server started at http://localhost:9876/
17 12 2020 20:14:02.709:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
17 12 2020 20:14:02.713:INFO [launcher]: Starting browser Chrome
✔ Browser application bundle generation complete.
- Generating browser application bundles...
✔ Browser application bundle generation complete.
17 12 2020 20:14:07.847:WARN [karma]: No captured browser, open http://localhost:9876/
17 12 2020 20:14:08.707:ERROR [launcher]: Cannot start Chrome
[2646:2646:1217/201408.612463:ERROR:browser_main_loop.cc(1434)] Unable to open X display.
17 12 2020 20:14:08.707:ERROR [launcher]: Chrome stdout:
17 12 2020 20:14:08.707:ERROR [launcher]: Chrome stderr: [2646:2646:1217/201408.612463:ERROR:browser_main_loop.cc(1434)] Unable to open X display.
17 12 2020 20:14:08.712:INFO [launcher]: Trying to start Chrome again (1/2).
17 12 2020 20:14:08.860:ERROR [launcher]: Cannot start Chrome
[2682:2682:1217/201408.854964:ERROR:browser_main_loop.cc(1434)] Unable to open X display.
17 12 2020 20:14:08.861:ERROR [launcher]: Chrome stdout:
17 12 2020 20:14:08.861:ERROR [launcher]: Chrome stderr: [2682:2682:1217/201408.854964:ERROR:browser_main_loop.cc(1434)] Unable to open X display.
17 12 2020 20:14:08.863:INFO [launcher]: Trying to start Chrome again (2/2).
17 12 2020 20:14:09.006:ERROR [launcher]: Cannot start Chrome
[2711:2711:1217/201409.000191:ERROR:browser_main_loop.cc(1434)] Unable to open X display.
17 12 2020 20:14:09.006:ERROR [launcher]: Chrome stdout:
17 12 2020 20:14:09.007:ERROR [launcher]: Chrome stderr: [2711:2711:1217/201409.000191:ERROR:browser_main_loop.cc(1434)] Unable to open X display.
17 12 2020 20:14:09.008:ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up.
如果你不想按照另一个答案中的建议更改你的package.json、karma.conf或安装Puppeter(不管是什么(,你可以修改你的GitHub操作配置,将--watch=false
和--browsers=ChromeHeadless
标志传递给底层脚本。
jobs:
build:
steps:
- run: npm test -- --watch=false --browsers=ChromeHeadless
第一个";浮动";--
很重要。。。它告诉NPM将任何后续参数传递给它正在调用的脚本,而不是试图根据NPM评估它们。因此,如果npm test
调用ng test
,则上述内容实际上将调用ng test --watch=false --browsers=ChromeHeadless
。
CCD_ 7将固定";跑步永远不会结束";问题,这是由ng test
监视文件更改以触发测试重新运行引起的。
--browsers=ChromeHeadless
将修复GitHub Actions无法在没有显示器的系统上运行完整的Chrome浏览器的事实(因此错误输出中出现"无法打开X显示器"。(因此,它将运行一个特殊的无头版本来模拟显示部分,而不是运行完整的Chrome。
您需要安装木偶机,因此需要一些额外的配置
npm install puppeteer --save-dev
在restartOnFileChange: true,
下的karma.conf.js中添加
customLaunchers: {
ChromeHeadlessCustom: {
base: 'ChromeHeadless',
flags: ['--no-sandbox', '--disable-gpu']
}
}
在package.json中,将测试脚本更新为"test": "ng test --watch=false --browsers=ChromeHeadlessCustom",
对我有效的是:
- 我用ng生成了一个karma配置文件generate config karma
- 我将其添加到配置文件中:
module.exports = function(config) {
config.set({
// ...
browsers: ['ChromeHeadlessNoSandbox'],
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
}
// ...
});
};
我的ci作业现在可以在测试中正常运行,如果在测试成功后作业仍然像我的一样卡住,请确保配置Karma以在测试后退出测试服务器,并将singleRun设置为true
module.exports = function(config) {
config.set({
// ...
singleRun: true
// ...
});
};