gmc
gmc

Reputation: 3990

GitLab runner Angular dependencies

I'd like to configure runner that automaticaly builds and deploys an Angular app. For that, I'd have to install the project dependencies with npm install before building or deploying, because those are not stored in the repo. The thing is that it is a very slow process, so I'd have to wait for like 5 minutes for that process to complete and the app to be deployed.

Is it possible to avoid this somewhat? A possibility in to install all project dependencies globally, but it is far from ideal.

Upvotes: 0

Views: 787

Answers (2)

gmc
gmc

Reputation: 3990

I was facing the same problem, and what I ended up doing is:

  1. Copy node_modules and package.json in an external folder, that is not overwritten in each commit
  2. In my task, the fisrt thing I do is creating a symlink called node_modules in the project root's folder pointing to the external node_modules folder
  3. I check for differences between the project's package.json and the one in the external location. If they are different, I run npm install and update the external node_modules and package.json
  4. Run, build, ... whatever. Don't forget to add the --preserve-symlinks option to your build or serve commands

With this setting I am now having a build time of 47s compared de 3+ min before.

Upvotes: 0

Alfageme
Alfageme

Reputation: 2145

Meet GitLab's CI cache!

You can use it e.g. to share your node_modules folder. Running npm install once the dependencies are there, will just update if needed.

Here you have an example project with Angular of using:

cache:
  paths: 
    - node_modules/

Where you can compare the execution time of the 2 stages (https://gitlab.com/solidgear-projects/GitlabCI/pipelines/11759264):

  • First stage: 8 minutes 25 seconds
  • Second stage: 56 seconds since after "Successfully extracted cache"

Note that cache is only to share stuff between jobs (you mentioned you have one for build and one for deploy).

To share stuff between different builds (pipelines), I'd recommend you to read on https://about.gitlab.com/2017/07/11/dockerizing-review-apps/ which gives some hints on how to use docker images to bundle all your app's dependencies in a base image and reuse it in the future for your builds. e.g.

build_base:
    stage: build
    image: docker
    services:
        - docker:dind
    before_script:
        - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD registry.gitlab.com
        - docker pull registry.gitlab.com/your-name/your-project:base
    script:
        - docker build -t registry.gitlab.com/your-name/your-project .
        - docker push registry.gitlab.com/your-name/your-project:base
    when: manual

Upvotes: 1

Related Questions