简体   繁体   English

Dockerizing Gitlab CI 的 Nodejs 依赖项

[英]Dockerizing Nodejs dependencies for Gitlab CI

I'm using Gitlab CI in order to implement CI for my Node.js app.我正在使用 Gitlab CI 来为我的 Node.js 应用程序实现 CI。 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.每次管道启动时,它都会在第一个作业中安装依赖项,我想通过在 Docker 映像中包含所有依赖项并将该映像传递到测试和生产阶段来防止这种情况。 However, I have been unable to do so.但是,我一直无法这样做。 Apparently Gitlab doesn't run the code inside my image's WORKDIR.显然 Gitlab 不会在我的图像的 WORKDIR 中运行代码。

Following is my Dockerfile:以下是我的 Dockerfile:

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

And following is my gitlab-ci.yml:以下是我的 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.查看日志, pwd导致/builds/anderson-martin/lambda-test ,这与定义的 WORKDIR 不同,并且未找到已安装的依赖项。 Do you have any recommendation for me how can I Dockerize my dependencies and speed up the build stage?你对我有什么建议,我怎样才能对我的依赖项进行 Dockerize 并加快构建阶段?

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:解决您的问题的最简单方法可能是将 node_modules 文件夹从您的基本图像符号链接到 gitlab CI 工作区,如下所示:

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 .符号链接的语法是ln -s EXISTING_FILE_OR_DIRECTORY SYMLINK_NAME

Please note that /home/app/ is the workspace which you´re using in your base image.请注意/home/app/是您在基本映像中使用的工作区。

Gitlab also provides other functionality to share dependencies. Gitlab 还提供了其他功能来共享依赖项。 On the one hand you have caching and on the other job artifacts .一方面你有缓存,另一方面你有工作工件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM