Run 'npm ci' and 'npm run build' only once

Hi, I’ve split my tests up so they run in different stages, however, using the config below, ‘npm ci’ and ‘npm run build’ are run before each test. Is it possible to run these scripts only once? My aim is to speed up the pipeline by running our unit and integration tests separately, so if one fails, it can be run independently without running the other. Our cypress integration tests are particularly slow, and can take up to 30 minutes. Any suggestions?

language: node_js

node_js:
  - 12

addons:
  apt:
    packages:
      # Ubuntu 16+ does not install this dependency by default, so we need to install it ourselves
      - libgconf-2-4

before_install:
  - npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN

cache:
  directories:
    - ~/.npm
    - ~/.cache
  override:
    - npm ci
    - npm run build

defaults: &defaults
  script:
    - npm run $STAGE_NAME

jobs:
  include:
    - stage: test
      env:
        - STAGE_NAME="test"
      <<: *defaults
    - stage: test
      env:
        - STAGE_NAME="integration"
      <<: *defaults

If you wish to skip creating build artifacts in succeeding stages and rather use ones created by a preceding stage, you can transfer them using workspaces:

Thanks for the advice, much appreciated. I have changed my config file to be:

language: node_js

node_js:
  - 12

addons:
  apt:
    packages:
      # Ubuntu 16+ does not install this dependency by default, so we need to install it ourselves
      - libgconf-2-4

before_install:
  - npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN

cache:
  directories:
    - ~/.npm
    - ~/.cache

defaults: &defaults
  script:
    - npm run $STAGE_NAME

jobs:
  include:
    - stage: build
      script:
        - npm ci
        - npm run build
      workspaces:
        create:
          name: build1
    - stage: test
      workspaces:
        use: build1
      env:
        - STAGE_NAME="test"
      <<: *defaults
    - stage: integration
      workspaces:
        use: build1
      env:
        - STAGE_NAME="integration"
      <<: *defaults

but the stage called “integration” still runs npm run build before it starts running its own script. Could you advise me how I can speed this up at all? Any suggestions would be very welcome! Thanks again.

Could you link to the build? Can’t quite say what’s happening without seeing it.

My best blind guess is the extra command is the default install command for language: node_js. To skip it, specify install: skip in the specific job(s).

Ah, I think I have it, there was a build command in the package.json, so it wasn’t coming from the travis.yml file, apologies for the mixup. Could you recommend anyway of making this faster, do I have the correct files cached etc? Thanks for any advice!

I don’t know much about NPM and you didn’t show me the build for me to see what’s happening.

So all I can say is I see your workspace is currently empty. You probably want to add your build artifacts and/or NPM cache locations to it – depending on what your later stages need to be present.

Imprint