Basic GitLab CI configuration for Node.js Project


GitLab CI is one of the good CI tool for individual developer becasue it is possible to use for free.
In this article, I would like to show basic GitLab CI configuration for basic Node.js project.

😸 .gitlab-ci.yml

Basic configuration

.gitlab-ci.yml which is GitLab CI configuration is as follows:

cache:
paths:
- node_modules/

stages:
- test
- build

unit_test:
image: node
stage: test
variables:
NODE_ENV: test
before_script:
# Install yarn
- yarn
script:
- yarn test
- yarn run prettier --list-different

build:
image: docker:latest
stage: build
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay
CI_REGISTRY: registry.gitlab.com
CI_REGISTRY_IMAGE: registry.gitlab.com/PROJECT_NAME
script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
- docker build --pull -t "${CI_REGISTRY_IMAGE}:latest" .
- docker push "${CI_REGISTRY_IMAGE}:latest"
only:
- master

PostgreSQL

If you want to use PostgreSQL for CI, please add:

unit_test:
+ services:
+ - postgres:9.6
variables:
ENVIRONMENT: test
+ POSTGRES_HOST: postgres
+ POSTGRES_DB: test
+ POSTGRES_USER: test
+ POSTGRES_PASSWORD: passw@rd
before_script:
+ # Install PG client & import schema info
+ - apt-get update && apt-get install -y postgresql-client libpq-dev
+ - PGPASSWORD=$POSTGRES_PASSWORD psql -U $POSTGRES_USER -h $POSTGRES_HOST -d $POSTGRES_DB -f db/schema.sql

Puppeteer

If you want to use puppeteer, please add:

unit_test:
before_script:
+ # Libraries for Puppeteer https://github.com/Googlechrome/puppeteer/issues/290
+ - apt-get update
+ - apt-get install -yyq ca-certificates
+ - apt-get install -yyq libappindicator1 libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6
+ - apt-get install -yyq gconf-service lsb-release wget xdg-utils
+ - apt-get install -yyq fonts-liberation

🖥 Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!