gitlab-ci

In Gitlab-CI, how to install packages and keep it to be used in another step like artifacts do?


I'm building a Gitlab-CI flow to deploy a project, bur i need run some NPM scripts in tests and code quality check. There is a way to install the NodeJS in some step and in another step use it? The point is, i wouldn't like to install the same package again and again for all steps using it.

I have tried something like this, but without success:

stages:
- "Install"
- "Code Quality"

"NPM":
  stage: "Install"
  before_script:
    - curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
    - echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" > /etc/apt/sources.list.d/nodesource.list
    - apt update -y
    - apt install -y nodejs
    - npm install -g npm
    - npm install -g pnpm
    - npm install -g bun
    - npm --version
  script:
    - npm install
    - npm run prod
  artifacts:
    expire_in: 1 days
    paths:
      - node_modules/
      - public/

"ESLint":
  stage: "Code Quality"
  dependencies:
    - "NPM"
  before_script:
    - npm --version
  script:
    - npm run eslint

The ESLint step return the following:

/usr/bin/bash: line 159: npm: command not found


Solution

  • The solution was use an image with a Node installation.

    image: sample/image/with/node/installation
    
    stages:
    - "Install"
    - "Code Quality"
    
    "NPM":
      stage: "Install"
      before_script:
        - npm --version
      script:
        - npm install
        - npm run prod
      artifacts:
        expire_in: 1 days
        paths:
          - node_modules/
          - public/
    
    "ESLint":
      stage: "Code Quality"
      dependencies:
        - "NPM"
      before_script:
        - npm --version
      script:
        - npm run eslint