I am trying to build a multistep pipeline that avoids running the jobs that are not needed on each and every run, speeding up the regular build in the process.
The build pipeline looks like:
- build a data snapshot
- install node packages
- build code
- run e2e-tests
I’m expecting that the prepare job should only run if the ci-build cache is not populated, and that the npm-install job should only run if the package-lock.json changes or there is no node_modules cache. This would let me reset the node_modules and the generated data snapshot for the next build run by just deleting the runners cache in the project pipelines.
What I’m seeing is that the tasks initial tasks don’t run when the cache is empty, I am able to work around this by manually starting the pipeline after clearing the cache, this works but it is obviously not the best solution.
The node_modules rule check in npm-install
works as expected for code pushes and edits to the package-lock.json file, but it doesn’t rebuild the node modules if you clear the cache and then checkin code. You need to manually start a build pipeline for it to rebuild the node_modules cache.
rules:
- changes:
- package-lock.json
- exists:
- node_modules
when: never
The following rule for the prepare
task always runs.
rules:
- exists:
- ci-build
when: never
- when: always
I am worried that I am incorrectly using the rules in this build file or is there another way to accomplish the same tasks. Any help would be appreciated!
This project is hosted on gitlab.com, it uses a self hosted runner but broke it out into the sample below for testing on the shared runners to ensure that there were no configuration differences causing the issue.
----------------------------------------------------------- .gitlab-ci.yml ---------------------------------------------------------
variables:
global_cache_key: ${CI_COMMIT_REF_SLUG}
default:
image: alpine
after_script: &default_after
- echo [.gitlab-ci.yml] saving root cache to local cache - in default after_script
- ls -lah
before_script: &default_before
- echo [.gitlab-ci.yml] saving local cache firebase to root cache - in default before_script
- export
- ls -lah
stages:
- prepare
- install
- build
- test
prepare:
stage: prepare
cache:
- key: ${global_cache_key}
paths:
- ci-build
- key: ${global_cache_key}-NODE_MODULES
paths:
- node_modules
script: |
#!/bin/bash
echo "-------------------- install-firebase --------------------"
if [ ! -d ci-build/firebase ]; then
echo [.gitlab-ci.yml] Installing Firestore - directory does not exist
mkdir -p ci-build/firebase
echo testing > ci-build/firebase/rules.txt
else
echo ![.gitlab-ci.yml] Using Firestore from cache
fi
echo "-------------------- create-extract --------------------"
if [ ! -d ci-build/firebase/seed-data ]; then
echo [.gitlab-ci.yml] No seed data - creating data extract
cd $CI_PROJECT_DIR/ci-build/firebase
mkdir seed-data
echo testing > seed-data/data-file.txt
ls -lah ./seed-data/
else
echo ![.gitlab-ci.yml] Using seed-data from cache, directory exists
fi
rules:
- exists:
- ci-build
when: never
- when: always
npm-install:
stage: install
cache:
key: ${global_cache_key}-NODE_MODULES
paths:
- node_modules
script:
- mkdir -p node_modules
- echo $(date) >> node_modules/test.txt
rules:
- changes:
- package-lock.json
- exists:
- node_modules
when: never
build-distribution:
stage: build
cache:
- key: ${global_cache_key}
paths:
- ci-build
- key: ${global_cache_key}-NODE_MODULES
paths:
- node_modules
script:
- echo "-------------------- build-ember-distribution --------------------"
- '[ ! -d ci-build/firebase/ ] && echo firebase missing && exit 1'
- '[ ! -d ci-build/firebase/seed-data ] && echo seed-data missing && exit 1'
- '[ ! -d node_modules ] && echo node_modules missing && exit 1'
- mkdir -p ./dist/
- echo "<h1>Home Page</h1>" > ./dist/index.html
artifacts:
expire_in: 1 week
when: always
paths:
- dist
run-e2e-tests:
stage: test
cache:
- key: ${global_cache_key}
paths:
- ci-build
- key: ${global_cache_key}-NODE_MODULES
paths:
- node_modules
script:
- echo "-------------------- run-e2e-tests --------------------"
- '[ ! -d ci-build/firebase/ ] && echo firebase missing && exit 1'
- '[ ! -d ci-build/firebase/seed-data ] && echo seed-data missing && exit 1'
- '[ ! -d node_modules ] && echo node_modules missing && exit 1'
- '[ ! -d dist ] && echo dist missing && exit 1'
- mkdir -p e2e-tests/cypress/screenshots
- echo testing > e2e-tests/cypress/screenshots/results.txt
- mkdir -p e2e-tests/cypress/videos
- echo testing > e2e-tests/cypress/videos/results.txt
- cat node_modules/test.txt
- cat dist/index.html
artifacts:
expire_in: 1 week
when: always
paths:
- e2e-tests/cypress/screenshots
- e2e-tests/cypress/videos