You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: jekyll/_cci2/caching.md
+58-8Lines changed: 58 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ Caching is one of the most effective ways to make jobs faster on CircleCI. By re
15
15
* TOC
16
16
{:toc}
17
17
18
-

18
+

19
19
20
20
Caching is particularly useful with **package dependency managers** such as Yarn, Bundler, or Pip. With dependencies restored from a cache, commands like `yarn install` need only download new or updated dependencies, rather than downloading everything on each build.
21
21
@@ -40,7 +40,7 @@ The Docker images used for CircleCI jobs are automatically cached on the server
40
40
**Warning:** Although several examples are included below, caching strategies need to be carefully planned for each individual project. Copying and pasting the code examples will not always be appropriate for your needs.
41
41
{: class="alert alert-warning"}
42
42
43
-
For information about caching and reuse of unchanged layers of a Docker image, see the [Docker Layer Caching]({{site.baseurl}}/2.0/docker-layer-caching/) document.
43
+
For information about caching and reuse of unchanged layers of a Docker image, see the [Docker Layer Caching]({{site.baseurl}}/2.0/docker-layer-caching/) document.
44
44
45
45
## How caching works
46
46
{: #how-caching-works }
@@ -55,7 +55,7 @@ Caching is about achieving a balance between reliability and getting maximum per
55
55
### Saving cache
56
56
{: #saving-cache }
57
57
58
-
CircleCI manual dependency caching requires you to be explicit about what you cache and how you cache it. See the [save cache section]({{site.baseurl}}/2.0/configuration-reference/#save_cache) of the Configuring CircleCI document for additional examples.
58
+
CircleCI manual dependency caching requires you to be explicit about what you cache and how you cache it. See the [save cache section]({{site.baseurl}}/2.0/configuration-reference/#save_cache) of the Configuring CircleCI document for additional examples.
59
59
60
60
To save a cache of a file or directory, add the `save_cache` step to a job in your `.circleci/config.yml` file:
61
61
@@ -72,7 +72,7 @@ CircleCI imposes a 900-character limit on the length of a `key`. Be sure to keep
72
72
The path for directories is relative to the `working_directory` of your job. You can specify an absolute path if you choose.
73
73
74
74
**Note:**
75
-
Unlike the special step [`persist_to_workspace`]({{site.baseurl}}/2.0/configuration-reference/#persist_to_workspace), neither `save_cache` nor `restore_cache` support globbing for the `paths` key.
75
+
Unlike the special step [`persist_to_workspace`]({{site.baseurl}}/2.0/configuration-reference/#persist_to_workspace), neither `save_cache` nor `restore_cache` support globbing for the `paths` key.
76
76
77
77
### Restoring cache
78
78
{: #restoring-cache }
@@ -100,7 +100,57 @@ Each line in the `keys:` list manages _one cache_ (each line does **not** corres
100
100
101
101
The first key concatenates the checksum of `package-lock.json` file into the string `v1-npm-deps-`. If this file changed in your commit, CircleCI would see a new cache key.
102
102
103
-
The next key does not have a dynamic component to it. It is simply a static string: `v1-npm-deps-`. If you would like to invalidate your cache manually, you can bump `v1` to `v2` in your `config.yml` file. In this case, you would now have a new cache key `v2-npm-deps`, which triggers the storing of a new cache.
103
+
The next key does not have a dynamic component to it. It is simply a static string: `v1-npm-deps-`. If you would like to invalidate your cache manually, you can bump `v1` to `v2` in your `.circleci/config.yml` file. In this case, you would now have a new cache key `v2-npm-deps`, which triggers the storing of a new cache.
[Yarn](https://yarnpkg.com/) is an open-source package manager for JavaScript. The packages it installs can be cached, which can speed up builds, but, more importantly, can reduce errors related to network connectivity.
109
+
110
+
Please note, the release of Yarn 2.x comes with a the ability to do [Zero Installs](https://yarnpkg.com/features/zero-installs). If you are using Zero Installs, you should not need to do any special caching within CircleCI.
111
+
112
+
If you are using Yarn 2.x _without_ Zero Installs, you can do something like the following:
113
+
114
+
{% raw %}
115
+
```yaml
116
+
#...
117
+
- restore_cache:
118
+
name: Restore Yarn Package Cache
119
+
keys:
120
+
- yarn-packages-{{ checksum "yarn.lock" }}
121
+
- run:
122
+
name: Install Dependencies
123
+
command: yarn install --immutable
124
+
- save_cache:
125
+
name: Save Yarn Package Cache
126
+
key: yarn-packages-{{ checksum "yarn.lock" }}
127
+
paths:
128
+
- .yarn/cache
129
+
- .yarn/unplugged
130
+
#...
131
+
```
132
+
{% endraw %}
133
+
134
+
If you are using Yarn 1.x, you can do something like the following:
@@ -116,9 +166,9 @@ If your project is open source/available to be forked and receive PRs from contr
116
166
117
167
If a job fetches data at any point, it is likely that you can make use of caching. The most important dependencies to cache during a job are the libraries on which your project depends. For example, cache the libraries that are installed with `pip` in Python or `npm` for Node.js. The various language dependency managers, for example `npm` or `pip`, each have their own paths where dependencies are installed. See our Language guides and [demo projects]({{site.baseurl}}/2.0/demo-apps/) for the specifics for your stack.
118
168
119
-
Tools that are not explicitly required for your project are best stored on the Docker image. The Docker image(s) prebuilt by CircleCI have tools preinstalled that are generic for building projects using the relevant language. For example, the `circleci/ruby:2.4.1` image includes useful tools like git, openssh-client, and gzip.
169
+
Tools that are not explicitly required for your project are best stored on the Docker image. The Docker image(s) prebuilt by CircleCI have tools preinstalled that are generic for building projects using the relevant language. For example, the `cimg/ruby:3.1.2` image includes useful tools like git, openssh-client, and gzip.
We recommend that you verify that the dependencies installation step succeeds before adding caching steps. Caching a failed dependency step will require you to change the cache key in order to avoid failed builds due to a bad cache.
124
174
@@ -269,7 +319,7 @@ There are many different approaches to utilizing caching in monorepos. The follo
269
319
270
320
Caches cannot be cleared. If you need to generate a new set of caches you can update the cache key, similar to the previous example. You might wish to do this if you have updated language or build management tool versions.
271
321
272
-
Updating the cache key on save and restore steps in your '.circleci/config.yml' file will then generate new sets of caches from that point onwards. Please note that older commits using the previous keys may still generate and save cache, so it is recommended that you rebase after the 'config.yml' changes when possible.
322
+
Updating the cache key on save and restore steps in your `.circleci/config.yml` file will then generate new sets of caches from that point onwards. Please note that older commits using the previous keys may still generate and save cache, so it is recommended that you rebase after the 'config.yml' changes when possible.
273
323
274
324
If you create a new cache by incrementing the cache version, the "older" cache is still stored. It is important to be aware that you are creating an additional cache. This method will increase your storage usage. As a general best practice, you should review what is currently being cached and reduce your storage usage as much as possible.
0 commit comments