I created a docker image which stores an npm project. The npm project has an npm script which runs tests. I use Gitlab for CI/CD where I want to define a job which will pull my image and run the npm script. This is the .gitlab.yml:
stages:
- test
.test-api:
image: $CI_REGISTRY_IMAGE
stage: test
script:
- cd packages/mypackage && npm run test:ci
artifacts:
paths:
- packages/mypackage/test-report.html
expire_in: 1 week
test-api-beta:
extends: .test-api
environment:
name: some-env
variables:
CI_REGISTRY_IMAGE: my_image_name
The gitlab job fails with the error:
> mypackage@1.0.0 test:ci /builds/my-organization/my-project/packages/mypackage
> DEBUG=jest-mongodb:* NODE_ENV=test ts-node --transpile-only --log-error node_modules/.bin/jest --watchAll=false --detectOpenHandles --bail
sh: 1: ts-node: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! mypackage@1.0.0 test:ci: `DEBUG=jest-mongodb:* NODE_ENV=test ts-node --transpile-only --log-error node_modules/.bin/jest --watchAll=false --detectOpenHandles --bail`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the mypackage@1.0.0 test:ci script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-04-28T09_05_39_023Z-debug.log
The main issue is the error npm WARN Local package.json exists, but node_modules missing, did you mean to install?. This means that the gitlab script is executed on the actual git repository of my project instead of being executed on the docker image. Indeed my repository doesn't contain node_modules so the job fails. But why doesn't gitlab execute the script on the actual image?
The docker image has a CMD directive:
CMD ["npm", "run", "start"]
Maybe the CMD somehow interferes with the gitlab script?
P.S. pulling the docker image manually and executing the npm script locally works.
This is my Dockerfile:
FROM node:14.15.1
COPY ./package.json /src/package.json
WORKDIR /src
RUN npm install
COPY ./lerna.json /src/lerna.json
COPY ./packages/mypackage/package.json /src/packages/mypackage/package.json
RUN npm run clean
COPY . /src
EXPOSE 8082
CMD ["npm" , "run", "start"]
EDIT: As per M. Iduoad answer if the script is changed as follows:
.test-api:
image: $CI_REGISTRY_IMAGE
stage: test
script:
- cd /src/packages/mypackage && npm run test:ci
artifacts:
paths:
- packages/mypackage/test-report.html
expire_in: 1 week
the npm script works. We need to cd /src/packages/mypackage because this is the location of script in the Dockerfile.