Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.

Commit 7219d1b

Browse files
gkalpakthePunderWoman
authored andcommitted
ci: prevent CI cache from growing indefinitely (#41814) (#41855)
Previously, the fallback key used for the CircleCI cache could match a cache indefinitely (as long as `.bazelversion` didn't change). This would allow the cache to grow quite large, which in turn would lead to slow-down in CI jobs. See, also, angular/angular-cli#17533 for more details of the impact of a growing CircleCI cache. Unfortunately, using something like the lockfile checksum in the fallback cache key would cause too many cache misses (esp. with automatic updates via Renovate), again slowing CI down. (The problem was originally discussed [here][2].) This commit uses the technique described in [this blogpost][1] to invalidate the cache monthly. This keeps the extra cache misses low (essentially once per month per fork), while also preventing the cache from growing indefinitely. [1]: https://support.circleci.com/hc/en-us/articles/360012618473-Creating-a-daily-cache [2]: angular/angular#41467 (comment) PR Close #41814 PR Close #41855
1 parent 61ad68a commit 7219d1b

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

.circleci/config.yml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,25 @@ version: 2.1
1919
# 1) yarn lock file changes --> cached "node_modules" are different.
2020
# 2) bazel repository definitions change --> cached bazel repositories are different.
2121
# Windows needs its own cache key because binaries in node_modules are different.
22-
# **NOTE 1 **: If you change the cache key prefix, also sync the cache_key_fallback to match.
23-
# **NOTE 2 **: Keep the static part of the cache key as prefix to enable correct fallbacks.
22+
# **NOTE 1 **: In order to avoid the cache from growing indefinitely and causing slow-downs, we invalidate the cache monthly.
23+
# (See https://support.circleci.com/hc/en-us/articles/360012618473-Creating-a-daily-cache.)
24+
# **NOTE 2 **: If you change the cache key prefix, also sync the cache_key_fallback to match.
25+
# **NOTE 3 **: Keep the static part of the cache key as prefix to enable correct fallbacks.
2426
# See https://circleci.com/docs/2.0/caching/#restoring-cache for how prefixes work in CircleCI.
25-
var_3: &cache_key v4-angular-node-12-{{ checksum ".bazelversion" }}-{{ checksum "yarn.lock" }}-{{ checksum "WORKSPACE" }}-{{ checksum "packages/bazel/package.bzl" }}-{{ checksum "aio/yarn.lock" }}
27+
var_3: &cache_key v4-angular-node-12-{{ checksum "month.txt" }}-{{ checksum ".bazelversion" }}-{{ checksum "yarn.lock" }}-{{ checksum "WORKSPACE" }}-{{ checksum "packages/bazel/package.bzl" }}-{{ checksum "aio/yarn.lock" }}
2628
# We invalidate the cache if the Bazel version changes because otherwise the `bazelisk` cache
2729
# folder will contain all previously used versions and ultimately cause the cache restoring to
2830
# be slower due to its growing size.
29-
var_4: &cache_key_fallback v4-angular-node-12-{{ checksum ".bazelversion" }}
31+
var_4: &cache_key_fallback v4-angular-node-12-{{ checksum "month.txt" }}-{{ checksum ".bazelversion" }}
3032

3133
# Windows needs its own cache key because binaries in node_modules are different.
32-
var_3_win: &cache_key_win v4-angular-win-node-12-{{ checksum ".bazelversion" }}-{{ checksum "yarn.lock" }}-{{ checksum "WORKSPACE" }}-{{ checksum "packages/bazel/package.bzl" }}-{{ checksum "aio/yarn.lock" }}
33-
var_4_win: &cache_key_win_fallback v4-angular-win-node-12-{{ checksum ".bazelversion" }}
34+
var_3_win: &cache_key_win v4-angular-win-node-12-{{ checksum "month.txt" }}-{{ checksum ".bazelversion" }}-{{ checksum "yarn.lock" }}-{{ checksum "WORKSPACE" }}-{{ checksum "packages/bazel/package.bzl" }}-{{ checksum "aio/yarn.lock" }}
35+
var_4_win: &cache_key_win_fallback v4-angular-win-node-12-{{ checksum "month.txt" }}-{{ checksum ".bazelversion" }}
3436

3537
# Cache key for the `components-repo-unit-tests` job. **Note** when updating the SHA in the
3638
# cache keys also update the SHA for the "COMPONENTS_REPO_COMMIT" environment variable.
37-
var_5: &components_repo_unit_tests_cache_key v1-angular-components-09e68db8ed5b1253f2fe38ff954ef0df019fc25a
38-
var_6: &components_repo_unit_tests_cache_key_fallback v1-angular-components-
39+
var_5: &components_repo_unit_tests_cache_key v1-angular-components-{{ checksum "month.txt" }}-09e68db8ed5b1253f2fe38ff954ef0df019fc25a
40+
var_6: &components_repo_unit_tests_cache_key_fallback v1-angular-components-{{ checksum "month.txt" }}
3941

4042
# Workspace initially persisted by the `setup` job, and then enhanced by `build-npm-packages` and
4143
# `build-ivy-npm-packages`.
@@ -180,13 +182,21 @@ commands:
180182
name: Setting up alias domain for local host.
181183
command: echo "127.0.0.1 $SAUCE_LOCALHOST_ALIAS_DOMAIN" | sudo tee -a /etc/hosts
182184

185+
save_month_to_file:
186+
description: Store the current year and month in a file, so that it can be used for computing the cache key.
187+
steps:
188+
- run:
189+
name: Save month to file
190+
command: date +%Y-%m > month.txt
191+
183192
# Normally this would be an individual job instead of a command.
184193
# But startup and setup time for each individual windows job are high enough to discourage
185194
# many small jobs, so instead we use a command for setup unless the gain becomes significant.
186195
setup_win:
187196
description: Setup windows node environment
188197
steps:
189198
- checkout
199+
- save_month_to_file
190200
# Install Bazel pre-requisites that aren't in the preconfigured CircleCI Windows VM.
191201
- run: ./.circleci/windows-env.ps1
192202
- run: node --version
@@ -220,6 +230,7 @@ jobs:
220230
executor: default-executor
221231
steps:
222232
- checkout
233+
- save_month_to_file
223234
- init_environment
224235
- run:
225236
name: Rebase PR on target branch

0 commit comments

Comments
 (0)