Node Modules Caching issue with Gitlab Ci

Hey,

I’m having an issue that I just can’t seem to figure out:

We haver a single runner installed on Linux using docker. We have a staging and master branch from which we do our deployments.

On staging the build works fine with no issue, however on master we are getting the following error

 $ npm run build
 > ef-storyblok-nextjs@0.0.1 build /builds/education-first/ef-storyblok-nextjs
 > next build
 NODE_ENV:  production
 Storyblok API Endpoint:  https://app.storyblok.com/v1
 phase *** phase-production-build
 Warning: No build cache found. Please configure build caching for faster rebuilds. Read more: https://err.sh/next.js/no-cache
 Creating an optimized production build...
 Attention: Next.js now collects completely anonymous telemetry regarding usage.
 This information is used to shape Next.js' roadmap and prioritize features.
 You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
 https://nextjs.org/telemetry
 Failed to compile.
 ./node_modules/next/dist/client/next.js
 Error: Cannot find module '@babel/core'
  babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.
 > Build error occurred
 Error: > Build failed because of webpack errors
     at build (/builds/education-first/ef-storyblok-nextjs/node_modules/next/dist/build/index.js:10:900)
 npm ERR! code ELIFECYCLE
 npm ERR! errno 1
 npm ERR! ef-storyblok-nextjs@0.0.1 build: `next build`
 npm ERR! Exit status 1
 npm ERR! 
 npm ERR! Failed at the ef-storyblok-nextjs@0.0.1 build script.
 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
 npm ERR! A complete log of this run can be found in:
 npm ERR!     /root/.npm/_logs/2020-01-17T08_18_10_095Z-debug.log
 ERROR: Job failed: exit code 1

I’ve tried from the master branch to run this locally and it seems to work fine. Again the build works fine in staging and when we merge to master and our gitlab ci runs the error keeps happening.

This is our Gitlab CI:

image: node:10.5.0

cache:
  paths:
    - node_modules/
    - out/
    - .next/cache/

stages:
  # - test # this will do unit testing and e2e testing
  - publish
  - deploy

id_staging_deploy_next_server:
  stage: deploy
  environment:
    name: id_staging
    url: https://example.com
  before_script:
    - export COMMIT_TIME=$(git show -s --format=%ct $CI_COMMIT_SHA)
    - export COMMIT_TAG=$(git show -s --format=%H $CI_COMMIT_TAG)
    - apt-get update && apt-get install -y zip
    - npm config set registry https://npm.ef.com/
    - npm config set //npm.ef.com/:_authToken ${EF_NPM_TOKEN}
  script:
    - npm install
    - npm run build
    - zip ./builds/server_build_$COMMIT_TAG.zip -rq * .[^.]* .next/\* -x out/\* -x .git/\*
    - node -e 'require("./devops/deploy").deployServerBuild()'
  only:
    refs:
      - staging
  except:
    - triggers
  tags:
    - e1
    - storyblok

id_production_deploy_next_server:
  stage: deploy
  environment:
    name: id_production
    url: http://example.com
  before_script:
    - export COMMIT_TIME=$(git show -s --format=%ct $CI_COMMIT_SHA)
    - export COMMIT_TAG=$(git show -s --format=%H $CI_COMMIT_TAG)
    - apt-get update && apt-get install -y zip
    - npm config set registry https://npm.ef.com/
    - npm config set //npm.ef.com/:_authToken ${EF_NPM_TOKEN}
  script:
    - npm install
    - npm run build
    - zip ./builds/server_build_$COMMIT_TAG.zip -rq * .[^.]* .next/\* -x out/\* -x .git/\*
    - node -e 'require("./devops/deploy").deployServerBuild()'
  only:
    refs:
      - master
  except:
    - triggers
  tags:
    - e1
    - storyblok

I’m assuming it possibly has something to do with the cache, but I’ve tried to remove the cache we have in the gitlab ci, clear the docker cache and have also upgrade our gitlab-runner from 12.2 to 12.6 and issue still persists. I’ve even registered a new runner and same thing happens on the new one. The code between staging and master is exactly the same.

I’m banging my head on this one as again as the buid works locally, it is not the framework we are using NextJs as it works in the staging gitlab ci build as well. I’m assuming it’s something to do with our runner but can’t figure out what exactly.

Any suggestions would be welcome? Is there a configuration that is causing this? I’m checking on both sides both NextJS community and gitlab CI. Thanks ahead of time for your help and support on this it’s greatly appreciated.

So I was able to sort of resolve this for one of my pipelines using artifacts and passing them between jobs.

But now I’ve gotten a similar issue for another pipeline and artifacts is not working which is making me realize that I’m still not sure of the exact problem.

I have now set my pipelines in two jobs, one is to install my npm packages, and the other is to bundle and deploy. I have one pipeline that is running on master when I do merge requests and another pipeline that is triggered via webhooks. My merge requests works fine but the webhook does not, and I have noticed the following differene:

In my merge requests my npm package is installing and finding all of the necessary packages / files but in my webhook trigger even though it’s using the same commit and branch it doesn’t seem to be installing all of the packages.

Image with all packages

Image where it seems it’s not installing the same number of packages

Is there a reason why this us happening even though they are in the same branch, commit but one is a merge request while the other is a pipeline trigger? Is there something I am missing? Thanks.

I figured it out, the main issue was in certain packages being set in the devDependencies.

  1. Our staging environment was working fine because NODE_ENV is not set to production
  2. Our master environment that was set in 2 jobs was working because we were actually setting the NODE_ENV after the npm install (in the second job). In the other job the NODE_ENV was being set before npm install and when NODE_ENV=production npm install does not install devDependencies
  3. Fix was to add the packages required from devDependencies as dependencies