/ CI

Setup GitLab CI for a NodeJS application on Ubuntu

As you may know GitLab, it is online repository with feature that you are able to store private project for free (GitHub private repo has fee). Beside of this, GitLab also support CI (Continuous integration), with this feature your application will be built and deploy since you have a new commit.

So how to setup this CI? I gave it a try and faced a lot of issue. So I thinks I should write some thing about it so you may get some useful things while trying with the GitLab CI. In this post I will introduce you setup it with a NodeJS application on Ubuntu virtual machine using nginx.

Step 1: Add .gitlab-ci.yml to the project

First step you need to add this file to your root repository source. With this file you can define what CI will execute. For instance, CI can execute build, test and deploy.

Assuming that you already have GitLab account and also have a project on it. So go to your project and click on Setup CI.
setup-ci_1
To continue you add content for this file:

before_script:
  - npm install
job_deploy:
  stage: deploy
  script:
    - forever start server.js

I'm going to explain the content above:
before_script: commands will execute before state (build, test, deploy)
job_deploy: script to deploy your application - forever start server.js

So you have just finished your first step.

Step 2: Setup gitlab-runner on Ubuntu

*Note: I use Shell to run script on server. There are some alternative like Docker, SSH and so on.

Install gitlab-runner on server:

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
sudo apt-get install gitlab-ci-multi-runner

Time to register runner:

sudo gitlab-ci-multi-runner register

There will be some question show, you need to answer them:

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci )
https://gitlab.com/ci
Please enter the gitlab-ci token for this runner
xxxxxxxxx
Please enter the gitlab-ci description for this runner
my-runner
INFO[0034] fcf5c619 Registering runner... succeeded
Please enter the executor: shell, docker, docker-ssh, ssh?
shell
Please enter identity_file path:
/home/user/.ssh/id_rsa
INFO[0037] Runner registered successfully. Feel free to start it, but if it's
running already the config should be automatically reloaded!

*url and token you can find on gitlab project website.
Go Settings > Runners
runners_activated
Yeah, everything is ready for your first CI deploy. Try to make a new commit on your repo to see what happens.

Hopefully, you can make it works!

The below is some references may helpful for you:
Gitlab CI Quickstart

Install Gitlab-runner

Gitlab-runner command references