GitHub Actions and GitLab CI for the same Node.js project: writing two pipelines side by side

  1. Goal
  2. Preparation
  3. Steps
  4. Step 1: the pipeline on GitHub Actions
  5. Step 2: the pipeline on GitLab CI
  6. Step 3: run it and add conditions
  7. Quick comparison
  8. Verification
  9. Common errors
  10. References

Goal

After this article you can write a complete CI pipeline on both GitHub Actions and GitLab CI for the same Node.js project, and see where the two tools are alike and where they differ.

The concepts of artifact and cache were explained in the first article of this series (about pipeline stages, artifacts and cache).

Preparation

The examples use Node.js 22 and an Ubuntu 24.04 runner. Check action and image version numbers against the official documentation, because they are updated over time.

Steps

Steps

Step 1: the pipeline on GitHub Actions

Create the file .github/workflows/ci.yml:

name: ci

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm run lint
      - run: npm test

  build:
    needs: test
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm run build
      - uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist/
          retention-days: 7

Reading it part by part:

Step 2: the pipeline on GitLab CI

Create the file .gitlab-ci.yml at the root of the repository:

image: node:22

stages:
  - test
  - build

default:
  cache:
    key:
      files:
        - package-lock.json
    paths:
      - .npm/

before_script:
  - npm ci --cache .npm --prefer-offline

test:
  stage: test
  script:
    - npm run lint
    - npm test

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

Reading it part by part:

Step 3: run it and add conditions

Push the files to the repository. GitHub shows the result in the Actions tab, GitLab in the Build, Pipelines section (menu names may vary between versions).

To limit a job to the default branch, GitLab uses rules:

build:
  stage: build
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  script:
    - npm run build

On GitHub, you add if: github.ref == 'refs/heads/main' to the relevant job.

Quick comparison

Item

Configuration file

GitHub Actions

.github/workflows/*.yml (several files)

GitLab CI

.gitlab-ci.yml (one root file)

Item

Main unit

GitHub Actions

job, with dependencies via needs

GitLab CI

stage containing jobs

Item

Runtime environment

GitHub Actions

runs-on plus setup actions

GitLab CI

image per job

Item

Reuse

GitHub Actions

actions from the marketplace

GitLab CI

include, templates

Item

Cache

GitHub Actions

actions/cache or the cache option of a setup action

GitLab CI

cache block with key, paths

Item

Artifact

GitHub Actions

actions/upload-artifact

GitLab CI

artifacts block

Neither tool is absolutely "better": choose based on where you already keep your code.

Verification

Common errors

References

GitHub Actions and GitLab CI for the same Node.js project: writing two pipelines side by side