Making long build times short by using cache?

First look to the differences between cache and artifacts:
https://docs.gitlab.com/ee/ci/caching/#cache-vs-artifacts

Furthermore, when using caches you can use the key (to create a unique cache) per commit, branch what ever you want.

So sharing the cache along the jobs within the same branch you could use:

cache:
   key: ${CI_COMMIT_REF_SLUG}
   paths:
       - .....

Sometimes you want to execute some command(s) before you want to start the jobs. Like in my case I don’t have a C++ project, but NodeJS. In my case this would be logical:

before_script:
  - npm install

Here you have an example I use for my Gitlab CI NodeJS project:

cache:
  key: ${CI_BUILD_REF_NAME}
  paths:
    - node_modules/
    
before_script:
    - npm install
    - node ./node_modules/protractor/bin/webdriver-manager update

build:
  tags:
    - build
  stage: build
  script:
    - npm run build

test:unittests:
  tags:
    - test
  stage: test
  script:
    - npm run citest

test:intergration:
  tags:
    - test
  stage: test
  script:
    - npm run e2e

test:lint:
  tags:
    - test
  stage: test
  script:
    - npm run lint

More info…