Gitlab持续集成npm后台进程
Gitlab Continuous Integration npm Background Process
我有一个 gitlab ci 设置,我想在其中启动一个本地 npm 服务器以在后台进行测试。我的 .gitlab-ci.yml
是这样的:
stages:
- setup
- build
- test
cache:
paths:
- venv/
- node_modules/
setup_nvm:
stage: setup
script:
- "echo installing npm and phantomJS via nvm"
- "git clone https://github.com/creationix/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`"
- ". ~/.nvm/nvm.sh"
- "nvm install 5.0"
- "nvm use 5.0"
- "npm install"
- "nohup npm run dev &" # HERE I TRY TO RUN THE SERVER IN THE BACKGROUND
setup_python:
stage: setup
script:
- "echo installing python dependencies in virtual environment"
- "[ ! -d venv ] && virtualenv -p python3 venv"
- "source venv/bin/activate"
- "pip3 install -r requirements.txt"
build_multilang:
stage: build
script:
- "[ ! -d tu9onlinekurstest ] && make -f tools/makefiles/multilang"
do_tests:
stage: test
script:
- "cd src/test"
- "python -m unittest"
但是作业停止并且 setup_python
永远不会启动并且永远处于 pending
状态。我认为作业会并行执行(根据 gitlab runner docs)。您有使用 gitlab runner 处理 运行 后台任务的经验吗?
根据Tomasz Maczukin on a related GitLab issue:
I think the best solution would be to use some service manager
(systemd, runit, upstart, sysv - whatever is present on this system).
On the server you should prepare configuration file to start the
service. Then in CI job you would do e.g. systemctl start tomcat
. This
command is expected to exit just after calling and it's service
manager (outside of Runner's scope) who starts the process.
Process started with Runner, even if you add nohup
and &
at the end, is marked with process group ID. When job is finished Runner is
sending kill signal to whole process group. So any process started
directly from CI job will be terminated at job end. Using service
manager you're not starting the process in context of Runner's job.
Your only notifying a manager to start a process using prepared
configuration :)
这里是 运行 使用 systemd 服务管理器在后台运行进程的示例。
在此示例中,Gitlab CI/CD 将在 Ubuntu 上的 HTTP 服务器 运行 中部署一个 React 网络应用程序。
第 1 步:在运行器上,创建一个服务单元文件
vi /etc/systemd/system/hello-react.service
[Unit]
Description=Hello React service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=gitlab-runner
ExecStart=npx http-server -p 3000 /home/gitlab-runner/hello-react/
[Install]
WantedBy=multi-user.target
第 2 步:在运行器上,将 sudo
权限授予用户 gitlab-runner
,没有密码限制。
$ sudo usermod -a -G sudo gitlab-runner
$ sudo visudo
现在将以下行添加到文件底部
gitlab-runner ALL=(ALL) NOPASSWD: ALL
第 3 步:在您的代码存储库中,创建 deploy.sh
#!/bin/bash
echo "Build the app for production"
npm run build
echo "Copy over the build"
rm -fr /home/gitlab-runner/hello-react/*
cp -r build/* /home/gitlab-runner/hello-react/
echo "Running server in the background"
sudo systemctl restart hello-react
echo "HTTP server started."
第 4 步:在您的代码库中,更新 .gitlab-ci.yml
image: node:latest
stages:
- build
- test
- deploy
cache:
paths:
- node_modules/
install_dependecies:
stage: build
script:
- npm install
artifacts:
paths:
- node_modules/
run_unit_tests:
stage: test
script:
- npm test
deploy_app:
stage: deploy
script:
- bash -c './deploy.sh'
就是这样。 CI/CD 作业将在不停止的情况下完成。该应用程序将被部署,您可以在http://your-server-ip:3000
访问它
我有一个 gitlab ci 设置,我想在其中启动一个本地 npm 服务器以在后台进行测试。我的 .gitlab-ci.yml
是这样的:
stages:
- setup
- build
- test
cache:
paths:
- venv/
- node_modules/
setup_nvm:
stage: setup
script:
- "echo installing npm and phantomJS via nvm"
- "git clone https://github.com/creationix/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`"
- ". ~/.nvm/nvm.sh"
- "nvm install 5.0"
- "nvm use 5.0"
- "npm install"
- "nohup npm run dev &" # HERE I TRY TO RUN THE SERVER IN THE BACKGROUND
setup_python:
stage: setup
script:
- "echo installing python dependencies in virtual environment"
- "[ ! -d venv ] && virtualenv -p python3 venv"
- "source venv/bin/activate"
- "pip3 install -r requirements.txt"
build_multilang:
stage: build
script:
- "[ ! -d tu9onlinekurstest ] && make -f tools/makefiles/multilang"
do_tests:
stage: test
script:
- "cd src/test"
- "python -m unittest"
但是作业停止并且 setup_python
永远不会启动并且永远处于 pending
状态。我认为作业会并行执行(根据 gitlab runner docs)。您有使用 gitlab runner 处理 运行 后台任务的经验吗?
根据Tomasz Maczukin on a related GitLab issue:
I think the best solution would be to use some service manager (systemd, runit, upstart, sysv - whatever is present on this system).
On the server you should prepare configuration file to start the service. Then in CI job you would do e.g.
systemctl start tomcat
. This command is expected to exit just after calling and it's service manager (outside of Runner's scope) who starts the process.Process started with Runner, even if you add
nohup
and&
at the end, is marked with process group ID. When job is finished Runner is sending kill signal to whole process group. So any process started directly from CI job will be terminated at job end. Using service manager you're not starting the process in context of Runner's job. Your only notifying a manager to start a process using prepared configuration :)
这里是 运行 使用 systemd 服务管理器在后台运行进程的示例。
在此示例中,Gitlab CI/CD 将在 Ubuntu 上的 HTTP 服务器 运行 中部署一个 React 网络应用程序。
第 1 步:在运行器上,创建一个服务单元文件
vi /etc/systemd/system/hello-react.service
[Unit]
Description=Hello React service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=gitlab-runner
ExecStart=npx http-server -p 3000 /home/gitlab-runner/hello-react/
[Install]
WantedBy=multi-user.target
第 2 步:在运行器上,将 sudo
权限授予用户 gitlab-runner
,没有密码限制。
$ sudo usermod -a -G sudo gitlab-runner
$ sudo visudo
现在将以下行添加到文件底部
gitlab-runner ALL=(ALL) NOPASSWD: ALL
第 3 步:在您的代码存储库中,创建 deploy.sh
#!/bin/bash
echo "Build the app for production"
npm run build
echo "Copy over the build"
rm -fr /home/gitlab-runner/hello-react/*
cp -r build/* /home/gitlab-runner/hello-react/
echo "Running server in the background"
sudo systemctl restart hello-react
echo "HTTP server started."
第 4 步:在您的代码库中,更新 .gitlab-ci.yml
image: node:latest
stages:
- build
- test
- deploy
cache:
paths:
- node_modules/
install_dependecies:
stage: build
script:
- npm install
artifacts:
paths:
- node_modules/
run_unit_tests:
stage: test
script:
- npm test
deploy_app:
stage: deploy
script:
- bash -c './deploy.sh'
就是这样。 CI/CD 作业将在不停止的情况下完成。该应用程序将被部署,您可以在http://your-server-ip:3000
访问它