Gitlab持续集成npm后台流程

nik*_*las 4 npm gitlab gitlab-ci-runner

我有一个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"
Run Code Online (Sandbox Code Playgroud)

但是,该作业暂停并且setup_python永远不会开始并且pending永远处于状态。我认为作业将并行执行(根据gitlabRunner文档)。您是否有使用gitlabRunner运行后台任务的经验?

Yuc*_*uci 7

这是使用 systemd 服务管理器在后台运行进程的示例。

在此示例中,Gitlab CI/CD 将在运行在 Ubuntu 上的 HTTP 服务器中部署一个 React Web 应用程序。

第一步:在运行器上,创建一个服务单元文件

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
Run Code Online (Sandbox Code Playgroud)

第 2 步:在 runner 上,授予sudo用户权限gitlab-runner,没有密码限制。

$ sudo usermod -a -G sudo gitlab-runner
$ sudo visudo
Run Code Online (Sandbox Code Playgroud)

现在将以下行添加到文件的底部

gitlab-runner ALL=(ALL) NOPASSWD: ALL
Run Code Online (Sandbox Code Playgroud)

第 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."
Run Code Online (Sandbox Code Playgroud)

第 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'
Run Code Online (Sandbox Code Playgroud)

就是这样。CI/CD 作业将完成而不会停止。该应用程序将被部署,您可以访问它http://your-server-ip:3000


fat*_*son 5

根据Tomasz Maczukin关于GitLab相关问题的说法

我认为最好的解决方案是使用一些服务管理器(systemd,runit,upstart,sysv-该系统上存在的任何内容)。

在服务器上,您应该准备配置文件以启动服务。然后在CI工作中,例如systemctl start tomcat。该命令应在调用后立即退出,并且由服务管理器(在Runner的作用域之外)启动该过程。

以Runner开头的进程(即使您添加了nohup&在末尾)也标记有进程组ID。作业完成后,Runner正在向整个进程组发送终止信号。因此,直接从CI作业启动的任何过程都将在作业结束时终止。使用服务管理器,您不会在Runner的工作范围内启动流程。您唯一通知管理员使用准备好的配置开始过程的方法:)