Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerizing Nodejs dependencies for Gitlab CI

I'm using Gitlab CI in order to implement CI for my Node.js app. I'm already using artifacts and sharing the dependencies between jobs, however, I would like to make it faster. Every time a pipeline starts, it installs the dependencies during the first job and I'm thinking to prevent this by having all dependencies in a Docker image and pass that image to test & production stages. However, I have been unable to do so. Apparently Gitlab doesn't run the code inside my image's WORKDIR.

Following is my Dockerfile:

FROM node:6.13-alpine
WORKDIR /home/app
COPY package.json .
RUN npm install
CMD [“sh”]

And following is my gitlab-ci.yml:

test:
  image: azarboon/dependencies-test
  stage: test
  script:
     — pwd
     — npm run test

Looking at logs, pwd results in /builds/anderson-martin/lambda-test, which is different from the defined WORKDIR and also installed dependencies are not found. Do you have any recommendation for me how can I Dockerize my dependencies and speed up the build stage?

like image 496
Mahdi Avatar asked Feb 18 '26 02:02

Mahdi


1 Answers

Probably the easiest way to solve your issue is to symlink the node_modules folder from your base image into the gitlab CI workspace like this:

test:
  image: azarboon/dependencies-test
  stage: test
  script:
     — ln -s /home/app/node_modules ./node_modules
     — npm run test

The syntax for symlinking is ln -s EXISTING_FILE_OR_DIRECTORY SYMLINK_NAME.

Please note that /home/app/ is the workspace which you´re using in your base image.

Gitlab also provides other functionality to share dependencies. On the one hand you have caching and on the other job artifacts.

like image 175
BOSD Avatar answered Feb 20 '26 16:02

BOSD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!