简体   繁体   English

使Nodejs脚本在gitlab CI中在后台运行

[英]Make Nodejs script run in background in gitlab CI

Our dev project start by command npm run serve Is it possible to run it on background mode? 我们的开发项目通过命令npm run serve启动。是否可以在后台模式下运行它? I tried to use nohup, & in end of string. 我尝试在字符串末尾使用nohup和&。 It works properly in shell, but when it start by CI on Gitlab, pipeline state always is "running" cause npm output permanently shows on screen 它可以在Shell中正常工作,但是当它在Gitlab上由CI启动时,管道状态始终处于“运行”状态,这会导致npm输出永久显示在屏幕上

The clean way would be to run a container whose run command is "npm run serve" 干净的方法是运行一个容器,其运行命令为“ npm run serve”

I'm not certain running a non-blocking command through your pipeline is the right way but you should try using "&" "npm run serve" will run the command in "detached mode. 我不确定通过您的管道运行非阻塞命令是正确的方法,但是您应该尝试使用“&”和“ npm run serve”将在“分离模式”下运行命令。

I've faced the same problem using nohup and & . 使用nohup and &遇到了同样的问题。 It was working well in shell, but not on Gitlab CI, It looks like npm start was not detached. 它在shell上运行良好,但在Gitlab CI上却无法运行,看起来npm start没有分离。

What worked for me is to call npm start inside a bash script and run it on before_script hook. 对我npm start是在bash脚本中调用npm start并在before_script钩子上运行它。

test:
  stage: test
  before_script:
    - ./serverstart.sh
  script:
   - npm test
  after_script:
    - kill -9 $(ps aux | grep '\snode\s' | awk '{print $2}')

on the bash script serverstart.sh 在bash脚本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 my 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

this allowed me to detach npm start and pass to next command while keeping npm start process alive 这使我能够分离npm start并传递到下一个命令,同时保持npm start进程npm start活动状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM