Deploying different Node.js projects using Gitlab Runner

I’m currently working on a large project that consists of several git repos. One of the repos is a static HTML project, and three of them are Node.js projects. I’m struggling to get a decent setup that allows me to easily deploy my latest builds to my server, without any difficult ways of setting it up or me having to manually clear the builds at a later stage.

Right now, I have all my projects in one repository (which I intend to change), with the following .gitlab-ci.yml:

# Check if the project properly installs and builds all dependencies
build_job:
  stage: build
  script:
    - cd src/server
    - npm install

# Deploy the API and place config file
deploy_api_job:
  stage: deploy
  only:
    - production
  script:
    - cp -r . /var/www/$CI_BUILD_REF
    - ln -s ./$CI_BUILD_REF /var/www/templink
    - mv -Tf /var/www/templink /var/www/production
    - cp /var/www/production.json /var/www/production/src/server/config/production.json

# Install all dependencies and restart the process
install_api_job:
  stage: dependencies
  only:
    - production
  script:
    - cd /var/www/production/src/server
    - npm install --production

# Install email_templates project dependencies
install_email_templates_job:
  stage: dependencies
  only:
    - production
  script:
    - cd /var/www/production/src/email_templates
    - npm install --production

# Reboot the process
reboot_job:
  stage: reboot
  only:
    - production
  script:
    - cd /var/www/production/src/server
    - pm2 stop api
    - pm2 delete api
    - NODE_ENV=production pm2 start --name api index.js

stages:
  - build
  - deploy
  - dependencies
  - reboot

The issues I’m running into are the following:

  1. I end up with a lot of “expired” builds, builds that I no longer use, but remain there anyway
  2. I have to run all the projects as the gitlab-runner user, otherwise I have to do a hell lot of file permission setups

Preferably, I’d have a different setup, where Gitlab Runner just has a folder with all my repos, on a new deployment, just pulls the latest code, leaves the files that shouldn’t be removed (such as config/production.json) where they are, and restarts the process, without creating a new folder for every build, installing all dependencies again for every build, and the list may go on.

Does anyone else here have experience with deploying Node.js apps, and if you do, is Gitlab-CI the right way to go, or should I move to something different such as webhooks?