Skip to content

Commit c534ad2

Browse files
Aiqiao YanAiqiao Yan
authored andcommitted
Add docs and tests
1 parent 15fefd9 commit c534ad2

File tree

9 files changed

+209
-1
lines changed

9 files changed

+209
-1
lines changed

.github/workflows/cache-tests.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: cache-unit-tests
2+
on: push
3+
4+
jobs:
5+
build:
6+
name: Build
7+
8+
strategy:
9+
matrix:
10+
runs-on: [ubuntu-latest, windows-latest, macOS-latest]
11+
fail-fast: false
12+
13+
runs-on: ${{ matrix.runs-on }}
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
19+
- name: Set Node.js 12.x
20+
uses: actions/setup-node@v1
21+
with:
22+
node-version: 12.x
23+
24+
# In order to save & restore cache artifacts from a shell script, certain env variables need to be set that are only available in the
25+
# node context. This runs a local action that gets and sets the necessary env variables that are needed
26+
- name: Set env variables
27+
uses: ./packages/cache/__tests__/__fixtures__/
28+
29+
# Need root node_modules because certain npm packages like jest are configured for the entire repository and it won't be possible
30+
# without these to just compile the cache package
31+
- name: Install root npm packages
32+
run: npm ci
33+
34+
- name: Compile cache package
35+
run: |
36+
npm ci
37+
npm run tsc
38+
working-directory: packages/cache
39+
40+
- name: Generate files in working directory
41+
shell: bash
42+
run: scripts/create-cache-files.sh ${{ runner.os }} test-cache
43+
44+
- name: Generate files outside working directory
45+
shell: bash
46+
run: scripts/create-cache-files.sh ${{ runner.os }} ~/test-cache
47+
48+
# We're using node -e to call the functions directly available in the @actions/cache package
49+
- name: Save cache using saveCache()
50+
run: |
51+
node -e "Promise.resolve(require('./packages/cache/lib/cache').saveCache(['test-cache','~/test-cache'],'test-${{ runner.os }}-${{ github.run_id }}'))"
52+
53+
- name: Restore cache using restoreCache()
54+
run: |
55+
node -e "Promise.resolve(require('./packages/cache/lib/cache').restoreCache(['test-cache','~/test-cache'],'test-${{ runner.os }}-${{ github.run_id }}'))"
56+
57+
- name: Verify cache
58+
shell: bash
59+
run: |
60+
scripts/verify-cache-files.sh ${{ runner.os }} test-cache
61+
scripts/verify-cache-files.sh ${{ runner.os }} ~/test-cache

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ $ npm install @actions/io --save
5959

6060
Provides functions for downloading and caching tools. e.g. setup-* actions. Read more [here](packages/tool-cache)
6161

62+
See @actions/cache for caching workflow dependencies.
63+
6264
```bash
6365
$ npm install @actions/tool-cache --save
6466
```
@@ -84,7 +86,7 @@ $ npm install @actions/artifact --save
8486

8587
:dart: [@actions/cache](packages/cache)
8688

87-
Provides functions to cache dependencies and build outputs to improve workflow execution time.. Read more [here](packages/cache)
89+
Provides functions to cache dependencies and build outputs to improve workflow execution time. Read more [here](packages/cache)
8890

8991
```bash
9092
$ npm install @actions/cache --save

packages/cache/CONTRIBUTIONS.md

Whitespace-only changes.

packages/cache/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,36 @@
66

77
#### Restore Cache
88

9+
Restores a cache based on `key` and `restoreKeys` to the `paths` provided. Function returns the cache key for cache hit and returns undefined if cache not found.
10+
11+
```js
12+
const cache = require('@actions/cache');
13+
const paths = [
14+
'node_modules',
15+
'packages/*/node_modules/'
16+
]
17+
const key = 'npm-foobar-d5ea0750'
18+
const restoreKeys = [
19+
'npm-foobar-',
20+
'npm-'
21+
]
22+
const cacheKey = await cache.restoreCache(paths, key, restoreKeys)
23+
```
24+
925
#### Save Cache
1026

27+
Saves a cache containing the files in `paths` using the `key` provided. Function returns the cache id if the cache was save succesfully.
28+
29+
```js
30+
const cache = require('@actions/cache');
31+
const paths = [
32+
'node_modules',
33+
'packages/*/node_modules/'
34+
]
35+
const key = 'npm-foobar-d5ea0750'
36+
const cacheId = await cache.saveCache(paths, key)
37+
```
38+
1139
## Additional Documentation
1240

1341
See ["Caching dependencies to speed up workflows"](https://help.github.com/github/automating-your-workflow-with-github-actions/caching-dependencies-to-speed-up-workflows).
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: 'Set env variables'
2+
description: 'Sets certain env variables so that e2e restore and save cache can be tested in a shell'
3+
runs:
4+
using: 'node12'
5+
main: 'index.js'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Certain env variables are not set by default in a shell context and are only available in a node context from a running action
2+
// In order to be able to restore and save cache e2e in a shell when running CI tests, we need these env variables set
3+
console.log(`::set-env name=ACTIONS_RUNTIME_URL::${process.env.ACTIONS_RUNTIME_URL}`)
4+
console.log(`::set-env name=ACTIONS_RUNTIME_TOKEN::${process.env.ACTIONS_RUNTIME_TOKEN}`)
5+
console.log(`::set-env name=GITHUB_RUN_ID::${process.env.GITHUB_RUN_ID}`)

packages/cache/package-lock.json

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/create-cache-files.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
# Validate args
4+
prefix="$1"
5+
if [ -z "$prefix" ]; then
6+
echo "Must supply prefix argument"
7+
exit 1
8+
fi
9+
10+
path="$2"
11+
if [ -z "$path" ]; then
12+
echo "Must supply path argument"
13+
exit 1
14+
fi
15+
16+
mkdir -p $path
17+
echo "$prefix $GITHUB_RUN_ID" > $path/test-file.txt

scripts/verify-cache-files.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
3+
# Validate args
4+
prefix="$1"
5+
if [ -z "$prefix" ]; then
6+
echo "Must supply prefix argument"
7+
exit 1
8+
fi
9+
10+
path="$2"
11+
if [ -z "$path" ]; then
12+
echo "Must specify path argument"
13+
exit 1
14+
fi
15+
16+
# Sanity check GITHUB_RUN_ID defined
17+
if [ -z "$GITHUB_RUN_ID" ]; then
18+
echo "GITHUB_RUN_ID not defined"
19+
exit 1
20+
fi
21+
22+
# Verify file exists
23+
file="$path/test-file.txt"
24+
echo "Checking for $file"
25+
if [ ! -e $file ]; then
26+
echo "File does not exist"
27+
exit 1
28+
fi
29+
30+
# Verify file content
31+
content="$(cat $file)"
32+
echo "File content:\n$content"
33+
if [ -z "$(echo $content | grep --fixed-strings "$prefix $GITHUB_RUN_ID")" ]; then
34+
echo "Unexpected file content"
35+
exit 1
36+
fi

0 commit comments

Comments
 (0)