mirror of
https://github.com/actions/cache.git
synced 2025-06-25 11:51:11 +02:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
cffae9552b | |||
44543250bd | |||
6491e51b66 | |||
86dff562ab | |||
0f810ad45a | |||
9d8c7b4041 |
@ -12,12 +12,5 @@
|
|||||||
"plugin:prettier/recommended",
|
"plugin:prettier/recommended",
|
||||||
"prettier/@typescript-eslint"
|
"prettier/@typescript-eslint"
|
||||||
],
|
],
|
||||||
"plugins": ["@typescript-eslint", "simple-import-sort", "jest"],
|
"plugins": ["@typescript-eslint", "jest"]
|
||||||
"rules": {
|
|
||||||
"import/first": "error",
|
|
||||||
"import/newline-after-import": "error",
|
|
||||||
"import/no-duplicates": "error",
|
|
||||||
"simple-import-sort/sort": "error",
|
|
||||||
"sort-imports": "off"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
35
.github/workflows/codeql.yml
vendored
35
.github/workflows/codeql.yml
vendored
@ -1,35 +0,0 @@
|
|||||||
name: "Code Scanning - Action"
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
schedule:
|
|
||||||
- cron: '0 0 * * 0'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
CodeQL-Build:
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
|
|
||||||
|
|
||||||
# CodeQL runs on ubuntu-latest, windows-latest, and macos-latest
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
|
||||||
- name: Initialize CodeQL
|
|
||||||
uses: github/codeql-action/init@v1
|
|
||||||
# Override language selection by uncommenting this and choosing your languages
|
|
||||||
# with:
|
|
||||||
# languages: go, javascript, csharp, python, cpp, java
|
|
||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
||||||
# If this step fails, then you should remove it and run the build manually (see below).
|
|
||||||
- name: Autobuild
|
|
||||||
uses: github/codeql-action/autobuild@v1
|
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
|
||||||
uses: github/codeql-action/analyze@v1
|
|
135
.github/workflows/workflow.yml
vendored
135
.github/workflows/workflow.yml
vendored
@ -4,151 +4,50 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
- releases/**
|
|
||||||
paths-ignore:
|
paths-ignore:
|
||||||
- '**.md'
|
- '**.md'
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
- releases/**
|
|
||||||
paths-ignore:
|
paths-ignore:
|
||||||
- '**.md'
|
- '**.md'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# Build and unit test
|
test:
|
||||||
build:
|
name: Test on ${{ matrix.os }}
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
os: [ubuntu-latest, ubuntu-16.04, windows-latest, macOS-latest]
|
os: [ubuntu-latest, windows-latest, macOS-latest]
|
||||||
fail-fast: false
|
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- uses: actions/checkout@v1
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: Setup Node.js
|
- uses: actions/setup-node@v1
|
||||||
uses: actions/setup-node@v1
|
|
||||||
with:
|
with:
|
||||||
node-version: '12.x'
|
node-version: '12.x'
|
||||||
- name: Determine npm cache directory
|
|
||||||
|
- name: Get npm cache directory
|
||||||
id: npm-cache
|
id: npm-cache
|
||||||
run: |
|
run: |
|
||||||
echo "::set-output name=dir::$(npm config get cache)"
|
echo "::set-output name=dir::$(npm config get cache)"
|
||||||
- name: Restore npm cache
|
|
||||||
uses: actions/cache@v1
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: ${{ steps.npm-cache.outputs.dir }}
|
path: ${{ steps.npm-cache.outputs.dir }}
|
||||||
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
|
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-node-
|
${{ runner.os }}-node-
|
||||||
|
|
||||||
- run: npm ci
|
- run: npm ci
|
||||||
|
|
||||||
- name: Prettier Format Check
|
- name: Prettier Format Check
|
||||||
run: npm run format-check
|
run: npm run format-check
|
||||||
|
|
||||||
- name: ESLint Check
|
- name: ESLint Check
|
||||||
run: npm run lint
|
run: npm run lint
|
||||||
|
|
||||||
- name: Build & Test
|
- name: Build & Test
|
||||||
run: npm run test
|
run: npm run test
|
||||||
- name: Ensure dist/ folder is up-to-date
|
|
||||||
if: ${{ runner.os == 'Linux' }}
|
|
||||||
shell: bash
|
|
||||||
run: |
|
|
||||||
npm run build
|
|
||||||
if [ "$(git status --porcelain | wc -l)" -gt "0" ]; then
|
|
||||||
echo "Detected uncommitted changes after build. See status below:"
|
|
||||||
git diff
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
# End to end save and restore
|
|
||||||
test-save:
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [ubuntu-latest, ubuntu-16.04, windows-latest, macOS-latest]
|
|
||||||
fail-fast: false
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: Generate files in working directory
|
|
||||||
shell: bash
|
|
||||||
run: __tests__/create-cache-files.sh ${{ runner.os }} test-cache
|
|
||||||
- name: Generate files outside working directory
|
|
||||||
shell: bash
|
|
||||||
run: __tests__/create-cache-files.sh ${{ runner.os }} ~/test-cache
|
|
||||||
- name: Save cache
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
key: test-${{ runner.os }}-${{ github.run_id }}
|
|
||||||
path: |
|
|
||||||
test-cache
|
|
||||||
~/test-cache
|
|
||||||
test-restore:
|
|
||||||
needs: test-save
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [ubuntu-latest, ubuntu-16.04, windows-latest, macOS-latest]
|
|
||||||
fail-fast: false
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: Restore cache
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
key: test-${{ runner.os }}-${{ github.run_id }}
|
|
||||||
path: |
|
|
||||||
test-cache
|
|
||||||
~/test-cache
|
|
||||||
- name: Verify cache files in working directory
|
|
||||||
shell: bash
|
|
||||||
run: __tests__/verify-cache-files.sh ${{ runner.os }} test-cache
|
|
||||||
- name: Verify cache files outside working directory
|
|
||||||
shell: bash
|
|
||||||
run: __tests__/verify-cache-files.sh ${{ runner.os }} ~/test-cache
|
|
||||||
|
|
||||||
# End to end with proxy
|
|
||||||
test-proxy-save:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
container:
|
|
||||||
image: ubuntu:latest
|
|
||||||
options: --dns 127.0.0.1
|
|
||||||
services:
|
|
||||||
squid-proxy:
|
|
||||||
image: datadog/squid:latest
|
|
||||||
ports:
|
|
||||||
- 3128:3128
|
|
||||||
env:
|
|
||||||
https_proxy: http://squid-proxy:3128
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: Generate files
|
|
||||||
run: __tests__/create-cache-files.sh proxy test-cache
|
|
||||||
- name: Save cache
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
key: test-proxy-${{ github.run_id }}
|
|
||||||
path: test-cache
|
|
||||||
test-proxy-restore:
|
|
||||||
needs: test-proxy-save
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
container:
|
|
||||||
image: ubuntu:latest
|
|
||||||
options: --dns 127.0.0.1
|
|
||||||
services:
|
|
||||||
squid-proxy:
|
|
||||||
image: datadog/squid:latest
|
|
||||||
ports:
|
|
||||||
- 3128:3128
|
|
||||||
env:
|
|
||||||
https_proxy: http://squid-proxy:3128
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
- name: Restore cache
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
key: test-proxy-${{ github.run_id }}
|
|
||||||
path: test-cache
|
|
||||||
- name: Verify cache
|
|
||||||
run: __tests__/verify-cache-files.sh proxy test-cache
|
|
||||||
|
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,5 +1,8 @@
|
|||||||
__tests__/runner/*
|
__tests__/runner/*
|
||||||
|
|
||||||
|
# comment out in distribution branches
|
||||||
|
dist/
|
||||||
|
|
||||||
node_modules/
|
node_modules/
|
||||||
lib/
|
lib/
|
||||||
|
|
||||||
@ -91,6 +94,3 @@ typings/
|
|||||||
|
|
||||||
# DynamoDB Local files
|
# DynamoDB Local files
|
||||||
.dynamodb/
|
.dynamodb/
|
||||||
|
|
||||||
# Text editor files
|
|
||||||
.vscode/
|
|
||||||
|
103
README.md
103
README.md
@ -1,6 +1,6 @@
|
|||||||
# cache
|
# cache
|
||||||
|
|
||||||
This action allows caching dependencies and build outputs to improve workflow execution time.
|
This GitHub Action allows caching dependencies and build outputs to improve workflow execution time.
|
||||||
|
|
||||||
<a href="https://github.com/actions/cache/actions?query=workflow%3ATests"><img alt="GitHub Actions status" src="https://github.com/actions/cache/workflows/Tests/badge.svg?branch=master&event=push"></a>
|
<a href="https://github.com/actions/cache/actions?query=workflow%3ATests"><img alt="GitHub Actions status" src="https://github.com/actions/cache/workflows/Tests/badge.svg?branch=master&event=push"></a>
|
||||||
|
|
||||||
@ -8,28 +8,6 @@ This action allows caching dependencies and build outputs to improve workflow ex
|
|||||||
|
|
||||||
See ["Caching dependencies to speed up workflows"](https://help.github.com/github/automating-your-workflow-with-github-actions/caching-dependencies-to-speed-up-workflows).
|
See ["Caching dependencies to speed up workflows"](https://help.github.com/github/automating-your-workflow-with-github-actions/caching-dependencies-to-speed-up-workflows).
|
||||||
|
|
||||||
## What's New
|
|
||||||
|
|
||||||
* Added support for multiple paths, [glob patterns](https://github.com/actions/toolkit/tree/master/packages/glob), and single file caches.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- name: Cache multiple paths
|
|
||||||
uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
~/cache
|
|
||||||
!~/cache/exclude
|
|
||||||
**/node_modules
|
|
||||||
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
|
|
||||||
```
|
|
||||||
|
|
||||||
* Increased performance and improved cache sizes using `zstd` compression for Linux and macOS runners
|
|
||||||
* Allowed caching for all events with a ref. See [events that trigger workflow](https://help.github.com/en/actions/reference/events-that-trigger-workflows) for info on which events do not have a `GITHUB_REF`
|
|
||||||
* Released the [`@actions/cache`](https://github.com/actions/toolkit/tree/master/packages/cache) npm package to allow other actions to utilize caching
|
|
||||||
* Added a best-effort cleanup step to delete the archive after extraction to reduce storage space
|
|
||||||
|
|
||||||
Refer [here](https://github.com/actions/cache/blob/v1/README.md) for previous versions
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Pre-requisites
|
### Pre-requisites
|
||||||
@ -37,7 +15,7 @@ Create a workflow `.yml` file in your repositories `.github/workflows` directory
|
|||||||
|
|
||||||
### Inputs
|
### Inputs
|
||||||
|
|
||||||
* `path` - A list of files, directories, and wildcard patterns to cache and restore. See [`@actions/glob`](https://github.com/actions/toolkit/tree/master/packages/glob) for supported patterns.
|
* `path` - A directory to store and save the cache
|
||||||
* `key` - An explicit key for restoring and saving the cache
|
* `key` - An explicit key for restoring and saving the cache
|
||||||
* `restore-keys` - An ordered list of keys to use for restoring the cache if no cache hit occurred for key
|
* `restore-keys` - An ordered list of keys to use for restoring the cache if no cache hit occurred for key
|
||||||
|
|
||||||
@ -47,11 +25,6 @@ Create a workflow `.yml` file in your repositories `.github/workflows` directory
|
|||||||
|
|
||||||
> See [Skipping steps based on cache-hit](#Skipping-steps-based-on-cache-hit) for info on using this output
|
> See [Skipping steps based on cache-hit](#Skipping-steps-based-on-cache-hit) for info on using this output
|
||||||
|
|
||||||
### Cache scopes
|
|
||||||
The cache is scoped to the key and branch. The default branch cache is available to other branches.
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
### Example workflow
|
### Example workflow
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
@ -64,11 +37,11 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v1
|
||||||
|
|
||||||
- name: Cache Primes
|
- name: Cache Primes
|
||||||
id: cache-primes
|
id: cache-primes
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: prime-numbers
|
path: prime-numbers
|
||||||
key: ${{ runner.os }}-primes
|
key: ${{ runner.os }}-primes
|
||||||
@ -81,69 +54,13 @@ jobs:
|
|||||||
run: /primes.sh -d prime-numbers
|
run: /primes.sh -d prime-numbers
|
||||||
```
|
```
|
||||||
|
|
||||||
## Implementation Examples
|
## Ecosystem Examples
|
||||||
|
|
||||||
Every programming language and framework has its own way of caching.
|
See [Examples](examples.md)
|
||||||
|
|
||||||
See [Examples](examples.md) for a list of `actions/cache` implementations for use with:
|
|
||||||
|
|
||||||
- [C# - Nuget](./examples.md#c---nuget)
|
|
||||||
- [D - DUB](./examples.md#d---dub)
|
|
||||||
- [Elixir - Mix](./examples.md#elixir---mix)
|
|
||||||
- [Go - Modules](./examples.md#go---modules)
|
|
||||||
- [Haskell - Cabal](./examples.md#haskell---cabal)
|
|
||||||
- [Java - Gradle](./examples.md#java---gradle)
|
|
||||||
- [Java - Maven](./examples.md#java---maven)
|
|
||||||
- [Node - npm](./examples.md#node---npm)
|
|
||||||
- [Node - Lerna](./examples.md#node---lerna)
|
|
||||||
- [Node - Yarn](./examples.md#node---yarn)
|
|
||||||
- [OCaml/Reason - esy](./examples.md##ocamlreason---esy)
|
|
||||||
- [PHP - Composer](./examples.md#php---composer)
|
|
||||||
- [Python - pip](./examples.md#python---pip)
|
|
||||||
- [R - renv](./examples.md#r---renv)
|
|
||||||
- [Ruby - Bundler](./examples.md#ruby---bundler)
|
|
||||||
- [Rust - Cargo](./examples.md#rust---cargo)
|
|
||||||
- [Scala - SBT](./examples.md#scala---sbt)
|
|
||||||
- [Swift, Objective-C - Carthage](./examples.md#swift-objective-c---carthage)
|
|
||||||
- [Swift, Objective-C - CocoaPods](./examples.md#swift-objective-c---cocoapods)
|
|
||||||
- [Swift - Swift Package Manager](./examples.md#swift---swift-package-manager)
|
|
||||||
|
|
||||||
## Creating a cache key
|
|
||||||
|
|
||||||
A cache key can include any of the contexts, functions, literals, and operators supported by GitHub Actions.
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
path/to/dependencies
|
|
||||||
some/other/dependencies
|
|
||||||
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
|
|
||||||
```
|
|
||||||
|
|
||||||
Additionally, you can use arbitrary command output in a cache key, such as a date or software version:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
# http://man7.org/linux/man-pages/man1/date.1.html
|
|
||||||
- name: Get Date
|
|
||||||
id: get-date
|
|
||||||
run: |
|
|
||||||
echo "::set-output name=date::$(/bin/date -u "+%Y%m%d")"
|
|
||||||
shell: bash
|
|
||||||
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: path/to/dependencies
|
|
||||||
key: ${{ runner.os }}-${{ steps.get-date.outputs.date }}-${{ hashFiles('**/lockfiles') }}
|
|
||||||
```
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
## Cache Limits
|
## Cache Limits
|
||||||
|
|
||||||
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.
|
Individual caches are limited to 400MB and a repository can have up to 2GB of caches. Once the 2GB 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.
|
||||||
|
|
||||||
## Skipping steps based on cache-hit
|
## Skipping steps based on cache-hit
|
||||||
|
|
||||||
@ -152,9 +69,9 @@ Using the `cache-hit` output, subsequent steps (such as install or build) can be
|
|||||||
Example:
|
Example:
|
||||||
```yaml
|
```yaml
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v1
|
||||||
|
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
id: cache
|
id: cache
|
||||||
with:
|
with:
|
||||||
path: path/to/dependencies
|
path: path/to/dependencies
|
||||||
@ -168,7 +85,7 @@ steps:
|
|||||||
> Note: The `id` defined in `actions/cache` must match the `id` in the `if` statement (i.e. `steps.[ID].outputs.cache-hit`)
|
> Note: The `id` defined in `actions/cache` must match the `id` in the `if` statement (i.e. `steps.[ID].outputs.cache-hit`)
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
We would love for you to contribute to `actions/cache`, pull requests are welcome! Please see the [CONTRIBUTING.md](CONTRIBUTING.md) for more information.
|
We would love for you to contribute to `@actions/cache`, pull requests are welcome! Please see the [CONTRIBUTING.md](CONTRIBUTING.md) for more information.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
The scripts and documentation in this project are released under the [MIT License](LICENSE)
|
The scripts and documentation in this project are released under the [MIT License](LICENSE)
|
||||||
|
@ -1,65 +1,84 @@
|
|||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
|
import * as os from "os";
|
||||||
|
import * as path from "path";
|
||||||
|
|
||||||
import { Events, Outputs, RefKey, State } from "../src/constants";
|
import { Events, Outputs, State } from "../src/constants";
|
||||||
|
import { ArtifactCacheEntry } from "../src/contracts";
|
||||||
import * as actionUtils from "../src/utils/actionUtils";
|
import * as actionUtils from "../src/utils/actionUtils";
|
||||||
|
|
||||||
jest.mock("@actions/core");
|
jest.mock("@actions/core");
|
||||||
|
jest.mock("os");
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
delete process.env[Events.Key];
|
delete process.env[Events.Key];
|
||||||
delete process.env[RefKey];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("isExactKeyMatch with undefined cache key returns false", () => {
|
test("getArchiveFileSize returns file size", () => {
|
||||||
const key = "linux-rust";
|
const filePath = path.join(__dirname, "__fixtures__", "helloWorld.txt");
|
||||||
const cacheKey = undefined;
|
|
||||||
|
|
||||||
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
|
const size = actionUtils.getArchiveFileSize(filePath);
|
||||||
|
|
||||||
|
expect(size).toBe(11);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("isExactKeyMatch with empty cache key returns false", () => {
|
test("isExactKeyMatch with undefined cache entry returns false", () => {
|
||||||
const key = "linux-rust";
|
const key = "linux-rust";
|
||||||
const cacheKey = "";
|
const cacheEntry = undefined;
|
||||||
|
|
||||||
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isExactKeyMatch with empty cache entry returns false", () => {
|
||||||
|
const key = "linux-rust";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {};
|
||||||
|
|
||||||
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("isExactKeyMatch with different keys returns false", () => {
|
test("isExactKeyMatch with different keys returns false", () => {
|
||||||
const key = "linux-rust";
|
const key = "linux-rust";
|
||||||
const cacheKey = "linux-";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "linux-"
|
||||||
|
};
|
||||||
|
|
||||||
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("isExactKeyMatch with different key accents returns false", () => {
|
test("isExactKeyMatch with different key accents returns false", () => {
|
||||||
const key = "linux-áccent";
|
const key = "linux-áccent";
|
||||||
const cacheKey = "linux-accent";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "linux-accent"
|
||||||
|
};
|
||||||
|
|
||||||
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("isExactKeyMatch with same key returns true", () => {
|
test("isExactKeyMatch with same key returns true", () => {
|
||||||
const key = "linux-rust";
|
const key = "linux-rust";
|
||||||
const cacheKey = "linux-rust";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "linux-rust"
|
||||||
|
};
|
||||||
|
|
||||||
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(true);
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("isExactKeyMatch with same key and different casing returns true", () => {
|
test("isExactKeyMatch with same key and different casing returns true", () => {
|
||||||
const key = "linux-rust";
|
const key = "linux-rust";
|
||||||
const cacheKey = "LINUX-RUST";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "LINUX-RUST"
|
||||||
|
};
|
||||||
|
|
||||||
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(true);
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("setOutputAndState with undefined entry to set cache-hit output", () => {
|
test("setOutputAndState with undefined entry to set cache-hit output", () => {
|
||||||
const key = "linux-rust";
|
const key = "linux-rust";
|
||||||
const cacheKey = undefined;
|
const cacheEntry = undefined;
|
||||||
|
|
||||||
const setOutputMock = jest.spyOn(core, "setOutput");
|
const setOutputMock = jest.spyOn(core, "setOutput");
|
||||||
const saveStateMock = jest.spyOn(core, "saveState");
|
const saveStateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
actionUtils.setOutputAndState(key, cacheKey);
|
actionUtils.setOutputAndState(key, cacheEntry);
|
||||||
|
|
||||||
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "false");
|
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "false");
|
||||||
expect(setOutputMock).toHaveBeenCalledTimes(1);
|
expect(setOutputMock).toHaveBeenCalledTimes(1);
|
||||||
@ -69,33 +88,43 @@ test("setOutputAndState with undefined entry to set cache-hit output", () => {
|
|||||||
|
|
||||||
test("setOutputAndState with exact match to set cache-hit output and state", () => {
|
test("setOutputAndState with exact match to set cache-hit output and state", () => {
|
||||||
const key = "linux-rust";
|
const key = "linux-rust";
|
||||||
const cacheKey = "linux-rust";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "linux-rust"
|
||||||
|
};
|
||||||
|
|
||||||
const setOutputMock = jest.spyOn(core, "setOutput");
|
const setOutputMock = jest.spyOn(core, "setOutput");
|
||||||
const saveStateMock = jest.spyOn(core, "saveState");
|
const saveStateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
actionUtils.setOutputAndState(key, cacheKey);
|
actionUtils.setOutputAndState(key, cacheEntry);
|
||||||
|
|
||||||
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "true");
|
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "true");
|
||||||
expect(setOutputMock).toHaveBeenCalledTimes(1);
|
expect(setOutputMock).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
expect(saveStateMock).toHaveBeenCalledWith(State.CacheMatchedKey, cacheKey);
|
expect(saveStateMock).toHaveBeenCalledWith(
|
||||||
|
State.CacheResult,
|
||||||
|
JSON.stringify(cacheEntry)
|
||||||
|
);
|
||||||
expect(saveStateMock).toHaveBeenCalledTimes(1);
|
expect(saveStateMock).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("setOutputAndState with no exact match to set cache-hit output and state", () => {
|
test("setOutputAndState with no exact match to set cache-hit output and state", () => {
|
||||||
const key = "linux-rust";
|
const key = "linux-rust";
|
||||||
const cacheKey = "linux-rust-bb828da54c148048dd17899ba9fda624811cfb43";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "linux-rust-bb828da54c148048dd17899ba9fda624811cfb43"
|
||||||
|
};
|
||||||
|
|
||||||
const setOutputMock = jest.spyOn(core, "setOutput");
|
const setOutputMock = jest.spyOn(core, "setOutput");
|
||||||
const saveStateMock = jest.spyOn(core, "saveState");
|
const saveStateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
actionUtils.setOutputAndState(key, cacheKey);
|
actionUtils.setOutputAndState(key, cacheEntry);
|
||||||
|
|
||||||
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "false");
|
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "false");
|
||||||
expect(setOutputMock).toHaveBeenCalledTimes(1);
|
expect(setOutputMock).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
expect(saveStateMock).toHaveBeenCalledWith(State.CacheMatchedKey, cacheKey);
|
expect(saveStateMock).toHaveBeenCalledWith(
|
||||||
|
State.CacheResult,
|
||||||
|
JSON.stringify(cacheEntry)
|
||||||
|
);
|
||||||
expect(saveStateMock).toHaveBeenCalledTimes(1);
|
expect(saveStateMock).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -109,23 +138,27 @@ test("getCacheState with no state returns undefined", () => {
|
|||||||
|
|
||||||
expect(state).toBe(undefined);
|
expect(state).toBe(undefined);
|
||||||
|
|
||||||
expect(getStateMock).toHaveBeenCalledWith(State.CacheMatchedKey);
|
expect(getStateMock).toHaveBeenCalledWith(State.CacheResult);
|
||||||
expect(getStateMock).toHaveBeenCalledTimes(1);
|
expect(getStateMock).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("getCacheState with valid state", () => {
|
test("getCacheState with valid state", () => {
|
||||||
const cacheKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
const getStateMock = jest.spyOn(core, "getState");
|
const getStateMock = jest.spyOn(core, "getState");
|
||||||
getStateMock.mockImplementation(() => {
|
getStateMock.mockImplementation(() => {
|
||||||
return cacheKey;
|
return JSON.stringify(cacheEntry);
|
||||||
});
|
});
|
||||||
|
|
||||||
const state = actionUtils.getCacheState();
|
const state = actionUtils.getCacheState();
|
||||||
|
|
||||||
expect(state).toEqual(cacheKey);
|
expect(state).toEqual(cacheEntry);
|
||||||
|
|
||||||
expect(getStateMock).toHaveBeenCalledWith(State.CacheMatchedKey);
|
expect(getStateMock).toHaveBeenCalledWith(State.CacheResult);
|
||||||
expect(getStateMock).toHaveBeenCalledTimes(1);
|
expect(getStateMock).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -139,7 +172,7 @@ test("logWarning logs a message with a warning prefix", () => {
|
|||||||
expect(infoMock).toHaveBeenCalledWith(`[warning]${message}`);
|
expect(infoMock).toHaveBeenCalledWith(`[warning]${message}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("isValidEvent returns false for event that does not have a branch or tag", () => {
|
test("isValidEvent returns false for unknown event", () => {
|
||||||
const event = "foo";
|
const event = "foo";
|
||||||
process.env[Events.Key] = event;
|
process.env[Events.Key] = event;
|
||||||
|
|
||||||
@ -148,10 +181,54 @@ test("isValidEvent returns false for event that does not have a branch or tag",
|
|||||||
expect(isValidEvent).toBe(false);
|
expect(isValidEvent).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("isValidEvent returns true for event that has a ref", () => {
|
test("resolvePath with no ~ in path", () => {
|
||||||
|
const filePath = ".cache/yarn";
|
||||||
|
|
||||||
|
const resolvedPath = actionUtils.resolvePath(filePath);
|
||||||
|
|
||||||
|
const expectedPath = path.resolve(filePath);
|
||||||
|
expect(resolvedPath).toBe(expectedPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("resolvePath with ~ in path", () => {
|
||||||
|
const filePath = "~/.cache/yarn";
|
||||||
|
|
||||||
|
const homedir = jest.requireActual("os").homedir();
|
||||||
|
const homedirMock = jest.spyOn(os, "homedir");
|
||||||
|
homedirMock.mockImplementation(() => {
|
||||||
|
return homedir;
|
||||||
|
});
|
||||||
|
|
||||||
|
const resolvedPath = actionUtils.resolvePath(filePath);
|
||||||
|
|
||||||
|
const expectedPath = path.join(homedir, ".cache/yarn");
|
||||||
|
expect(resolvedPath).toBe(expectedPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("resolvePath with home not found", () => {
|
||||||
|
const filePath = "~/.cache/yarn";
|
||||||
|
const homedirMock = jest.spyOn(os, "homedir");
|
||||||
|
homedirMock.mockImplementation(() => {
|
||||||
|
return "";
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(() => actionUtils.resolvePath(filePath)).toThrow(
|
||||||
|
"Unable to resolve `~` to HOME"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isValidEvent returns true for push event", () => {
|
||||||
const event = Events.Push;
|
const event = Events.Push;
|
||||||
process.env[Events.Key] = event;
|
process.env[Events.Key] = event;
|
||||||
process.env[RefKey] = "ref/heads/feature";
|
|
||||||
|
const isValidEvent = actionUtils.isValidEvent();
|
||||||
|
|
||||||
|
expect(isValidEvent).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isValidEvent returns true for pull request event", () => {
|
||||||
|
const event = Events.PullRequest;
|
||||||
|
process.env[Events.Key] = event;
|
||||||
|
|
||||||
const isValidEvent = actionUtils.isValidEvent();
|
const isValidEvent = actionUtils.isValidEvent();
|
||||||
|
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
# Validate args
|
|
||||||
prefix="$1"
|
|
||||||
if [ -z "$prefix" ]; then
|
|
||||||
echo "Must supply prefix argument"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
path="$2"
|
|
||||||
if [ -z "$path" ]; then
|
|
||||||
echo "Must supply path argument"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
mkdir -p $path
|
|
||||||
echo "$prefix $GITHUB_RUN_ID" > $path/test-file.txt
|
|
@ -1,14 +1,24 @@
|
|||||||
import * as cache from "@actions/cache";
|
|
||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
|
import * as exec from "@actions/exec";
|
||||||
import { Events, Inputs, RefKey } from "../src/constants";
|
import * as io from "@actions/io";
|
||||||
|
import * as path from "path";
|
||||||
|
import * as cacheHttpClient from "../src/cacheHttpClient";
|
||||||
|
import { Events, Inputs } from "../src/constants";
|
||||||
|
import { ArtifactCacheEntry } from "../src/contracts";
|
||||||
import run from "../src/restore";
|
import run from "../src/restore";
|
||||||
import * as actionUtils from "../src/utils/actionUtils";
|
import * as actionUtils from "../src/utils/actionUtils";
|
||||||
import * as testUtils from "../src/utils/testUtils";
|
import * as testUtils from "../src/utils/testUtils";
|
||||||
|
|
||||||
|
jest.mock("@actions/exec");
|
||||||
|
jest.mock("@actions/io");
|
||||||
jest.mock("../src/utils/actionUtils");
|
jest.mock("../src/utils/actionUtils");
|
||||||
|
jest.mock("../src/cacheHttpClient");
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
|
jest.spyOn(actionUtils, "resolvePath").mockImplementation(filePath => {
|
||||||
|
return path.resolve(filePath);
|
||||||
|
});
|
||||||
|
|
||||||
jest.spyOn(actionUtils, "isExactKeyMatch").mockImplementation(
|
jest.spyOn(actionUtils, "isExactKeyMatch").mockImplementation(
|
||||||
(key, cacheResult) => {
|
(key, cacheResult) => {
|
||||||
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
||||||
@ -20,17 +30,24 @@ beforeAll(() => {
|
|||||||
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
||||||
return actualUtils.isValidEvent();
|
return actualUtils.isValidEvent();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
jest.spyOn(actionUtils, "getSupportedEvents").mockImplementation(() => {
|
||||||
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
||||||
|
return actualUtils.getSupportedEvents();
|
||||||
|
});
|
||||||
|
|
||||||
|
jest.spyOn(io, "which").mockImplementation(tool => {
|
||||||
|
return Promise.resolve(tool);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env[Events.Key] = Events.Push;
|
process.env[Events.Key] = Events.Push;
|
||||||
process.env[RefKey] = "refs/heads/feature-branch";
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
testUtils.clearInputs();
|
testUtils.clearInputs();
|
||||||
delete process.env[Events.Key];
|
delete process.env[Events.Key];
|
||||||
delete process.env[RefKey];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("restore with invalid event outputs warning", async () => {
|
test("restore with invalid event outputs warning", async () => {
|
||||||
@ -38,21 +55,17 @@ test("restore with invalid event outputs warning", async () => {
|
|||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const invalidEvent = "commit_comment";
|
const invalidEvent = "commit_comment";
|
||||||
process.env[Events.Key] = invalidEvent;
|
process.env[Events.Key] = invalidEvent;
|
||||||
delete process.env[RefKey];
|
|
||||||
await run();
|
await run();
|
||||||
expect(logWarningMock).toHaveBeenCalledWith(
|
expect(logWarningMock).toHaveBeenCalledWith(
|
||||||
`Event Validation Error: The event type ${invalidEvent} is not supported because it's not tied to a branch or tag ref.`
|
`Event Validation Error: The event type ${invalidEvent} is not supported. Only push, pull_request events are supported at this time.`
|
||||||
);
|
);
|
||||||
expect(failedMock).toHaveBeenCalledTimes(0);
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("restore with no path should fail", async () => {
|
test("restore with no path should fail", async () => {
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const restoreCacheMock = jest.spyOn(cache, "restoreCache");
|
|
||||||
await run();
|
await run();
|
||||||
expect(restoreCacheMock).toHaveBeenCalledTimes(0);
|
expect(failedMock).toHaveBeenCalledWith(
|
||||||
// this input isn't necessary for restore b/c tarball contains entries relative to workspace
|
|
||||||
expect(failedMock).not.toHaveBeenCalledWith(
|
|
||||||
"Input required and not supplied: path"
|
"Input required and not supplied: path"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -60,120 +73,99 @@ test("restore with no path should fail", async () => {
|
|||||||
test("restore with no key", async () => {
|
test("restore with no key", async () => {
|
||||||
testUtils.setInput(Inputs.Path, "node_modules");
|
testUtils.setInput(Inputs.Path, "node_modules");
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const restoreCacheMock = jest.spyOn(cache, "restoreCache");
|
|
||||||
await run();
|
await run();
|
||||||
expect(restoreCacheMock).toHaveBeenCalledTimes(0);
|
|
||||||
expect(failedMock).toHaveBeenCalledWith(
|
expect(failedMock).toHaveBeenCalledWith(
|
||||||
"Input required and not supplied: key"
|
"Input required and not supplied: key"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("restore with too many keys should fail", async () => {
|
test("restore with too many keys should fail", async () => {
|
||||||
const path = "node_modules";
|
|
||||||
const key = "node-test";
|
const key = "node-test";
|
||||||
const restoreKeys = [...Array(20).keys()].map(x => x.toString());
|
const restoreKeys = [...Array(20).keys()].map(x => x.toString());
|
||||||
testUtils.setInputs({
|
testUtils.setInputs({
|
||||||
path: path,
|
path: "node_modules",
|
||||||
key,
|
key,
|
||||||
restoreKeys
|
restoreKeys
|
||||||
});
|
});
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const restoreCacheMock = jest.spyOn(cache, "restoreCache");
|
|
||||||
await run();
|
await run();
|
||||||
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledWith([path], key, restoreKeys);
|
|
||||||
expect(failedMock).toHaveBeenCalledWith(
|
expect(failedMock).toHaveBeenCalledWith(
|
||||||
`Key Validation Error: Keys are limited to a maximum of 10.`
|
`Key Validation Error: Keys are limited to a maximum of 10.`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("restore with large key should fail", async () => {
|
test("restore with large key should fail", async () => {
|
||||||
const path = "node_modules";
|
|
||||||
const key = "foo".repeat(512); // Over the 512 character limit
|
const key = "foo".repeat(512); // Over the 512 character limit
|
||||||
testUtils.setInputs({
|
testUtils.setInputs({
|
||||||
path: path,
|
path: "node_modules",
|
||||||
key
|
key
|
||||||
});
|
});
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const restoreCacheMock = jest.spyOn(cache, "restoreCache");
|
|
||||||
await run();
|
await run();
|
||||||
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
|
|
||||||
expect(failedMock).toHaveBeenCalledWith(
|
expect(failedMock).toHaveBeenCalledWith(
|
||||||
`Key Validation Error: ${key} cannot be larger than 512 characters.`
|
`Key Validation Error: ${key} cannot be larger than 512 characters.`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("restore with invalid key should fail", async () => {
|
test("restore with invalid key should fail", async () => {
|
||||||
const path = "node_modules";
|
|
||||||
const key = "comma,comma";
|
const key = "comma,comma";
|
||||||
testUtils.setInputs({
|
testUtils.setInputs({
|
||||||
path: path,
|
path: "node_modules",
|
||||||
key
|
key
|
||||||
});
|
});
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const restoreCacheMock = jest.spyOn(cache, "restoreCache");
|
|
||||||
await run();
|
await run();
|
||||||
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
|
|
||||||
expect(failedMock).toHaveBeenCalledWith(
|
expect(failedMock).toHaveBeenCalledWith(
|
||||||
`Key Validation Error: ${key} cannot contain commas.`
|
`Key Validation Error: ${key} cannot contain commas.`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("restore with no cache found", async () => {
|
test("restore with no cache found", async () => {
|
||||||
const path = "node_modules";
|
|
||||||
const key = "node-test";
|
const key = "node-test";
|
||||||
testUtils.setInputs({
|
testUtils.setInputs({
|
||||||
path: path,
|
path: "node_modules",
|
||||||
key
|
key
|
||||||
});
|
});
|
||||||
|
|
||||||
const infoMock = jest.spyOn(core, "info");
|
const infoMock = jest.spyOn(core, "info");
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const stateMock = jest.spyOn(core, "saveState");
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
const restoreCacheMock = jest
|
|
||||||
.spyOn(cache, "restoreCache")
|
const clientMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
.mockImplementationOnce(() => {
|
clientMock.mockImplementation(() => {
|
||||||
return Promise.resolve(undefined);
|
return Promise.resolve(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
|
|
||||||
|
|
||||||
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
expect(failedMock).toHaveBeenCalledTimes(0);
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
expect(infoMock).toHaveBeenCalledWith(
|
expect(infoMock).toHaveBeenCalledWith(
|
||||||
`Cache not found for input keys: ${key}`
|
`Cache not found for input keys: ${key}.`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("restore with server error should fail", async () => {
|
test("restore with server error should fail", async () => {
|
||||||
const path = "node_modules";
|
|
||||||
const key = "node-test";
|
const key = "node-test";
|
||||||
testUtils.setInputs({
|
testUtils.setInputs({
|
||||||
path: path,
|
path: "node_modules",
|
||||||
key
|
key
|
||||||
});
|
});
|
||||||
|
|
||||||
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
|
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const stateMock = jest.spyOn(core, "saveState");
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
const restoreCacheMock = jest
|
|
||||||
.spyOn(cache, "restoreCache")
|
const clientMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
.mockImplementationOnce(() => {
|
clientMock.mockImplementation(() => {
|
||||||
throw new Error("HTTP Error Occurred");
|
throw new Error("HTTP Error Occurred");
|
||||||
});
|
});
|
||||||
|
|
||||||
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
|
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
|
|
||||||
|
|
||||||
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
|
|
||||||
expect(logWarningMock).toHaveBeenCalledTimes(1);
|
expect(logWarningMock).toHaveBeenCalledTimes(1);
|
||||||
@ -186,11 +178,10 @@ test("restore with server error should fail", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("restore with restore keys and no cache found", async () => {
|
test("restore with restore keys and no cache found", async () => {
|
||||||
const path = "node_modules";
|
|
||||||
const key = "node-test";
|
const key = "node-test";
|
||||||
const restoreKey = "node-";
|
const restoreKey = "node-";
|
||||||
testUtils.setInputs({
|
testUtils.setInputs({
|
||||||
path: path,
|
path: "node_modules",
|
||||||
key,
|
key,
|
||||||
restoreKeys: [restoreKey]
|
restoreKeys: [restoreKey]
|
||||||
});
|
});
|
||||||
@ -198,49 +189,170 @@ test("restore with restore keys and no cache found", async () => {
|
|||||||
const infoMock = jest.spyOn(core, "info");
|
const infoMock = jest.spyOn(core, "info");
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const stateMock = jest.spyOn(core, "saveState");
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
const restoreCacheMock = jest
|
|
||||||
.spyOn(cache, "restoreCache")
|
const clientMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
.mockImplementationOnce(() => {
|
clientMock.mockImplementation(() => {
|
||||||
return Promise.resolve(undefined);
|
return Promise.resolve(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [restoreKey]);
|
|
||||||
|
|
||||||
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
expect(failedMock).toHaveBeenCalledTimes(0);
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
expect(infoMock).toHaveBeenCalledWith(
|
expect(infoMock).toHaveBeenCalledWith(
|
||||||
`Cache not found for input keys: ${key}, ${restoreKey}`
|
`Cache not found for input keys: ${key}, ${restoreKey}.`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("restore with cache found for key", async () => {
|
test("restore with cache found", async () => {
|
||||||
const path = "node_modules";
|
|
||||||
const key = "node-test";
|
const key = "node-test";
|
||||||
|
const cachePath = path.resolve("node_modules");
|
||||||
testUtils.setInputs({
|
testUtils.setInputs({
|
||||||
path: path,
|
path: "node_modules",
|
||||||
key
|
key
|
||||||
});
|
});
|
||||||
|
|
||||||
const infoMock = jest.spyOn(core, "info");
|
const infoMock = jest.spyOn(core, "info");
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const stateMock = jest.spyOn(core, "saveState");
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
|
|
||||||
const restoreCacheMock = jest
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
.spyOn(cache, "restoreCache")
|
cacheKey: key,
|
||||||
.mockImplementationOnce(() => {
|
scope: "refs/heads/master",
|
||||||
return Promise.resolve(key);
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
const getCacheMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
|
getCacheMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(cacheEntry);
|
||||||
});
|
});
|
||||||
|
const tempPath = "/foo/bar";
|
||||||
|
|
||||||
|
const createTempDirectoryMock = jest.spyOn(
|
||||||
|
actionUtils,
|
||||||
|
"createTempDirectory"
|
||||||
|
);
|
||||||
|
createTempDirectoryMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(tempPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
const archivePath = path.join(tempPath, "cache.tgz");
|
||||||
|
const setCacheStateMock = jest.spyOn(actionUtils, "setCacheState");
|
||||||
|
const downloadCacheMock = jest.spyOn(cacheHttpClient, "downloadCache");
|
||||||
|
|
||||||
|
const fileSize = 142;
|
||||||
|
const getArchiveFileSizeMock = jest
|
||||||
|
.spyOn(actionUtils, "getArchiveFileSize")
|
||||||
|
.mockReturnValue(fileSize);
|
||||||
|
|
||||||
|
const mkdirMock = jest.spyOn(io, "mkdirP");
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
|
expect(getCacheMock).toHaveBeenCalledWith([key]);
|
||||||
|
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
||||||
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(downloadCacheMock).toHaveBeenCalledWith(cacheEntry, archivePath);
|
||||||
|
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
||||||
|
expect(mkdirMock).toHaveBeenCalledWith(cachePath);
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-xz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/")
|
||||||
|
]
|
||||||
|
: ["-xz", "-f", archivePath, "-C", cachePath];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
|
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(setCacheHitOutputMock).toHaveBeenCalledWith(true);
|
||||||
|
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`);
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with a pull request event and cache found", async () => {
|
||||||
|
const key = "node-test";
|
||||||
|
const cachePath = path.resolve("node_modules");
|
||||||
|
testUtils.setInputs({
|
||||||
|
path: "node_modules",
|
||||||
|
key
|
||||||
|
});
|
||||||
|
|
||||||
|
process.env[Events.Key] = Events.PullRequest;
|
||||||
|
|
||||||
|
const infoMock = jest.spyOn(core, "info");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: key,
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
const getCacheMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
|
getCacheMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(cacheEntry);
|
||||||
|
});
|
||||||
|
const tempPath = "/foo/bar";
|
||||||
|
|
||||||
|
const createTempDirectoryMock = jest.spyOn(
|
||||||
|
actionUtils,
|
||||||
|
"createTempDirectory"
|
||||||
|
);
|
||||||
|
createTempDirectoryMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(tempPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
const archivePath = path.join(tempPath, "cache.tgz");
|
||||||
|
const setCacheStateMock = jest.spyOn(actionUtils, "setCacheState");
|
||||||
|
const downloadCacheMock = jest.spyOn(cacheHttpClient, "downloadCache");
|
||||||
|
|
||||||
|
const fileSize = 62915000;
|
||||||
|
const getArchiveFileSizeMock = jest
|
||||||
|
.spyOn(actionUtils, "getArchiveFileSize")
|
||||||
|
.mockReturnValue(fileSize);
|
||||||
|
|
||||||
|
const mkdirMock = jest.spyOn(io, "mkdirP");
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
|
expect(getCacheMock).toHaveBeenCalledWith([key]);
|
||||||
|
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
||||||
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(downloadCacheMock).toHaveBeenCalledWith(cacheEntry, archivePath);
|
||||||
|
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`);
|
||||||
|
expect(mkdirMock).toHaveBeenCalledWith(cachePath);
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-xz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/")
|
||||||
|
]
|
||||||
|
: ["-xz", "-f", archivePath, "-C", cachePath];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
||||||
expect(setCacheHitOutputMock).toHaveBeenCalledWith(true);
|
expect(setCacheHitOutputMock).toHaveBeenCalledWith(true);
|
||||||
|
|
||||||
@ -249,11 +361,11 @@ test("restore with cache found for key", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("restore with cache found for restore key", async () => {
|
test("restore with cache found for restore key", async () => {
|
||||||
const path = "node_modules";
|
|
||||||
const key = "node-test";
|
const key = "node-test";
|
||||||
const restoreKey = "node-";
|
const restoreKey = "node-";
|
||||||
|
const cachePath = path.resolve("node_modules");
|
||||||
testUtils.setInputs({
|
testUtils.setInputs({
|
||||||
path: path,
|
path: "node_modules",
|
||||||
key,
|
key,
|
||||||
restoreKeys: [restoreKey]
|
restoreKeys: [restoreKey]
|
||||||
});
|
});
|
||||||
@ -261,19 +373,65 @@ test("restore with cache found for restore key", async () => {
|
|||||||
const infoMock = jest.spyOn(core, "info");
|
const infoMock = jest.spyOn(core, "info");
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const stateMock = jest.spyOn(core, "saveState");
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
|
|
||||||
const restoreCacheMock = jest
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
.spyOn(cache, "restoreCache")
|
cacheKey: restoreKey,
|
||||||
.mockImplementationOnce(() => {
|
scope: "refs/heads/master",
|
||||||
return Promise.resolve(restoreKey);
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
const getCacheMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
|
getCacheMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(cacheEntry);
|
||||||
});
|
});
|
||||||
|
const tempPath = "/foo/bar";
|
||||||
|
|
||||||
|
const createTempDirectoryMock = jest.spyOn(
|
||||||
|
actionUtils,
|
||||||
|
"createTempDirectory"
|
||||||
|
);
|
||||||
|
createTempDirectoryMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(tempPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
const archivePath = path.join(tempPath, "cache.tgz");
|
||||||
|
const setCacheStateMock = jest.spyOn(actionUtils, "setCacheState");
|
||||||
|
const downloadCacheMock = jest.spyOn(cacheHttpClient, "downloadCache");
|
||||||
|
|
||||||
|
const fileSize = 142;
|
||||||
|
const getArchiveFileSizeMock = jest
|
||||||
|
.spyOn(actionUtils, "getArchiveFileSize")
|
||||||
|
.mockReturnValue(fileSize);
|
||||||
|
|
||||||
|
const mkdirMock = jest.spyOn(io, "mkdirP");
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [restoreKey]);
|
|
||||||
|
|
||||||
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
|
expect(getCacheMock).toHaveBeenCalledWith([key, restoreKey]);
|
||||||
|
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
||||||
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(downloadCacheMock).toHaveBeenCalledWith(cacheEntry, archivePath);
|
||||||
|
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`);
|
||||||
|
expect(mkdirMock).toHaveBeenCalledWith(cachePath);
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-xz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/")
|
||||||
|
]
|
||||||
|
: ["-xz", "-f", archivePath, "-C", cachePath];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
||||||
expect(setCacheHitOutputMock).toHaveBeenCalledWith(false);
|
expect(setCacheHitOutputMock).toHaveBeenCalledWith(false);
|
||||||
|
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
import * as cache from "@actions/cache";
|
|
||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
|
import * as exec from "@actions/exec";
|
||||||
import { Events, Inputs, RefKey } from "../src/constants";
|
import * as io from "@actions/io";
|
||||||
|
import * as path from "path";
|
||||||
|
import * as cacheHttpClient from "../src/cacheHttpClient";
|
||||||
|
import { Events, Inputs } from "../src/constants";
|
||||||
|
import { ArtifactCacheEntry } from "../src/contracts";
|
||||||
import run from "../src/save";
|
import run from "../src/save";
|
||||||
import * as actionUtils from "../src/utils/actionUtils";
|
import * as actionUtils from "../src/utils/actionUtils";
|
||||||
import * as testUtils from "../src/utils/testUtils";
|
import * as testUtils from "../src/utils/testUtils";
|
||||||
|
|
||||||
jest.mock("@actions/core");
|
jest.mock("@actions/core");
|
||||||
jest.mock("@actions/cache");
|
jest.mock("@actions/exec");
|
||||||
|
jest.mock("@actions/io");
|
||||||
jest.mock("../src/utils/actionUtils");
|
jest.mock("../src/utils/actionUtils");
|
||||||
|
jest.mock("../src/cacheHttpClient");
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
jest.spyOn(core, "getInput").mockImplementation((name, options) => {
|
jest.spyOn(core, "getInput").mockImplementation((name, options) => {
|
||||||
@ -31,17 +36,32 @@ beforeAll(() => {
|
|||||||
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
||||||
return actualUtils.isValidEvent();
|
return actualUtils.isValidEvent();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
jest.spyOn(actionUtils, "getSupportedEvents").mockImplementation(() => {
|
||||||
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
||||||
|
return actualUtils.getSupportedEvents();
|
||||||
|
});
|
||||||
|
|
||||||
|
jest.spyOn(actionUtils, "resolvePath").mockImplementation(filePath => {
|
||||||
|
return path.resolve(filePath);
|
||||||
|
});
|
||||||
|
|
||||||
|
jest.spyOn(actionUtils, "createTempDirectory").mockImplementation(() => {
|
||||||
|
return Promise.resolve("/foo/bar");
|
||||||
|
});
|
||||||
|
|
||||||
|
jest.spyOn(io, "which").mockImplementation(tool => {
|
||||||
|
return Promise.resolve(tool);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env[Events.Key] = Events.Push;
|
process.env[Events.Key] = Events.Push;
|
||||||
process.env[RefKey] = "refs/heads/feature-branch";
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
testUtils.clearInputs();
|
testUtils.clearInputs();
|
||||||
delete process.env[Events.Key];
|
delete process.env[Events.Key];
|
||||||
delete process.env[RefKey];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("save with invalid event outputs warning", async () => {
|
test("save with invalid event outputs warning", async () => {
|
||||||
@ -49,10 +69,9 @@ test("save with invalid event outputs warning", async () => {
|
|||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
const invalidEvent = "commit_comment";
|
const invalidEvent = "commit_comment";
|
||||||
process.env[Events.Key] = invalidEvent;
|
process.env[Events.Key] = invalidEvent;
|
||||||
delete process.env[RefKey];
|
|
||||||
await run();
|
await run();
|
||||||
expect(logWarningMock).toHaveBeenCalledWith(
|
expect(logWarningMock).toHaveBeenCalledWith(
|
||||||
`Event Validation Error: The event type ${invalidEvent} is not supported because it's not tied to a branch or tag ref.`
|
`Event Validation Error: The event type ${invalidEvent} is not supported. Only push, pull_request events are supported at this time.`
|
||||||
);
|
);
|
||||||
expect(failedMock).toHaveBeenCalledTimes(0);
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
});
|
});
|
||||||
@ -61,21 +80,25 @@ test("save with no primary key in state outputs warning", async () => {
|
|||||||
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
|
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
const savedCacheKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
jest.spyOn(core, "getState")
|
jest.spyOn(core, "getState")
|
||||||
// Cache Entry State
|
// Cache Entry State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
return savedCacheKey;
|
return JSON.stringify(cacheEntry);
|
||||||
})
|
})
|
||||||
// Cache Key State
|
// Cache Key State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
return "";
|
return "";
|
||||||
});
|
});
|
||||||
const saveCacheMock = jest.spyOn(cache, "saveCache");
|
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
expect(saveCacheMock).toHaveBeenCalledTimes(0);
|
|
||||||
expect(logWarningMock).toHaveBeenCalledWith(
|
expect(logWarningMock).toHaveBeenCalledWith(
|
||||||
`Error retrieving key from state.`
|
`Error retrieving key from state.`
|
||||||
);
|
);
|
||||||
@ -88,25 +111,33 @@ test("save with exact match returns early", async () => {
|
|||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||||
const savedCacheKey = primaryKey;
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: primaryKey,
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
jest.spyOn(core, "getState")
|
jest.spyOn(core, "getState")
|
||||||
// Cache Entry State
|
// Cache Entry State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
return savedCacheKey;
|
return JSON.stringify(cacheEntry);
|
||||||
})
|
})
|
||||||
// Cache Key State
|
// Cache Key State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
return primaryKey;
|
return primaryKey;
|
||||||
});
|
});
|
||||||
const saveCacheMock = jest.spyOn(cache, "saveCache");
|
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
expect(saveCacheMock).toHaveBeenCalledTimes(0);
|
|
||||||
expect(infoMock).toHaveBeenCalledWith(
|
expect(infoMock).toHaveBeenCalledWith(
|
||||||
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
|
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
expect(failedMock).toHaveBeenCalledTimes(0);
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -115,22 +146,25 @@ test("save with missing input outputs warning", async () => {
|
|||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||||
const savedCacheKey = "Linux-node-";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
jest.spyOn(core, "getState")
|
jest.spyOn(core, "getState")
|
||||||
// Cache Entry State
|
// Cache Entry State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
return savedCacheKey;
|
return JSON.stringify(cacheEntry);
|
||||||
})
|
})
|
||||||
// Cache Key State
|
// Cache Key State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
return primaryKey;
|
return primaryKey;
|
||||||
});
|
});
|
||||||
const saveCacheMock = jest.spyOn(cache, "saveCache");
|
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
expect(saveCacheMock).toHaveBeenCalledTimes(0);
|
|
||||||
expect(logWarningMock).toHaveBeenCalledWith(
|
expect(logWarningMock).toHaveBeenCalledWith(
|
||||||
"Input required and not supplied: path"
|
"Input required and not supplied: path"
|
||||||
);
|
);
|
||||||
@ -143,12 +177,17 @@ test("save with large cache outputs warning", async () => {
|
|||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||||
const savedCacheKey = "Linux-node-";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
jest.spyOn(core, "getState")
|
jest.spyOn(core, "getState")
|
||||||
// Cache Entry State
|
// Cache Entry State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
return savedCacheKey;
|
return JSON.stringify(cacheEntry);
|
||||||
})
|
})
|
||||||
// Cache Key State
|
// Cache Key State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
@ -156,68 +195,41 @@ test("save with large cache outputs warning", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const inputPath = "node_modules";
|
const inputPath = "node_modules";
|
||||||
|
const cachePath = path.resolve(inputPath);
|
||||||
testUtils.setInput(Inputs.Path, inputPath);
|
testUtils.setInput(Inputs.Path, inputPath);
|
||||||
|
|
||||||
const saveCacheMock = jest
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
.spyOn(cache, "saveCache")
|
|
||||||
.mockImplementationOnce(() => {
|
const cacheSize = 1024 * 1024 * 1024; //~1GB, over the 400MB limit
|
||||||
throw new Error(
|
jest.spyOn(actionUtils, "getArchiveFileSize").mockImplementationOnce(() => {
|
||||||
"Cache size of ~6144 MB (6442450944 B) is over the 5GB limit, not saving cache."
|
return cacheSize;
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
const archivePath = path.join("/foo/bar", "cache.tgz");
|
||||||
expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey);
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-cz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/"),
|
||||||
|
"."
|
||||||
|
]
|
||||||
|
: ["-cz", "-f", archivePath, "-C", cachePath, "."];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
expect(logWarningMock).toHaveBeenCalledTimes(1);
|
expect(logWarningMock).toHaveBeenCalledTimes(1);
|
||||||
expect(logWarningMock).toHaveBeenCalledWith(
|
expect(logWarningMock).toHaveBeenCalledWith(
|
||||||
"Cache size of ~6144 MB (6442450944 B) is over the 5GB limit, not saving cache."
|
"Cache size of ~1024 MB (1073741824 B) is over the 400MB limit, not saving cache."
|
||||||
);
|
);
|
||||||
expect(failedMock).toHaveBeenCalledTimes(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("save with reserve cache failure outputs warning", async () => {
|
|
||||||
const infoMock = jest.spyOn(core, "info");
|
|
||||||
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
|
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
|
||||||
|
|
||||||
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
|
||||||
const savedCacheKey = "Linux-node-";
|
|
||||||
|
|
||||||
jest.spyOn(core, "getState")
|
|
||||||
// Cache Entry State
|
|
||||||
.mockImplementationOnce(() => {
|
|
||||||
return savedCacheKey;
|
|
||||||
})
|
|
||||||
// Cache Key State
|
|
||||||
.mockImplementationOnce(() => {
|
|
||||||
return primaryKey;
|
|
||||||
});
|
|
||||||
|
|
||||||
const inputPath = "node_modules";
|
|
||||||
testUtils.setInput(Inputs.Path, inputPath);
|
|
||||||
|
|
||||||
const saveCacheMock = jest
|
|
||||||
.spyOn(cache, "saveCache")
|
|
||||||
.mockImplementationOnce(() => {
|
|
||||||
const actualCache = jest.requireActual("@actions/cache");
|
|
||||||
const error = new actualCache.ReserveCacheError(
|
|
||||||
`Unable to reserve cache with key ${primaryKey}, another job may be creating this cache.`
|
|
||||||
);
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
|
|
||||||
await run();
|
|
||||||
|
|
||||||
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey);
|
|
||||||
|
|
||||||
expect(infoMock).toHaveBeenCalledWith(
|
|
||||||
`Unable to reserve cache with key ${primaryKey}, another job may be creating this cache.`
|
|
||||||
);
|
|
||||||
expect(logWarningMock).toHaveBeenCalledTimes(0);
|
|
||||||
expect(failedMock).toHaveBeenCalledTimes(0);
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -226,12 +238,17 @@ test("save with server error outputs warning", async () => {
|
|||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||||
const savedCacheKey = "Linux-node-";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
jest.spyOn(core, "getState")
|
jest.spyOn(core, "getState")
|
||||||
// Cache Entry State
|
// Cache Entry State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
return savedCacheKey;
|
return JSON.stringify(cacheEntry);
|
||||||
})
|
})
|
||||||
// Cache Key State
|
// Cache Key State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
@ -239,18 +256,39 @@ test("save with server error outputs warning", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const inputPath = "node_modules";
|
const inputPath = "node_modules";
|
||||||
|
const cachePath = path.resolve(inputPath);
|
||||||
testUtils.setInput(Inputs.Path, inputPath);
|
testUtils.setInput(Inputs.Path, inputPath);
|
||||||
|
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
|
||||||
const saveCacheMock = jest
|
const saveCacheMock = jest
|
||||||
.spyOn(cache, "saveCache")
|
.spyOn(cacheHttpClient, "saveCache")
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
throw new Error("HTTP Error Occurred");
|
throw new Error("HTTP Error Occurred");
|
||||||
});
|
});
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
|
const archivePath = path.join("/foo/bar", "cache.tgz");
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-cz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/"),
|
||||||
|
"."
|
||||||
|
]
|
||||||
|
: ["-cz", "-f", archivePath, "-C", cachePath, "."];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
||||||
expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey);
|
expect(saveCacheMock).toHaveBeenCalledWith(primaryKey, archivePath);
|
||||||
|
|
||||||
expect(logWarningMock).toHaveBeenCalledTimes(1);
|
expect(logWarningMock).toHaveBeenCalledTimes(1);
|
||||||
expect(logWarningMock).toHaveBeenCalledWith("HTTP Error Occurred");
|
expect(logWarningMock).toHaveBeenCalledWith("HTTP Error Occurred");
|
||||||
@ -262,12 +300,17 @@ test("save with valid inputs uploads a cache", async () => {
|
|||||||
const failedMock = jest.spyOn(core, "setFailed");
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||||
const savedCacheKey = "Linux-node-";
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
jest.spyOn(core, "getState")
|
jest.spyOn(core, "getState")
|
||||||
// Cache Entry State
|
// Cache Entry State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
return savedCacheKey;
|
return JSON.stringify(cacheEntry);
|
||||||
})
|
})
|
||||||
// Cache Key State
|
// Cache Key State
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
@ -275,19 +318,35 @@ test("save with valid inputs uploads a cache", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const inputPath = "node_modules";
|
const inputPath = "node_modules";
|
||||||
|
const cachePath = path.resolve(inputPath);
|
||||||
testUtils.setInput(Inputs.Path, inputPath);
|
testUtils.setInput(Inputs.Path, inputPath);
|
||||||
|
|
||||||
const cacheId = 4;
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
const saveCacheMock = jest
|
|
||||||
.spyOn(cache, "saveCache")
|
const saveCacheMock = jest.spyOn(cacheHttpClient, "saveCache");
|
||||||
.mockImplementationOnce(() => {
|
|
||||||
return Promise.resolve(cacheId);
|
|
||||||
});
|
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
|
const archivePath = path.join("/foo/bar", "cache.tgz");
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-cz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/"),
|
||||||
|
"."
|
||||||
|
]
|
||||||
|
: ["-cz", "-f", archivePath, "-C", cachePath, "."];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
||||||
expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey);
|
expect(saveCacheMock).toHaveBeenCalledWith(primaryKey, archivePath);
|
||||||
|
|
||||||
expect(failedMock).toHaveBeenCalledTimes(0);
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
});
|
});
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
# Validate args
|
|
||||||
prefix="$1"
|
|
||||||
if [ -z "$prefix" ]; then
|
|
||||||
echo "Must supply prefix argument"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
path="$2"
|
|
||||||
if [ -z "$path" ]; then
|
|
||||||
echo "Must specify path argument"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Sanity check GITHUB_RUN_ID defined
|
|
||||||
if [ -z "$GITHUB_RUN_ID" ]; then
|
|
||||||
echo "GITHUB_RUN_ID not defined"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Verify file exists
|
|
||||||
file="$path/test-file.txt"
|
|
||||||
echo "Checking for $file"
|
|
||||||
if [ ! -e $file ]; then
|
|
||||||
echo "File does not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Verify file content
|
|
||||||
content="$(cat $file)"
|
|
||||||
echo "File content:\n$content"
|
|
||||||
if [ -z "$(echo $content | grep --fixed-strings "$prefix $GITHUB_RUN_ID")" ]; then
|
|
||||||
echo "Unexpected file content"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
@ -1,9 +1,9 @@
|
|||||||
name: 'Cache'
|
name: 'Cache'
|
||||||
description: 'Cache artifacts like dependencies and build outputs to improve workflow execution time'
|
description: 'Cache dependencies and build outputs to improve workflow execution time'
|
||||||
author: 'GitHub'
|
author: 'GitHub'
|
||||||
inputs:
|
inputs:
|
||||||
path:
|
path:
|
||||||
description: 'A list of files, directories, and wildcard patterns to cache and restore'
|
description: 'A directory to store and save the cache'
|
||||||
required: true
|
required: true
|
||||||
key:
|
key:
|
||||||
description: 'An explicit key for restoring and saving the cache'
|
description: 'An explicit key for restoring and saving the cache'
|
||||||
|
8608
dist/restore/index.js
vendored
8608
dist/restore/index.js
vendored
File diff suppressed because it is too large
Load Diff
8465
dist/save/index.js
vendored
8465
dist/save/index.js
vendored
File diff suppressed because it is too large
Load Diff
299
examples.md
299
examples.md
@ -1,41 +1,31 @@
|
|||||||
# Examples
|
# Examples
|
||||||
|
|
||||||
- [Examples](#examples)
|
- [Examples](#examples)
|
||||||
- [C# - NuGet](#c---nuget)
|
- [C# - Nuget](#c---nuget)
|
||||||
- [D - DUB](#d---dub)
|
|
||||||
- [Elixir - Mix](#elixir---mix)
|
- [Elixir - Mix](#elixir---mix)
|
||||||
- [Go - Modules](#go---modules)
|
- [Go - Modules](#go---modules)
|
||||||
- [Haskell - Cabal](#haskell---cabal)
|
|
||||||
- [Java - Gradle](#java---gradle)
|
- [Java - Gradle](#java---gradle)
|
||||||
- [Java - Maven](#java---maven)
|
- [Java - Maven](#java---maven)
|
||||||
- [Node - npm](#node---npm)
|
- [Node - npm](#node---npm)
|
||||||
- [macOS and Ubuntu](#macos-and-ubuntu)
|
- [macOS and Ubuntu](#macos-and-ubuntu)
|
||||||
- [Windows](#windows)
|
- [Windows](#windows)
|
||||||
- [Using multiple systems and `npm config`](#using-multiple-systems-and-npm-config)
|
- [Using multiple systems and `npm config`](#using-multiple-systems-and-npm-config)
|
||||||
- [Node - Lerna](#node---lerna)
|
|
||||||
- [Node - Yarn](#node---yarn)
|
- [Node - Yarn](#node---yarn)
|
||||||
- [OCaml/Reason - esy](#ocamlreason---esy)
|
|
||||||
- [PHP - Composer](#php---composer)
|
- [PHP - Composer](#php---composer)
|
||||||
- [Python - pip](#python---pip)
|
- [Python - pip](#python---pip)
|
||||||
- [Simple example](#simple-example)
|
- [Simple example](#simple-example)
|
||||||
- [Multiple OS's in a workflow](#multiple-oss-in-a-workflow)
|
- [Multiple OS's in a workflow](#multiple-oss-in-a-workflow)
|
||||||
- [Using pip to get cache location](#using-pip-to-get-cache-location)
|
|
||||||
- [Using a script to get cache location](#using-a-script-to-get-cache-location)
|
- [Using a script to get cache location](#using-a-script-to-get-cache-location)
|
||||||
- [R - renv](#r---renv)
|
- [Ruby - Gem](#ruby---gem)
|
||||||
- [Simple example](#simple-example-1)
|
|
||||||
- [Multiple OS's in a workflow](#multiple-oss-in-a-workflow-1)
|
|
||||||
- [Ruby - Bundler](#ruby---bundler)
|
|
||||||
- [Rust - Cargo](#rust---cargo)
|
- [Rust - Cargo](#rust---cargo)
|
||||||
- [Scala - SBT](#scala---sbt)
|
|
||||||
- [Swift, Objective-C - Carthage](#swift-objective-c---carthage)
|
- [Swift, Objective-C - Carthage](#swift-objective-c---carthage)
|
||||||
- [Swift, Objective-C - CocoaPods](#swift-objective-c---cocoapods)
|
- [Swift, Objective-C - CocoaPods](#swift-objective-c---cocoapods)
|
||||||
- [Swift - Swift Package Manager](#swift---swift-package-manager)
|
|
||||||
|
|
||||||
## C# - NuGet
|
## C# - Nuget
|
||||||
Using [NuGet lock files](https://docs.microsoft.com/nuget/consume-packages/package-references-in-project-files#locking-dependencies):
|
Using [NuGet lock files](https://docs.microsoft.com/nuget/consume-packages/package-references-in-project-files#locking-dependencies):
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: ~/.nuget/packages
|
path: ~/.nuget/packages
|
||||||
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
|
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
|
||||||
@ -43,60 +33,9 @@ Using [NuGet lock files](https://docs.microsoft.com/nuget/consume-packages/packa
|
|||||||
${{ runner.os }}-nuget-
|
${{ runner.os }}-nuget-
|
||||||
```
|
```
|
||||||
|
|
||||||
Depending on the environment, huge packages might be pre-installed in the global cache folder.
|
|
||||||
With `actions/cache@v2` you can now exclude unwanted packages with [exclude pattern](https://github.com/actions/toolkit/tree/master/packages/glob#exclude-patterns)
|
|
||||||
```yaml
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
~/.nuget/packages
|
|
||||||
!~/.nuget/packages/unwanted
|
|
||||||
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-nuget-
|
|
||||||
```
|
|
||||||
|
|
||||||
Or you could move the cache folder like below.
|
|
||||||
>Note: This workflow does not work for projects that require files to be placed in user profile package folder
|
|
||||||
```yaml
|
|
||||||
env:
|
|
||||||
NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages
|
|
||||||
steps:
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: ${{ github.workspace }}/.nuget/packages
|
|
||||||
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-nuget-
|
|
||||||
```
|
|
||||||
|
|
||||||
## D - DUB
|
|
||||||
|
|
||||||
### POSIX
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: ~/.dub
|
|
||||||
key: ${{ runner.os }}-dub-${{ hashFiles('**/dub.json') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-dub-
|
|
||||||
```
|
|
||||||
|
|
||||||
### Windows
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: ~\AppData\Local\dub
|
|
||||||
key: ${{ runner.os }}-dub-${{ hashFiles('**/dub.json') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-dub-
|
|
||||||
```
|
|
||||||
|
|
||||||
## Elixir - Mix
|
## Elixir - Mix
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: deps
|
path: deps
|
||||||
key: ${{ runner.os }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
|
key: ${{ runner.os }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
|
||||||
@ -107,7 +46,7 @@ steps:
|
|||||||
## Go - Modules
|
## Go - Modules
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@ -115,28 +54,13 @@ steps:
|
|||||||
${{ runner.os }}-go-
|
${{ runner.os }}-go-
|
||||||
```
|
```
|
||||||
|
|
||||||
## Haskell - Cabal
|
|
||||||
|
|
||||||
We cache the elements of the Cabal store separately, as the entirety of `~/.cabal` can grow very large for projects with many dependencies.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
name: Cache ~/.cabal/packages, ~/.cabal/store and dist-newstyle
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
~/.cabal/packages
|
|
||||||
~/.cabal/store
|
|
||||||
dist-newstyle
|
|
||||||
key: ${{ runner.os }}-${{ matrix.ghc }}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Java - Gradle
|
## Java - Gradle
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: ~/.gradle/caches
|
path: ~/.gradle/caches
|
||||||
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
|
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-gradle-
|
${{ runner.os }}-gradle-
|
||||||
```
|
```
|
||||||
@ -144,7 +68,7 @@ We cache the elements of the Cabal store separately, as the entirety of `~/.caba
|
|||||||
## Java - Maven
|
## Java - Maven
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: ~/.m2/repository
|
path: ~/.m2/repository
|
||||||
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
|
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
|
||||||
@ -161,7 +85,7 @@ For npm, cache files are stored in `~/.npm` on Posix, or `%AppData%/npm-cache` o
|
|||||||
### macOS and Ubuntu
|
### macOS and Ubuntu
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: ~/.npm
|
path: ~/.npm
|
||||||
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
|
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
|
||||||
@ -172,14 +96,10 @@ For npm, cache files are stored in `~/.npm` on Posix, or `%AppData%/npm-cache` o
|
|||||||
### Windows
|
### Windows
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- name: Get npm cache directory
|
- uses: actions/cache@v1
|
||||||
id: npm-cache
|
|
||||||
run: |
|
|
||||||
echo "::set-output name=dir::$(npm config get cache)"
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
with:
|
with:
|
||||||
path: ${{ steps.npm-cache.outputs.dir }}
|
path: ~\AppData\Roaming\npm-cache
|
||||||
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
|
key: ${{ runner.os }}-node-${{ hashFiles('**\package-lock.json') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-node-
|
${{ runner.os }}-node-
|
||||||
```
|
```
|
||||||
@ -191,7 +111,7 @@ For npm, cache files are stored in `~/.npm` on Posix, or `%AppData%/npm-cache` o
|
|||||||
id: npm-cache
|
id: npm-cache
|
||||||
run: |
|
run: |
|
||||||
echo "::set-output name=dir::$(npm config get cache)"
|
echo "::set-output name=dir::$(npm config get cache)"
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: ${{ steps.npm-cache.outputs.dir }}
|
path: ${{ steps.npm-cache.outputs.dir }}
|
||||||
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
|
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
|
||||||
@ -199,63 +119,22 @@ For npm, cache files are stored in `~/.npm` on Posix, or `%AppData%/npm-cache` o
|
|||||||
${{ runner.os }}-node-
|
${{ runner.os }}-node-
|
||||||
```
|
```
|
||||||
|
|
||||||
## Node - Lerna
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- name: restore lerna
|
|
||||||
uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
node_modules
|
|
||||||
*/*/node_modules
|
|
||||||
key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Node - Yarn
|
## Node - Yarn
|
||||||
The yarn cache directory will depend on your operating system and version of `yarn`. See https://yarnpkg.com/lang/en/docs/cli/cache/ for more info.
|
The yarn cache directory will depend on your operating system and version of `yarn`. See https://yarnpkg.com/lang/en/docs/cli/cache/ for more info.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- name: Get yarn cache directory path
|
- name: Get yarn cache
|
||||||
id: yarn-cache-dir-path
|
id: yarn-cache
|
||||||
run: echo "::set-output name=dir::$(yarn cache dir)"
|
run: echo "::set-output name=dir::$(yarn cache dir)"
|
||||||
|
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
|
|
||||||
with:
|
with:
|
||||||
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
|
path: ${{ steps.yarn-cache.outputs.dir }}
|
||||||
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
|
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-yarn-
|
${{ runner.os }}-yarn-
|
||||||
```
|
```
|
||||||
|
|
||||||
## OCaml/Reason - esy
|
|
||||||
Esy allows you to export built dependencies and import pre-built dependencies.
|
|
||||||
```yaml
|
|
||||||
- name: Restore Cache
|
|
||||||
id: restore-cache
|
|
||||||
uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: _export
|
|
||||||
key: ${{ runner.os }}-esy-${{ hashFiles('esy.lock/index.json') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-esy-
|
|
||||||
- name: Esy install
|
|
||||||
run: 'esy install'
|
|
||||||
- name: Import Cache
|
|
||||||
run: |
|
|
||||||
esy import-dependencies _export
|
|
||||||
rm -rf _export
|
|
||||||
|
|
||||||
...(Build job)...
|
|
||||||
|
|
||||||
# Re-export dependencies if anything has changed or if it is the first time
|
|
||||||
- name: Setting dependency cache
|
|
||||||
run: |
|
|
||||||
esy export-dependencies
|
|
||||||
if: steps.restore-cache.outputs.cache-hit != 'true'
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
## PHP - Composer
|
## PHP - Composer
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
@ -263,7 +142,7 @@ Esy allows you to export built dependencies and import pre-built dependencies.
|
|||||||
id: composer-cache
|
id: composer-cache
|
||||||
run: |
|
run: |
|
||||||
echo "::set-output name=dir::$(composer config cache-files-dir)"
|
echo "::set-output name=dir::$(composer config cache-files-dir)"
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: ${{ steps.composer-cache.outputs.dir }}
|
path: ${{ steps.composer-cache.outputs.dir }}
|
||||||
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
|
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
|
||||||
@ -282,7 +161,7 @@ Locations:
|
|||||||
|
|
||||||
### Simple example
|
### Simple example
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: ~/.cache/pip
|
path: ~/.cache/pip
|
||||||
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
|
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
|
||||||
@ -295,7 +174,7 @@ Replace `~/.cache/pip` with the correct `path` if not using Ubuntu.
|
|||||||
### Multiple OS's in a workflow
|
### Multiple OS's in a workflow
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
if: startsWith(runner.os, 'Linux')
|
if: startsWith(runner.os, 'Linux')
|
||||||
with:
|
with:
|
||||||
path: ~/.cache/pip
|
path: ~/.cache/pip
|
||||||
@ -303,7 +182,7 @@ Replace `~/.cache/pip` with the correct `path` if not using Ubuntu.
|
|||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-pip-
|
${{ runner.os }}-pip-
|
||||||
|
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
if: startsWith(runner.os, 'macOS')
|
if: startsWith(runner.os, 'macOS')
|
||||||
with:
|
with:
|
||||||
path: ~/Library/Caches/pip
|
path: ~/Library/Caches/pip
|
||||||
@ -311,7 +190,7 @@ Replace `~/.cache/pip` with the correct `path` if not using Ubuntu.
|
|||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-pip-
|
${{ runner.os }}-pip-
|
||||||
|
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
if: startsWith(runner.os, 'Windows')
|
if: startsWith(runner.os, 'Windows')
|
||||||
with:
|
with:
|
||||||
path: ~\AppData\Local\pip\Cache
|
path: ~\AppData\Local\pip\Cache
|
||||||
@ -320,34 +199,16 @@ Replace `~/.cache/pip` with the correct `path` if not using Ubuntu.
|
|||||||
${{ runner.os }}-pip-
|
${{ runner.os }}-pip-
|
||||||
```
|
```
|
||||||
|
|
||||||
### Using pip to get cache location
|
|
||||||
|
|
||||||
> Note: This requires pip 20.1+
|
|
||||||
```yaml
|
|
||||||
- name: Get pip cache dir
|
|
||||||
id: pip-cache
|
|
||||||
run: |
|
|
||||||
echo "::set-output name=dir::$(pip cache dir)"
|
|
||||||
|
|
||||||
- name: pip cache
|
|
||||||
uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: ${{ steps.pip-cache.outputs.dir }}
|
|
||||||
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-pip-
|
|
||||||
```
|
|
||||||
|
|
||||||
### Using a script to get cache location
|
### Using a script to get cache location
|
||||||
|
|
||||||
> Note: This uses an internal pip API and may not always work
|
> Note: This uses an internal pip API and may not always work
|
||||||
```yaml
|
```yaml
|
||||||
- name: Get pip cache dir
|
- name: Get pip cache
|
||||||
id: pip-cache
|
id: pip-cache
|
||||||
run: |
|
run: |
|
||||||
python -c "from pip._internal.locations import USER_CACHE_DIR; print('::set-output name=dir::' + USER_CACHE_DIR)"
|
python -c "from pip._internal.locations import USER_CACHE_DIR; print('::set-output name=dir::' + USER_CACHE_DIR)"
|
||||||
|
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: ${{ steps.pip-cache.outputs.dir }}
|
path: ${{ steps.pip-cache.outputs.dir }}
|
||||||
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
|
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
|
||||||
@ -355,102 +216,41 @@ Replace `~/.cache/pip` with the correct `path` if not using Ubuntu.
|
|||||||
${{ runner.os }}-pip-
|
${{ runner.os }}-pip-
|
||||||
```
|
```
|
||||||
|
|
||||||
## R - renv
|
## Ruby - Gem
|
||||||
|
|
||||||
For renv, the cache directory will vary by OS. Look at https://rstudio.github.io/renv/articles/renv.html#cache
|
|
||||||
|
|
||||||
Locations:
|
|
||||||
- Ubuntu: `~/.local/share/renv`
|
|
||||||
- macOS: `~/Library/Application Support/renv`
|
|
||||||
- Windows: `%LOCALAPPDATA%/renv`
|
|
||||||
|
|
||||||
### Simple example
|
|
||||||
```yaml
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: ~/.local/share/renv
|
|
||||||
key: ${{ runner.os }}-renv-${{ hashFiles('**/renv.lock') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-renv-
|
|
||||||
```
|
|
||||||
|
|
||||||
Replace `~/.local/share/renv` with the correct `path` if not using Ubuntu.
|
|
||||||
|
|
||||||
### Multiple OS's in a workflow
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
if: startsWith(runner.os, 'Linux')
|
|
||||||
with:
|
|
||||||
path: ~/.local/share/renv
|
|
||||||
key: ${{ runner.os }}-renv-${{ hashFiles('**/renv.lock') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-renv-
|
|
||||||
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
if: startsWith(runner.os, 'macOS')
|
|
||||||
with:
|
|
||||||
path: ~/Library/Application Support/renv
|
|
||||||
key: ${{ runner.os }}-renv-${{ hashFiles('**/renv.lock') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-renv-
|
|
||||||
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
if: startsWith(runner.os, 'Windows')
|
|
||||||
with:
|
|
||||||
path: ~\AppData\Local\renv
|
|
||||||
key: ${{ runner.os }}-renv-${{ hashFiles('**/renv.lock') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-renv-
|
|
||||||
```
|
|
||||||
|
|
||||||
## Ruby - Bundler
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
with:
|
with:
|
||||||
path: vendor/bundle
|
path: vendor/bundle
|
||||||
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
|
key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-gems-
|
${{ runner.os }}-gem-
|
||||||
```
|
|
||||||
When dependencies are installed later in the workflow, we must specify the same path for the bundler.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- name: Bundle install
|
|
||||||
run: |
|
|
||||||
bundle config path vendor/bundle
|
|
||||||
bundle install --jobs 4 --retry 3
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Rust - Cargo
|
## Rust - Cargo
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- name: Cache cargo registry
|
||||||
|
uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: |
|
path: ~/.cargo/registry
|
||||||
~/.cargo/registry
|
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
|
||||||
~/.cargo/git
|
- name: Cache cargo index
|
||||||
target
|
uses: actions/cache@v1
|
||||||
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Scala - SBT
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- name: Cache SBT
|
|
||||||
uses: actions/cache@v2
|
|
||||||
with:
|
with:
|
||||||
path: |
|
path: ~/.cargo/git
|
||||||
~/.ivy2/cache
|
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
|
||||||
~/.sbt
|
- name: Cache cargo build
|
||||||
key: ${{ runner.os }}-sbt-${{ hashFiles('**/build.sbt') }}
|
uses: actions/cache@v1
|
||||||
|
with:
|
||||||
|
path: target
|
||||||
|
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Swift, Objective-C - Carthage
|
## Swift, Objective-C - Carthage
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: Carthage
|
path: Carthage
|
||||||
key: ${{ runner.os }}-carthage-${{ hashFiles('**/Cartfile.resolved') }}
|
key: ${{ runner.os }}-carthage-${{ hashFiles('**/Cartfile.resolved') }}
|
||||||
@ -461,21 +261,10 @@ When dependencies are installed later in the workflow, we must specify the same
|
|||||||
## Swift, Objective-C - CocoaPods
|
## Swift, Objective-C - CocoaPods
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: Pods
|
path: Pods
|
||||||
key: ${{ runner.os }}-pods-${{ hashFiles('**/Podfile.lock') }}
|
key: ${{ runner.os }}-pods-${{ hashFiles('**/Podfile.lock') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-pods-
|
${{ runner.os }}-pods-
|
||||||
```
|
```
|
||||||
|
|
||||||
## Swift - Swift Package Manager
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: .build
|
|
||||||
key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-spm-
|
|
||||||
```
|
|
||||||
|
3920
package-lock.json
generated
3920
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
16
package.json
16
package.json
@ -1,15 +1,16 @@
|
|||||||
{
|
{
|
||||||
"name": "cache",
|
"name": "cache",
|
||||||
"version": "1.1.2",
|
"version": "1.0.3",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "Cache dependencies and build outputs",
|
"description": "Cache dependencies and build outputs",
|
||||||
"main": "dist/restore/index.js",
|
"main": "dist/restore/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc && ncc build -o dist/restore src/restore.ts && ncc build -o dist/save src/save.ts",
|
"build": "tsc",
|
||||||
"test": "tsc --noEmit && jest --coverage",
|
"test": "tsc --noEmit && jest --coverage",
|
||||||
"lint": "eslint **/*.ts --cache",
|
"lint": "eslint **/*.ts --cache",
|
||||||
"format": "prettier --write **/*.ts",
|
"format": "prettier --write **/*.ts",
|
||||||
"format-check": "prettier --check **/*.ts"
|
"format-check": "prettier --check **/*.ts",
|
||||||
|
"release": "ncc build -o dist/restore src/restore.ts && ncc build -o dist/save src/save.ts && git add -f dist/"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -26,12 +27,14 @@
|
|||||||
"@actions/core": "^1.2.0",
|
"@actions/core": "^1.2.0",
|
||||||
"@actions/exec": "^1.0.1",
|
"@actions/exec": "^1.0.1",
|
||||||
"@actions/io": "^1.0.1",
|
"@actions/io": "^1.0.1",
|
||||||
"@actions/cache": "^0.2.1"
|
"typed-rest-client": "^1.5.0",
|
||||||
|
"uuid": "^3.3.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^24.0.13",
|
"@types/jest": "^24.0.13",
|
||||||
"@types/nock": "^11.1.0",
|
"@types/nock": "^11.1.0",
|
||||||
"@types/node": "^12.0.4",
|
"@types/node": "^12.0.4",
|
||||||
|
"@types/uuid": "^3.4.5",
|
||||||
"@typescript-eslint/eslint-plugin": "^2.7.0",
|
"@typescript-eslint/eslint-plugin": "^2.7.0",
|
||||||
"@typescript-eslint/parser": "^2.7.0",
|
"@typescript-eslint/parser": "^2.7.0",
|
||||||
"@zeit/ncc": "^0.20.5",
|
"@zeit/ncc": "^0.20.5",
|
||||||
@ -40,12 +43,11 @@
|
|||||||
"eslint-plugin-import": "^2.18.2",
|
"eslint-plugin-import": "^2.18.2",
|
||||||
"eslint-plugin-jest": "^23.0.3",
|
"eslint-plugin-jest": "^23.0.3",
|
||||||
"eslint-plugin-prettier": "^3.1.1",
|
"eslint-plugin-prettier": "^3.1.1",
|
||||||
"eslint-plugin-simple-import-sort": "^5.0.2",
|
|
||||||
"jest": "^24.8.0",
|
"jest": "^24.8.0",
|
||||||
"jest-circus": "^24.7.1",
|
"jest-circus": "^24.7.1",
|
||||||
"nock": "^11.7.0",
|
"nock": "^11.7.0",
|
||||||
"prettier": "^1.19.1",
|
"prettier": "1.18.2",
|
||||||
"ts-jest": "^24.0.2",
|
"ts-jest": "^24.0.2",
|
||||||
"typescript": "^3.7.3"
|
"typescript": "^3.6.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
129
src/cacheHttpClient.ts
Normal file
129
src/cacheHttpClient.ts
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
import * as core from "@actions/core";
|
||||||
|
import * as fs from "fs";
|
||||||
|
import { BearerCredentialHandler } from "typed-rest-client/Handlers";
|
||||||
|
import { HttpClient } from "typed-rest-client/HttpClient";
|
||||||
|
import { IHttpClientResponse } from "typed-rest-client/Interfaces";
|
||||||
|
import { IRequestOptions, RestClient } from "typed-rest-client/RestClient";
|
||||||
|
import { ArtifactCacheEntry } from "./contracts";
|
||||||
|
|
||||||
|
function getCacheUrl(): string {
|
||||||
|
// Ideally we just use ACTIONS_CACHE_URL
|
||||||
|
const cacheUrl: string = (
|
||||||
|
process.env["ACTIONS_CACHE_URL"] ||
|
||||||
|
process.env["ACTIONS_RUNTIME_URL"] ||
|
||||||
|
""
|
||||||
|
).replace("pipelines", "artifactcache");
|
||||||
|
if (!cacheUrl) {
|
||||||
|
throw new Error(
|
||||||
|
"Cache Service Url not found, unable to restore cache."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
core.debug(`Cache Url: ${cacheUrl}`);
|
||||||
|
return cacheUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createAcceptHeader(type: string, apiVersion: string): string {
|
||||||
|
return `${type};api-version=${apiVersion}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRequestOptions(): IRequestOptions {
|
||||||
|
const requestOptions: IRequestOptions = {
|
||||||
|
acceptHeader: createAcceptHeader("application/json", "5.2-preview.1")
|
||||||
|
};
|
||||||
|
|
||||||
|
return requestOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getCacheEntry(
|
||||||
|
keys: string[]
|
||||||
|
): Promise<ArtifactCacheEntry | null> {
|
||||||
|
const cacheUrl = getCacheUrl();
|
||||||
|
const token = process.env["ACTIONS_RUNTIME_TOKEN"] || "";
|
||||||
|
const bearerCredentialHandler = new BearerCredentialHandler(token);
|
||||||
|
|
||||||
|
const resource = `_apis/artifactcache/cache?keys=${encodeURIComponent(
|
||||||
|
keys.join(",")
|
||||||
|
)}`;
|
||||||
|
|
||||||
|
const restClient = new RestClient("actions/cache", cacheUrl, [
|
||||||
|
bearerCredentialHandler
|
||||||
|
]);
|
||||||
|
|
||||||
|
const response = await restClient.get<ArtifactCacheEntry>(
|
||||||
|
resource,
|
||||||
|
getRequestOptions()
|
||||||
|
);
|
||||||
|
if (response.statusCode === 204) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (response.statusCode !== 200) {
|
||||||
|
throw new Error(`Cache service responded with ${response.statusCode}`);
|
||||||
|
}
|
||||||
|
const cacheResult = response.result;
|
||||||
|
if (!cacheResult || !cacheResult.archiveLocation) {
|
||||||
|
throw new Error("Cache not found.");
|
||||||
|
}
|
||||||
|
core.setSecret(cacheResult.archiveLocation);
|
||||||
|
core.debug(`Cache Result:`);
|
||||||
|
core.debug(JSON.stringify(cacheResult));
|
||||||
|
|
||||||
|
return cacheResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function pipeResponseToStream(
|
||||||
|
response: IHttpClientResponse,
|
||||||
|
stream: NodeJS.WritableStream
|
||||||
|
): Promise<void> {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
response.message.pipe(stream).on("close", () => {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function downloadCache(
|
||||||
|
cacheEntry: ArtifactCacheEntry,
|
||||||
|
archivePath: string
|
||||||
|
): Promise<void> {
|
||||||
|
const stream = fs.createWriteStream(archivePath);
|
||||||
|
const httpClient = new HttpClient("actions/cache");
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
|
const downloadResponse = await httpClient.get(cacheEntry.archiveLocation!);
|
||||||
|
await pipeResponseToStream(downloadResponse, stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveCache(
|
||||||
|
key: string,
|
||||||
|
archivePath: string
|
||||||
|
): Promise<void> {
|
||||||
|
const stream = fs.createReadStream(archivePath);
|
||||||
|
|
||||||
|
const cacheUrl = getCacheUrl();
|
||||||
|
const token = process.env["ACTIONS_RUNTIME_TOKEN"] || "";
|
||||||
|
const bearerCredentialHandler = new BearerCredentialHandler(token);
|
||||||
|
|
||||||
|
const resource = `_apis/artifactcache/cache/${encodeURIComponent(key)}`;
|
||||||
|
const postUrl = cacheUrl + resource;
|
||||||
|
|
||||||
|
const restClient = new RestClient("actions/cache", undefined, [
|
||||||
|
bearerCredentialHandler
|
||||||
|
]);
|
||||||
|
|
||||||
|
const requestOptions = getRequestOptions();
|
||||||
|
requestOptions.additionalHeaders = {
|
||||||
|
"Content-Type": "application/octet-stream"
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await restClient.uploadStream<void>(
|
||||||
|
"POST",
|
||||||
|
postUrl,
|
||||||
|
stream,
|
||||||
|
requestOptions
|
||||||
|
);
|
||||||
|
if (response.statusCode !== 200) {
|
||||||
|
throw new Error(`Cache service responded with ${response.statusCode}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
core.info("Cache saved successfully");
|
||||||
|
}
|
@ -9,8 +9,8 @@ export enum Outputs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export enum State {
|
export enum State {
|
||||||
CachePrimaryKey = "CACHE_KEY",
|
CacheKey = "CACHE_KEY",
|
||||||
CacheMatchedKey = "CACHE_RESULT"
|
CacheResult = "CACHE_RESULT"
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum Events {
|
export enum Events {
|
||||||
@ -18,5 +18,3 @@ export enum Events {
|
|||||||
Push = "push",
|
Push = "push",
|
||||||
PullRequest = "pull_request"
|
PullRequest = "pull_request"
|
||||||
}
|
}
|
||||||
|
|
||||||
export const RefKey = "GITHUB_REF";
|
|
||||||
|
6
src/contracts.d.ts
vendored
Normal file
6
src/contracts.d.ts
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export interface ArtifactCacheEntry {
|
||||||
|
cacheKey?: string;
|
||||||
|
scope?: string;
|
||||||
|
creationTime?: string;
|
||||||
|
archiveLocation?: string;
|
||||||
|
}
|
114
src/restore.ts
114
src/restore.ts
@ -1,6 +1,8 @@
|
|||||||
import * as cache from "@actions/cache";
|
|
||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
|
import { exec } from "@actions/exec";
|
||||||
|
import * as io from "@actions/io";
|
||||||
|
import * as path from "path";
|
||||||
|
import * as cacheHttpClient from "./cacheHttpClient";
|
||||||
import { Events, Inputs, State } from "./constants";
|
import { Events, Inputs, State } from "./constants";
|
||||||
import * as utils from "./utils/actionUtils";
|
import * as utils from "./utils/actionUtils";
|
||||||
|
|
||||||
@ -11,55 +13,115 @@ async function run(): Promise<void> {
|
|||||||
utils.logWarning(
|
utils.logWarning(
|
||||||
`Event Validation Error: The event type ${
|
`Event Validation Error: The event type ${
|
||||||
process.env[Events.Key]
|
process.env[Events.Key]
|
||||||
} is not supported because it's not tied to a branch or tag ref.`
|
} is not supported. Only ${utils
|
||||||
|
.getSupportedEvents()
|
||||||
|
.join(", ")} events are supported at this time.`
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cachePath = utils.resolvePath(
|
||||||
|
core.getInput(Inputs.Path, { required: true })
|
||||||
|
);
|
||||||
|
core.debug(`Cache Path: ${cachePath}`);
|
||||||
|
|
||||||
const primaryKey = core.getInput(Inputs.Key, { required: true });
|
const primaryKey = core.getInput(Inputs.Key, { required: true });
|
||||||
core.saveState(State.CachePrimaryKey, primaryKey);
|
core.saveState(State.CacheKey, primaryKey);
|
||||||
|
|
||||||
const restoreKeys = core
|
const restoreKeys = core
|
||||||
.getInput(Inputs.RestoreKeys)
|
.getInput(Inputs.RestoreKeys)
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.filter(x => x !== "");
|
.filter(x => x !== "");
|
||||||
|
const keys = [primaryKey, ...restoreKeys];
|
||||||
|
|
||||||
const cachePaths = core
|
core.debug("Resolved Keys:");
|
||||||
.getInput(Inputs.Path, { required: true })
|
core.debug(JSON.stringify(keys));
|
||||||
.split("\n")
|
|
||||||
.filter(x => x !== "");
|
if (keys.length > 10) {
|
||||||
|
core.setFailed(
|
||||||
|
`Key Validation Error: Keys are limited to a maximum of 10.`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (const key of keys) {
|
||||||
|
if (key.length > 512) {
|
||||||
|
core.setFailed(
|
||||||
|
`Key Validation Error: ${key} cannot be larger than 512 characters.`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const regex = /^[^,]*$/;
|
||||||
|
if (!regex.test(key)) {
|
||||||
|
core.setFailed(
|
||||||
|
`Key Validation Error: ${key} cannot contain commas.`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const cacheKey = await cache.restoreCache(
|
const cacheEntry = await cacheHttpClient.getCacheEntry(keys);
|
||||||
cachePaths,
|
if (!cacheEntry) {
|
||||||
primaryKey,
|
|
||||||
restoreKeys
|
|
||||||
);
|
|
||||||
if (!cacheKey) {
|
|
||||||
core.info(
|
core.info(
|
||||||
`Cache not found for input keys: ${[
|
`Cache not found for input keys: ${keys.join(", ")}.`
|
||||||
primaryKey,
|
|
||||||
...restoreKeys
|
|
||||||
].join(", ")}`
|
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the matched cache key
|
const archivePath = path.join(
|
||||||
utils.setCacheState(cacheKey);
|
await utils.createTempDirectory(),
|
||||||
|
"cache.tgz"
|
||||||
|
);
|
||||||
|
core.debug(`Archive Path: ${archivePath}`);
|
||||||
|
|
||||||
const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey);
|
// Store the cache result
|
||||||
|
utils.setCacheState(cacheEntry);
|
||||||
|
|
||||||
|
// Download the cache from the cache entry
|
||||||
|
await cacheHttpClient.downloadCache(cacheEntry, archivePath);
|
||||||
|
|
||||||
|
const archiveFileSize = utils.getArchiveFileSize(archivePath);
|
||||||
|
core.info(
|
||||||
|
`Cache Size: ~${Math.round(
|
||||||
|
archiveFileSize / (1024 * 1024)
|
||||||
|
)} MB (${archiveFileSize} B)`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Create directory to extract tar into
|
||||||
|
await io.mkdirP(cachePath);
|
||||||
|
|
||||||
|
// http://man7.org/linux/man-pages/man1/tar.1.html
|
||||||
|
// tar [-options] <name of the tar archive> [files or directories which to add into archive]
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-xz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/")
|
||||||
|
]
|
||||||
|
: ["-xz", "-f", archivePath, "-C", cachePath];
|
||||||
|
|
||||||
|
const tarPath = await io.which("tar", true);
|
||||||
|
core.debug(`Tar Path: ${tarPath}`);
|
||||||
|
|
||||||
|
await exec(`"${tarPath}"`, args);
|
||||||
|
|
||||||
|
const isExactKeyMatch = utils.isExactKeyMatch(
|
||||||
|
primaryKey,
|
||||||
|
cacheEntry
|
||||||
|
);
|
||||||
utils.setCacheHitOutput(isExactKeyMatch);
|
utils.setCacheHitOutput(isExactKeyMatch);
|
||||||
|
|
||||||
core.info(`Cache restored from key: ${cacheKey}`);
|
core.info(
|
||||||
|
`Cache restored from key: ${cacheEntry && cacheEntry.cacheKey}`
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.name === cache.ValidationError.name) {
|
|
||||||
throw error;
|
|
||||||
} else {
|
|
||||||
utils.logWarning(error.message);
|
utils.logWarning(error.message);
|
||||||
utils.setCacheHitOutput(false);
|
utils.setCacheHitOutput(false);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(error.message);
|
core.setFailed(error.message);
|
||||||
}
|
}
|
||||||
|
67
src/save.ts
67
src/save.ts
@ -1,6 +1,8 @@
|
|||||||
import * as cache from "@actions/cache";
|
|
||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
|
import { exec } from "@actions/exec";
|
||||||
|
import * as io from "@actions/io";
|
||||||
|
import * as path from "path";
|
||||||
|
import * as cacheHttpClient from "./cacheHttpClient";
|
||||||
import { Events, Inputs, State } from "./constants";
|
import { Events, Inputs, State } from "./constants";
|
||||||
import * as utils from "./utils/actionUtils";
|
import * as utils from "./utils/actionUtils";
|
||||||
|
|
||||||
@ -10,7 +12,9 @@ async function run(): Promise<void> {
|
|||||||
utils.logWarning(
|
utils.logWarning(
|
||||||
`Event Validation Error: The event type ${
|
`Event Validation Error: The event type ${
|
||||||
process.env[Events.Key]
|
process.env[Events.Key]
|
||||||
} is not supported because it's not tied to a branch or tag ref.`
|
} is not supported. Only ${utils
|
||||||
|
.getSupportedEvents()
|
||||||
|
.join(", ")} events are supported at this time.`
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -18,7 +22,7 @@ async function run(): Promise<void> {
|
|||||||
const state = utils.getCacheState();
|
const state = utils.getCacheState();
|
||||||
|
|
||||||
// Inputs are re-evaluted before the post action, so we want the original key used for restore
|
// Inputs are re-evaluted before the post action, so we want the original key used for restore
|
||||||
const primaryKey = core.getState(State.CachePrimaryKey);
|
const primaryKey = core.getState(State.CacheKey);
|
||||||
if (!primaryKey) {
|
if (!primaryKey) {
|
||||||
utils.logWarning(`Error retrieving key from state.`);
|
utils.logWarning(`Error retrieving key from state.`);
|
||||||
return;
|
return;
|
||||||
@ -31,22 +35,49 @@ async function run(): Promise<void> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const cachePaths = core
|
const cachePath = utils.resolvePath(
|
||||||
.getInput(Inputs.Path, { required: true })
|
core.getInput(Inputs.Path, { required: true })
|
||||||
.split("\n")
|
);
|
||||||
.filter(x => x !== "");
|
core.debug(`Cache Path: ${cachePath}`);
|
||||||
|
|
||||||
try {
|
const archivePath = path.join(
|
||||||
await cache.saveCache(cachePaths, primaryKey);
|
await utils.createTempDirectory(),
|
||||||
} catch (error) {
|
"cache.tgz"
|
||||||
if (error.name === cache.ValidationError.name) {
|
);
|
||||||
throw error;
|
core.debug(`Archive Path: ${archivePath}`);
|
||||||
} else if (error.name === cache.ReserveCacheError.name) {
|
|
||||||
core.info(error.message);
|
// http://man7.org/linux/man-pages/man1/tar.1.html
|
||||||
} else {
|
// tar [-options] <name of the tar archive> [files or directories which to add into archive]
|
||||||
utils.logWarning(error.message);
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
}
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-cz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/"),
|
||||||
|
"."
|
||||||
|
]
|
||||||
|
: ["-cz", "-f", archivePath, "-C", cachePath, "."];
|
||||||
|
|
||||||
|
const tarPath = await io.which("tar", true);
|
||||||
|
core.debug(`Tar Path: ${tarPath}`);
|
||||||
|
await exec(`"${tarPath}"`, args);
|
||||||
|
|
||||||
|
const fileSizeLimit = 400 * 1024 * 1024; // 400MB
|
||||||
|
const archiveFileSize = utils.getArchiveFileSize(archivePath);
|
||||||
|
core.debug(`File Size: ${archiveFileSize}`);
|
||||||
|
if (archiveFileSize > fileSizeLimit) {
|
||||||
|
utils.logWarning(
|
||||||
|
`Cache size of ~${Math.round(
|
||||||
|
archiveFileSize / (1024 * 1024)
|
||||||
|
)} MB (${archiveFileSize} B) is over the 400MB limit, not saving cache.`
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await cacheHttpClient.saveCache(primaryKey, archivePath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
utils.logWarning(error.message);
|
utils.logWarning(error.message);
|
||||||
}
|
}
|
||||||
|
@ -1,35 +1,77 @@
|
|||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
|
import * as io from "@actions/io";
|
||||||
|
import * as fs from "fs";
|
||||||
|
import * as os from "os";
|
||||||
|
import * as path from "path";
|
||||||
|
import * as uuidV4 from "uuid/v4";
|
||||||
|
|
||||||
import { Outputs, RefKey, State } from "../constants";
|
import { Events, Outputs, State } from "../constants";
|
||||||
|
import { ArtifactCacheEntry } from "../contracts";
|
||||||
|
|
||||||
export function isExactKeyMatch(key: string, cacheKey?: string): boolean {
|
// From https://github.com/actions/toolkit/blob/master/packages/tool-cache/src/tool-cache.ts#L23
|
||||||
|
export async function createTempDirectory(): Promise<string> {
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
|
||||||
|
let tempDirectory: string = process.env["RUNNER_TEMP"] || "";
|
||||||
|
|
||||||
|
if (!tempDirectory) {
|
||||||
|
let baseLocation: string;
|
||||||
|
if (IS_WINDOWS) {
|
||||||
|
// On Windows use the USERPROFILE env variable
|
||||||
|
baseLocation = process.env["USERPROFILE"] || "C:\\";
|
||||||
|
} else {
|
||||||
|
if (process.platform === "darwin") {
|
||||||
|
baseLocation = "/Users";
|
||||||
|
} else {
|
||||||
|
baseLocation = "/home";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tempDirectory = path.join(baseLocation, "actions", "temp");
|
||||||
|
}
|
||||||
|
const dest = path.join(tempDirectory, uuidV4.default());
|
||||||
|
await io.mkdirP(dest);
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getArchiveFileSize(path: string): number {
|
||||||
|
return fs.statSync(path).size;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isExactKeyMatch(
|
||||||
|
key: string,
|
||||||
|
cacheResult?: ArtifactCacheEntry
|
||||||
|
): boolean {
|
||||||
return !!(
|
return !!(
|
||||||
cacheKey &&
|
cacheResult &&
|
||||||
cacheKey.localeCompare(key, undefined, {
|
cacheResult.cacheKey &&
|
||||||
|
cacheResult.cacheKey.localeCompare(key, undefined, {
|
||||||
sensitivity: "accent"
|
sensitivity: "accent"
|
||||||
}) === 0
|
}) === 0
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setCacheState(state: string): void {
|
export function setCacheState(state: ArtifactCacheEntry): void {
|
||||||
core.saveState(State.CacheMatchedKey, state);
|
core.saveState(State.CacheResult, JSON.stringify(state));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setCacheHitOutput(isCacheHit: boolean): void {
|
export function setCacheHitOutput(isCacheHit: boolean): void {
|
||||||
core.setOutput(Outputs.CacheHit, isCacheHit.toString());
|
core.setOutput(Outputs.CacheHit, isCacheHit.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setOutputAndState(key: string, cacheKey?: string): void {
|
export function setOutputAndState(
|
||||||
setCacheHitOutput(isExactKeyMatch(key, cacheKey));
|
key: string,
|
||||||
// Store the matched cache key if it exists
|
cacheResult?: ArtifactCacheEntry
|
||||||
cacheKey && setCacheState(cacheKey);
|
): void {
|
||||||
|
setCacheHitOutput(isExactKeyMatch(key, cacheResult));
|
||||||
|
// Store the cache result if it exists
|
||||||
|
cacheResult && setCacheState(cacheResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCacheState(): string | undefined {
|
export function getCacheState(): ArtifactCacheEntry | undefined {
|
||||||
const cacheKey = core.getState(State.CacheMatchedKey);
|
const stateData = core.getState(State.CacheResult);
|
||||||
if (cacheKey) {
|
core.debug(`State: ${stateData}`);
|
||||||
core.debug(`Cache state/key: ${cacheKey}`);
|
if (stateData) {
|
||||||
return cacheKey;
|
return JSON.parse(stateData) as ArtifactCacheEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -40,8 +82,26 @@ export function logWarning(message: string): void {
|
|||||||
core.info(`${warningPrefix}${message}`);
|
core.info(`${warningPrefix}${message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache token authorized for all events that are tied to a ref
|
export function resolvePath(filePath: string): string {
|
||||||
|
if (filePath[0] === "~") {
|
||||||
|
const home = os.homedir();
|
||||||
|
if (!home) {
|
||||||
|
throw new Error("Unable to resolve `~` to HOME");
|
||||||
|
}
|
||||||
|
return path.join(home, filePath.slice(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
return path.resolve(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getSupportedEvents(): string[] {
|
||||||
|
return [Events.Push, Events.PullRequest];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Currently the cache token is only authorized for push and pull_request events
|
||||||
|
// All other events will fail when reading and saving the cache
|
||||||
// See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context
|
// See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context
|
||||||
export function isValidEvent(): boolean {
|
export function isValidEvent(): boolean {
|
||||||
return RefKey in process.env && Boolean(process.env[RefKey]);
|
const githubEvent = process.env[Events.Key] || "";
|
||||||
|
return getSupportedEvents().includes(githubEvent);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user