[英]GitLab CI stuck on running NodeJS server
I'm trying to use GitLab CI to build, test and deploy an Express app on a server (the Runner is running with the shell executor). 我正在尝试使用GitLab CI在服务器上构建,测试和部署Express应用程序(运行程序与Shell执行程序一起运行)。 However, the test:async
and deploy_staging
jobs do not terminate. 但是, test:async
和deploy_staging
作业不会终止。 But when checking the terminal inside GitLab, the Express server does indeed start. 但是,当检查GitLab内的终端时,Express服务器确实会启动。 What gives ? 是什么赋予了 ?
stages:
- build
- test
- deploy
### Jobs ###
build:
stage: build
script:
- npm install -q
- npm run build
- knex migrate:latest
- knex seed:run
artifacts:
paths:
- build/
- node_modules/
tags:
- database
- build
test:lint:
stage: test
script:
- npm run lint
tags:
- lint
# Run the Express server
test:async:
stage: test
script:
- npm start &
- curl http://localhost:3000
tags:
- server
deploy_staging:
stage: deploy
script:
- npm start
environment:
name: staging
url: my_url_here
tags:
- deployment
The npm start
is just node build/bundle.js
. npm start
只是node build/bundle.js
。 The build script is using Webpack. 构建脚本正在使用Webpack。
You are starting a background job during your test phase which never terminates - therefore the job runs forever. 您正在测试阶段启动一个永远不会终止的后台作业,因此该作业将永远运行。
The idea of the GitLab CI jobs are shortly-running tasks - like compiling, executing unit tests or gathering information such as code coverage - which are executed in a predefined order. GitLab CI作业的想法是短期运行的任务,例如编译,执行单元测试或收集诸如代码覆盖率之类的信息,这些任务以预定义的顺序执行。 In your case, the order is build -> test -> deploy
; 在您的情况下,顺序为build -> test -> deploy
; since the test
job doesn't finish, deploy
isn't even executed. 由于test
工作尚未完成,因此甚至不会执行deploy
。
Depending on your environment, you will have to create a different job for deploying your node app. 根据您的环境,您将不得不创建其他作业来部署节点应用程序。 For example, you can push the build output to a remote server using a tool like scp
or upload it to AWS; 例如,您可以使用诸如scp
类的工具将构建输出推送到远程服务器,或者将其上传到AWS。 after that, you reference the final URL in the url:
field in your .gitlab-ci.yml
. 之后,您可以在.gitlab-ci.yml
的url:
字段中引用最终URL。
Note: solution works fine when using a gitlab runner with shell executor
注意:将gitlab Runner与shell executor
一起使用时,解决方案可以正常工作
Generally in Gitlab CI we run ordered jobs with specific tasks that should be executed one after the end of the other. 通常,在Gitlab CI中,我们运行带有特定任务的有序作业,这些任务应在另一任务结束后执行。
So for the job build
we have the npm install -q
command that runs and terminates with an exit status (0 exit status if the command was succesful), then runs the next command npm run build
and so on until the job is terminated. 因此,对于作业build
我们有npm install -q
命令,该命令运行并以退出状态终止(如果命令成功,则退出状态为0),然后运行下一个命令npm run build
,依此类推,直到作业终止。
For the test
job we have npm start &
process that keeps running so the job wont be able to terminate. 对于test
作业,我们有npm start &
process保持运行,因此该作业将无法终止。
The problem is that sometimes we need to have some process that need to run in background or having some process that keeps living between tasks. 问题在于,有时我们需要某个需要在后台运行的流程,或者某个需要在任务之间运行的流程。 For example in some kind of test we need to keep the server running, something like that: 例如,在某种测试中,我们需要保持服务器运行,如下所示:
test:
stage: test
script:
- npm start
- npm test
in this case npm test
will never start because npm statrt
keeps running without terminating. 在这种情况下, npm test
将永远不会开始,因为npm statrt
会继续运行而不会终止。
The solution is to use before_script
where we run a shell script that keeps npm start
process running then we call after_script
to kill that npm start
process 解决方案是使用before_script
,在此我们运行使npm start
进程保持运行的shell脚本,然后调用after_script
杀死该npm start
进程。
so on our .gitlab-ci.yml we write 所以在我们的.gitlab-ci.yml中,我们写
test:
stage: test
before_script:
- ./serverstart.sh
script:
- npm test
after_script:
- kill -9 $(ps aux | grep '\snode\s' | awk '{print $2}')
and on the serverstart.sh 并在serverstart.sh上
# !/bin/bash
# start the server and send the console and error logs on nodeserver.log
npm start > nodeserver.log 2>&1 &
# keep waiting until the server is started
# (in this case wait for mongodb://localhost:27017/app-test to be logged)
while ! grep -q "mongodb://localhost:27017/app-test" nodeserver.log
do
sleep .1
done
echo -e "server has started\n"
exit 0
thanks to that serverstart.sh
script is terminated while keeping npm start
process alive and help us by the way move to the job where we have npm test
. 多亏了serverstart.sh
脚本的终止,同时使npm start
进程保持活动状态,并通过转移到我们进行npm test
的工作来帮助我们。
npm test
terminates and pass to after script where we kill all nodejs process. npm test
终止并传递到after脚本,在此我们杀死所有nodejs进程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.