如何在 GitLab CI 中优雅地终止以 'npm start' 开头的监听 npm 应用程序?
How to elegantly terminate listening npm app started with 'npm start' in GitLab CI?
应用程序使用 .gitlab-ci.yml
文件中的 npm start
命令启动。该应用程序的配置方式使其在定义的端口上侦听连接。因此,上面的命令永远不会停止,从而阻止执行 CI 文件中的其他步骤。
我试图用 (timeout 30s npm start; exit 0)
命令停止服务器,但它 return 的代码仍然是 1,并且管道失败:
$ (timeout 30s npm start; exit 0)
> app-srv@1.0.0 start /builds/app/frontend_server
> nodemon server.js
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
listening on *:4000
ERROR: Job failed: exit code 1
有没有办法 return 代码 0 并执行下一步?
这是一种解决方法,但在我的案例中确实有效:
- timeout 20s npm start > npm_start.log || FAILED=true # if it fails, the variable is set up
- grep -e 'listening on \*:4000' npm_start.log # if 'listening...' is in the output,
# it means the server started successfully;
# if not, the pipeline fails at this point
- if [ "$FAILED" == "true" ]; then exit 0; fi # variable gets checked, status 0 is ensured
应用程序使用 .gitlab-ci.yml
文件中的 npm start
命令启动。该应用程序的配置方式使其在定义的端口上侦听连接。因此,上面的命令永远不会停止,从而阻止执行 CI 文件中的其他步骤。
我试图用 (timeout 30s npm start; exit 0)
命令停止服务器,但它 return 的代码仍然是 1,并且管道失败:
$ (timeout 30s npm start; exit 0)
> app-srv@1.0.0 start /builds/app/frontend_server
> nodemon server.js
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
listening on *:4000
ERROR: Job failed: exit code 1
有没有办法 return 代码 0 并执行下一步?
这是一种解决方法,但在我的案例中确实有效:
- timeout 20s npm start > npm_start.log || FAILED=true # if it fails, the variable is set up
- grep -e 'listening on \*:4000' npm_start.log # if 'listening...' is in the output,
# it means the server started successfully;
# if not, the pipeline fails at this point
- if [ "$FAILED" == "true" ]; then exit 0; fi # variable gets checked, status 0 is ensured