Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab Continuous Integration npm Background Process

I have a setup of gitlab ci where I want to start a local npm server for testing in the background. My .gitlab-ci.yml is like:

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"

However the job halts and setup_python is never started and in status pending forever. I thought that jobs would be executed in parallel (according to gitlab runner docs). Do you have experience in running background tasks with gitlab runner?

like image 360
niklas Avatar asked Jul 28 '16 11:07

niklas


People also ask

Is GitLab continuous integration?

GitLab Continuous Integration and Delivery automates all the steps required to build, test and deploy your code to your production environment. Continuous integration automates the builds, provides feedback via code review, and automates code quality and security tests.

Is GitLab good for CI CD?

GitLab CI/ CD is not only used for building code but to scrutinize or review code as well. It allows improved collaboration with easy merge requests and merge management systems.

How does CI CD work in GitLab?

GitLab CI (Continuous Integration) service is a part of GitLab that build and test the software whenever developer pushes code to application. GitLab CD (Continuous Deployment) is a software service that places the changes of every code in the production which results in every day deployment of production.

Why GitLab is better than Jenkins?

While Jenkins boasts of a large plugin shelf, Gitlab is a comprehensive DevOps tool. While multiple plugins do your job efficiently, integration and management of these plugins might become a challenge when the project scales up. Gitlab offers SLA support.


2 Answers

Here is an example of running a process in the background using the systemd service manager.

In this example, Gitlab CI/CD will deploy a React web app in an HTTP server running on Ubuntu.

Step 1: On the runner, create a service unit file

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

Step 2: On the runner, grant the sudo permission to user gitlab-runner with no password restriction.

$ sudo usermod -a -G sudo gitlab-runner
$ sudo visudo

Now add the following line to the bottom of the file

gitlab-runner ALL=(ALL) NOPASSWD: ALL

Step 3: In your code repo, create 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."

Step 4: In your code repo, update .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'

That's it. The CI/CD job will finish without halting. The app will be deployed, and you can visit it at http://your-server-ip:3000

like image 94
Yuci Avatar answered Sep 18 '22 02:09

Yuci


According to 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 :)

like image 44
fatfatson Avatar answered Sep 22 '22 02:09

fatfatson