0

应用程序通过文件中的npm start命令启动.gitlab-ci.yml。该应用程序的配置方式是侦听已定义端口上的连接。因此,上面的命令永远不会停止,从而阻止执行 CI 文件中的其他步骤。

我试图用(timeout 30s npm start; exit 0)命令停止服务器,但它仍然返回代码 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

有没有办法返回代码 0 并执行下一步?

4

1 回答 1

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
于 2020-10-23T12:34:25.043 回答