Gitlab.com CI runner issues with npm CI when application (cypress) is in subfolder

##Objective:
Configure a build stage properly considering cypress located in a subfolder instead of the root of the project

I’m using the following configuration .gitlab-ci.yml config:

# first, install Cypress, then run all tests (in parallel)
stages:
  - build
  - test

# to cache both npm modules and Cypress binary we use environment variables
# to point at the folders we can list as paths in "cache" job settings
variables:
  npm_config_cache: "$CI_PROJECT_DIR/cypress-cast/.npm"
  CYPRESS_CACHE_FOLDER: "$CI_PROJECT_DIR/cypress-cast/cache/Cypress"

# cache using branch name
# https://gitlab.com/help/ci/caching/index.md
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .npm
    - cache/Cypress
    - node_modules

# this job installs NPM dependencies and Cypress
install:
  image: cypress/base:10
  stage: build

  script:
    - npm ci
    # check Cypress binary path and cached versions
    # useful to make sure we are not carrying around old versions
    - npx cypress cache path
    - npx cypress cache list
    - $(npm bin)/print-env CI
    - npm run cy:verify


# all jobs that actually run tests can use the same definition
.job_template:
  image: cypress/base:10
  stage: test
  script:
    # print CI environment variables for reference
    - $(npm bin)/print-env CI
    # start the server in the background
    - npm run start:ci &
    # run Cypress test in load balancing mode
    - npm run e2e:record -- --key 4efafe61-914a-46bb-8134-3f77c6100719 --parallel --group "electrons on GitLab CI"
  artifacts:
    when: always
    paths:
      - cypress/videos/
      - cypress/screenshots/

# actual job definitions
# all steps are the same, they come from the template above
electrons-1:
  extends: .job_template
electrons-2:
  extends: .job_template
electrons-3:
  extends: .job_template
electrons-4:
  extends: .job_template
electrons-5:
  extends: .job_template

#Gitlab CI Runner Error:

 $ npm ci
 npm ERR! path /builds/Cast-Soft/cypress-ci-test/package.json
 npm ERR! code ENOENT
 npm ERR! errno -2
 npm ERR! syscall open
 npm ERR! enoent ENOENT: no such file or directory, open '/builds/Cast-Soft/cypress-ci-test/package.json'
 npm ERR! enoent This is related to npm not being able to find a file.
 npm ERR! enoent 
 npm ERR! A complete log of this run can be found in:
 npm ERR!     /builds/Cast-Soft/cypress-ci-test/cypress-cast/.npm/_logs/2020-04-08T18_31_07_337Z-debug.log
Running after_script
00:01
Uploading artifacts for failed job
00:02
 ERROR: Job failed: exit code 1

I know this is a problem because cypress is not on the root folder (including the package and cypress config) but inside a folder called cypress-cast, so I added the command

cd $CI_PROJECT_DIR/cypress-cast/

  script:
    - cd $CI_PROJECT_DIR/cypress-cast/
    - npm ci
    # check Cypress binary path and cached versions
    # useful to make sure we are not carrying around old versions
    - npx cypress cache path
    - npx cypress cache list
    - $(npm bin)/print-env CI
    - npm run cy:verify

#New problems

 $ npm run start:ci &
 $ npm run e2e:record -- --key xxxxxxxxxxxxxxxxxxxxx --parallel --group "electrons on GitLab CI"
 npmnpm  ERR! ERR!path /builds/Cast-Soft/cypress-ci-test/package.json
  path /builds/Cast-Soft/cypress-ci-test/package.json
 npm npm ERR! codeERR! code ENOENT
  ENOENT
 npmnpm  ERR!ERR! errno errno -2
  -2
 npm ERR!npm  ERR!syscall open
  syscall open
 npmnpm  ERR!ERR! enoent enoent ENOENT: no such file or directory, open '/builds/Cast-Soft/cypress-ci-test/package.json'
  ENOENT: no such file or directory, open '/builds/Cast-Soft/cypress-ci-test/package.json'
 npmnpm  ERR!ERR!  enoentenoent This is related to npm not being able to find a file.
  This is related to npm not being able to find a file.
 npmnpm  ERR!ERR!  enoentenoent 
  
 npmnpm ERR! ERR! A complete log of this run can be found in:
  A complete log of this run can be found in:
 npm ERR!npm     /builds/Cast-Soft/cypress-ci-test/cypress-cast/.npm/_logs/2020-04-08T21_52_03_800Z-debug.log
  ERR!     /builds/Cast-Soft/cypress-ci-test/cypress-cast/.npm/_logs/2020-04-08T21_52_03_802Z-debug.log
Running after_script
00:01
Uploading artifacts for failed job
00:01
 Uploading artifacts...
 WARNING: cypress/videos/: no matching files        
 WARNING: cypress/screenshots/: no matching files   
 ERROR: No files to upload                          
 ERROR: Job failed: exit code 1

It looks like using CD creates a snowball of new problems, I’ve tried npm ci --path $CI_PROJECT_DIR/cypress-cast/ and some variations but it didn’t work.

#Question:
Where and how should I set in the beginning the default for cypress path, so it works together in multiple stages such as build and test?

2 Likes