How to use GitLab CI for Node.js apps

Using GitLab CI for Node.js applications automates testing, building, and deployment with integrated CI/CD pipelines. As the creator of CoreUI with over 12 years of Node.js experience since 2014, I’ve configured GitLab CI for numerous backend services. GitLab CI provides YAML-based pipeline configuration with stages, jobs, and deployment environments built into GitLab. This approach streamlines development workflow with automated quality checks and deployments.

Use .gitlab-ci.yml to configure automated Node.js testing, building, and deployment pipelines in GitLab.

Basic Node.js pipeline:

# .gitlab-ci.yml
image: node:20

stages:
  - test
  - build
  - deploy

cache:
  paths:
    - node_modules/

before_script:
  - npm ci

lint:
  stage: test
  script:
    - npm run lint

test:
  stage: test
  script:
    - npm test
  coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
  artifacts:
    reports:
      junit: junit.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura-coverage.xml

build:
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 week
  only:
    - main
    - develop

deploy_staging:
  stage: deploy
  script:
    - npm run deploy:staging
  environment:
    name: staging
    url: https://staging-api.example.com
  only:
    - develop

deploy_production:
  stage: deploy
  script:
    - npm run deploy:prod
  environment:
    name: production
    url: https://api.example.com
  when: manual
  only:
    - main

Docker build and deploy:

# .gitlab-ci.yml
stages:
  - test
  - build
  - deploy

test:
  image: node:20
  stage: test
  script:
    - npm ci
    - npm test

docker_build:
  stage: build
  image: docker:latest
  services:
    - docker:dind
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
  only:
    - main

deploy_kubernetes:
  stage: deploy
  image: bitnami/kubectl:latest
  script:
    - kubectl set image deployment/nodejs-app nodejs-app=$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
  only:
    - main

Multi-environment:

# .gitlab-ci.yml
.deploy_template: &deploy
  image: node:20
  before_script:
    - npm ci

deploy:dev:
  <<: *deploy
  stage: deploy
  script:
    - npm run deploy:dev
  environment:
    name: development
  variables:
    API_URL: $DEV_API_URL
  only:
    - develop

deploy:prod:
  <<: *deploy
  stage: deploy
  script:
    - npm run deploy:prod
  environment:
    name: production
  variables:
    API_URL: $PROD_API_URL
  when: manual
  only:
    - main

Best Practice Note

Use cache for node_modules to speed up pipeline runs. Store secrets in GitLab CI/CD variables. Use manual deployment to production with when: manual. Artifacts persist build output between stages. Environment URLs provide quick access to deployments. This is how we configure GitLab CI for CoreUI Node.js backends—automated testing, Docker builds, and controlled deployments with GitLab’s integrated CI/CD platform.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author