When setting up CI pipelines, caching dependencies can significantly speed up your build times. However, it's important to cache the right directories to ensure efficiency and avoid potential issues.
Node.js Dependencies
For Node.js projects, instead of caching the node_modules directory, it's recommended to cache the npm cache directory. This is because node_modules can be large and may contain platform-specific binaries that could cause issues if reused across different environments.
-
Cache Directory:
~/.npm -
Optional: You might also consider caching
~/.cacheif you use tools that store cache there, like Yarn.
PHP Composer Dependencies
For PHP projects using Composer, the consensus is similar. Instead of caching the vendor directory, which can be large and contain platform-specific binaries, you should cache the Composer cache directory.
-
Cache Directory:
~/.composer/cache
Example CI Configuration
Here's a basic example of how you might configure caching in a CI pipeline for both Node.js and PHP projects:
# Example for a CI configuration file
cache:
paths:
# Cache npm cache directory
- ~/.npm
# Cache Composer cache directory
- ~/.composer/cache
# Example job for Node.js
jobs:
node_install:
script:
- npm ci
# Example job for PHP
jobs:
php_install:
script:
- composer install --prefer-dist --no-progress --no-suggest
Benefits
- Speed: Caching the package manager's cache directory speeds up dependency installation because it avoids downloading packages that have already been fetched.
-
Consistency: By not caching
node_modulesorvendor, you ensure that your builds are consistent and not affected by platform-specific binaries or changes in the environment.
By following these practices, you can optimize your CI pipeline for both speed and reliability.