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?
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.
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.
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.
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.
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.
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
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
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."
.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
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With