Skip to content

Commit 2b83e91

Browse files
authored
Add examples for creating a cache key (#312)
1 parent 16a133d commit 2b83e91

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

README.md

+34-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ Create a workflow `.yml` file in your repositories `.github/workflows` directory
2525

2626
> See [Skipping steps based on cache-hit](#Skipping-steps-based-on-cache-hit) for info on using this output
2727
28-
### Branch scope
28+
### Cache scopes
2929
The cache is scoped to the key and branch. The default branch cache is available to other branches.
3030

31-
See https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key for more info.
31+
See [Matching a cache key](https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key) for more info.
3232

3333
### Example workflow
3434

@@ -86,6 +86,37 @@ See [Examples](examples.md) for a list of `actions/cache` implementations for us
8686
- [Swift, Objective-C - CocoaPods](./examples.md#swift-objective-c---cocoapods)
8787
- [Swift - Swift Package Manager](./examples.md#swift---swift-package-manager)
8888

89+
## Creating a cache key
90+
91+
A cache key can include any of the contexts, functions, literals, and operators supported by GitHub Actions.
92+
93+
For example, using the [`hashFiles`](https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#hashfiles) function allows you to create a new cache when dependencies change.
94+
95+
```yaml
96+
- uses: actions/cache@v1
97+
with:
98+
path: path/to/dependencies
99+
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
100+
```
101+
102+
Additionally, you can use arbitrary command output in a cache key, such as a date or software version:
103+
104+
```yaml
105+
# http://man7.org/linux/man-pages/man1/date.1.html
106+
- name: Get Date
107+
id: get-date
108+
run: |
109+
echo "::set-output name=date::$(/bin/date -u "+%Y%m%d")"
110+
shell: bash
111+
112+
- uses: actions/cache@v1
113+
with:
114+
path: path/to/dependencies
115+
key: ${{ runner.os }}-${{ steps.get-date.outputs.date }}-${{ hashFiles('**/lockfiles') }}
116+
```
117+
118+
See [Using contexts to create cache keys](https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#using-contexts-to-create-cache-keys)
119+
89120
## Cache Limits
90121

91122
A repository can have up to 5GB of caches. Once the 5GB limit is reached, older caches will be evicted based on when the cache was last accessed. Caches that are not accessed within the last week will also be evicted.
@@ -113,7 +144,7 @@ steps:
113144
> Note: The `id` defined in `actions/cache` must match the `id` in the `if` statement (i.e. `steps.[ID].outputs.cache-hit`)
114145

115146
## Contributing
116-
We would love for you to contribute to `@actions/cache`, pull requests are welcome! Please see the [CONTRIBUTING.md](CONTRIBUTING.md) for more information.
147+
We would love for you to contribute to `actions/cache`, pull requests are welcome! Please see the [CONTRIBUTING.md](CONTRIBUTING.md) for more information.
117148

118149
## License
119150
The scripts and documentation in this project are released under the [MIT License](LICENSE)

0 commit comments

Comments
 (0)