mirror of
https://github.com/actions/cache.git
synced 2025-06-27 04:41:14 +02:00
Compare commits
29 Commits
joshmgross
...
v1.1.2
Author | SHA1 | Date | |
---|---|---|---|
70655ec832 | |||
78a4b2143b | |||
4dc4b4e758 | |||
85aee6a487 | |||
fe1055e9d1 | |||
fab26f3f4f | |||
4887979af8 | |||
f9c9166ecb | |||
23e301d35c | |||
e43776276f | |||
b6d538e2aa | |||
296374f6c9 | |||
6c11532937 | |||
c33bff8d72 | |||
d1991bb4c5 | |||
60e292adf7 | |||
a505c2e7a6 | |||
c262ac0154 | |||
10a14413e7 | |||
cf4f44db70 | |||
4c4974aff1 | |||
1da52de10f | |||
b45d91cc4b | |||
cffae9552b | |||
44543250bd | |||
6491e51b66 | |||
86dff562ab | |||
0f810ad45a | |||
9d8c7b4041 |
1
.github/workflows/workflow.yml
vendored
1
.github/workflows/workflow.yml
vendored
@ -19,6 +19,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
os: [ubuntu-latest, windows-latest, macOS-latest]
|
os: [ubuntu-latest, windows-latest, macOS-latest]
|
||||||
|
fail-fast: false
|
||||||
|
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -94,3 +94,6 @@ typings/
|
|||||||
|
|
||||||
# DynamoDB Local files
|
# DynamoDB Local files
|
||||||
.dynamodb/
|
.dynamodb/
|
||||||
|
|
||||||
|
# Text editor files
|
||||||
|
.vscode/
|
||||||
|
11
README.md
11
README.md
@ -1,6 +1,6 @@
|
|||||||
# cache
|
# cache
|
||||||
|
|
||||||
This GitHub Action allows caching dependencies and build outputs to improve workflow execution time.
|
This 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>
|
||||||
|
|
||||||
@ -63,21 +63,24 @@ See [Examples](examples.md) for a list of `actions/cache` implementations for us
|
|||||||
- [C# - Nuget](./examples.md#c---nuget)
|
- [C# - Nuget](./examples.md#c---nuget)
|
||||||
- [Elixir - Mix](./examples.md#elixir---mix)
|
- [Elixir - Mix](./examples.md#elixir---mix)
|
||||||
- [Go - Modules](./examples.md#go---modules)
|
- [Go - Modules](./examples.md#go---modules)
|
||||||
|
- [Haskell - Cabal](./examples.md#haskell---cabal)
|
||||||
- [Java - Gradle](./examples.md#java---gradle)
|
- [Java - Gradle](./examples.md#java---gradle)
|
||||||
- [Java - Maven](./examples.md#java---maven)
|
- [Java - Maven](./examples.md#java---maven)
|
||||||
- [Node - npm](./examples.md#node---npm)
|
- [Node - npm](./examples.md#node---npm)
|
||||||
- [Node - Yarn](./examples.md#node---yarn)
|
- [Node - Yarn](./examples.md#node---yarn)
|
||||||
- [PHP - Composer](./examples.md#php---composer)
|
- [PHP - Composer](./examples.md#php---composer)
|
||||||
- [Python - pip](./examples.md#python---pip)
|
- [Python - pip](./examples.md#python---pip)
|
||||||
- [Ruby - Gem](./examples.md#ruby---gem)
|
- [R - renv](./examples.md#r---renv)
|
||||||
|
- [Ruby - Bundler](./examples.md#ruby---bundler)
|
||||||
- [Rust - Cargo](./examples.md#rust---cargo)
|
- [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 - Carthage](./examples.md#swift-objective-c---carthage)
|
||||||
- [Swift, Objective-C - CocoaPods](./examples.md#swift-objective-c---cocoapods)
|
- [Swift, Objective-C - CocoaPods](./examples.md#swift-objective-c---cocoapods)
|
||||||
|
- [Swift - Swift Package Manager](./examples.md#swift---swift-package-manager)
|
||||||
|
|
||||||
## Cache Limits
|
## Cache Limits
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
## Skipping steps based on cache-hit
|
## Skipping steps based on cache-hit
|
||||||
|
|
||||||
|
@ -248,7 +248,10 @@ test("restore with cache found", async () => {
|
|||||||
expect(getCacheMock).toHaveBeenCalledWith([key]);
|
expect(getCacheMock).toHaveBeenCalledWith([key]);
|
||||||
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
||||||
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
||||||
expect(downloadCacheMock).toHaveBeenCalledWith(cacheEntry, archivePath);
|
expect(downloadCacheMock).toHaveBeenCalledWith(
|
||||||
|
cacheEntry.archiveLocation,
|
||||||
|
archivePath
|
||||||
|
);
|
||||||
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
||||||
|
|
||||||
expect(extractTarMock).toHaveBeenCalledTimes(1);
|
expect(extractTarMock).toHaveBeenCalledTimes(1);
|
||||||
@ -312,7 +315,10 @@ test("restore with a pull request event and cache found", async () => {
|
|||||||
expect(getCacheMock).toHaveBeenCalledWith([key]);
|
expect(getCacheMock).toHaveBeenCalledWith([key]);
|
||||||
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
||||||
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
||||||
expect(downloadCacheMock).toHaveBeenCalledWith(cacheEntry, archivePath);
|
expect(downloadCacheMock).toHaveBeenCalledWith(
|
||||||
|
cacheEntry.archiveLocation,
|
||||||
|
archivePath
|
||||||
|
);
|
||||||
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
||||||
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`);
|
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`);
|
||||||
|
|
||||||
@ -377,7 +383,10 @@ test("restore with cache found for restore key", async () => {
|
|||||||
expect(getCacheMock).toHaveBeenCalledWith([key, restoreKey]);
|
expect(getCacheMock).toHaveBeenCalledWith([key, restoreKey]);
|
||||||
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
||||||
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
||||||
expect(downloadCacheMock).toHaveBeenCalledWith(cacheEntry, archivePath);
|
expect(downloadCacheMock).toHaveBeenCalledWith(
|
||||||
|
cacheEntry.archiveLocation,
|
||||||
|
archivePath
|
||||||
|
);
|
||||||
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
||||||
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`);
|
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`);
|
||||||
|
|
||||||
|
@ -194,7 +194,7 @@ test("save with large cache outputs warning", async () => {
|
|||||||
|
|
||||||
const createTarMock = jest.spyOn(tar, "createTar");
|
const createTarMock = jest.spyOn(tar, "createTar");
|
||||||
|
|
||||||
const cacheSize = 1024 * 1024 * 1024; //~1GB, over the 400MB limit
|
const cacheSize = 6 * 1024 * 1024 * 1024; //~6GB, over the 5GB limit
|
||||||
jest.spyOn(actionUtils, "getArchiveFileSize").mockImplementationOnce(() => {
|
jest.spyOn(actionUtils, "getArchiveFileSize").mockImplementationOnce(() => {
|
||||||
return cacheSize;
|
return cacheSize;
|
||||||
});
|
});
|
||||||
@ -208,12 +208,63 @@ test("save with large cache outputs warning", async () => {
|
|||||||
|
|
||||||
expect(logWarningMock).toHaveBeenCalledTimes(1);
|
expect(logWarningMock).toHaveBeenCalledTimes(1);
|
||||||
expect(logWarningMock).toHaveBeenCalledWith(
|
expect(logWarningMock).toHaveBeenCalledWith(
|
||||||
"Cache size of ~1024 MB (1073741824 B) is over the 400MB limit, not saving cache."
|
"Cache size of ~6144 MB (6442450944 B) is over the 5GB limit, not saving cache."
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(failedMock).toHaveBeenCalledTimes(0);
|
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 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")
|
||||||
|
// Cache Entry State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return JSON.stringify(cacheEntry);
|
||||||
|
})
|
||||||
|
// Cache Key State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return primaryKey;
|
||||||
|
});
|
||||||
|
|
||||||
|
const inputPath = "node_modules";
|
||||||
|
testUtils.setInput(Inputs.Path, inputPath);
|
||||||
|
|
||||||
|
const reserveCacheMock = jest
|
||||||
|
.spyOn(cacheHttpClient, "reserveCache")
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return Promise.resolve(-1);
|
||||||
|
});
|
||||||
|
|
||||||
|
const createTarMock = jest.spyOn(tar, "createTar");
|
||||||
|
|
||||||
|
const saveCacheMock = jest.spyOn(cacheHttpClient, "saveCache");
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(reserveCacheMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(reserveCacheMock).toHaveBeenCalledWith(primaryKey);
|
||||||
|
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(
|
||||||
|
`Unable to reserve cache with key ${primaryKey}, another job may be creating this cache.`
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(createTarMock).toHaveBeenCalledTimes(0);
|
||||||
|
expect(saveCacheMock).toHaveBeenCalledTimes(0);
|
||||||
|
expect(logWarningMock).toHaveBeenCalledTimes(0);
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
test("save with server error outputs warning", async () => {
|
test("save with server error 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");
|
||||||
@ -240,6 +291,13 @@ test("save with server error outputs warning", async () => {
|
|||||||
const cachePath = path.resolve(inputPath);
|
const cachePath = path.resolve(inputPath);
|
||||||
testUtils.setInput(Inputs.Path, inputPath);
|
testUtils.setInput(Inputs.Path, inputPath);
|
||||||
|
|
||||||
|
const cacheId = 4;
|
||||||
|
const reserveCacheMock = jest
|
||||||
|
.spyOn(cacheHttpClient, "reserveCache")
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return Promise.resolve(cacheId);
|
||||||
|
});
|
||||||
|
|
||||||
const createTarMock = jest.spyOn(tar, "createTar");
|
const createTarMock = jest.spyOn(tar, "createTar");
|
||||||
|
|
||||||
const saveCacheMock = jest
|
const saveCacheMock = jest
|
||||||
@ -250,13 +308,16 @@ test("save with server error outputs warning", async () => {
|
|||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
|
expect(reserveCacheMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(reserveCacheMock).toHaveBeenCalledWith(primaryKey);
|
||||||
|
|
||||||
const archivePath = path.join("/foo/bar", "cache.tgz");
|
const archivePath = path.join("/foo/bar", "cache.tgz");
|
||||||
|
|
||||||
expect(createTarMock).toHaveBeenCalledTimes(1);
|
expect(createTarMock).toHaveBeenCalledTimes(1);
|
||||||
expect(createTarMock).toHaveBeenCalledWith(archivePath, cachePath);
|
expect(createTarMock).toHaveBeenCalledWith(archivePath, cachePath);
|
||||||
|
|
||||||
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
||||||
expect(saveCacheMock).toHaveBeenCalledWith(primaryKey, archivePath);
|
expect(saveCacheMock).toHaveBeenCalledWith(cacheId, archivePath);
|
||||||
|
|
||||||
expect(logWarningMock).toHaveBeenCalledTimes(1);
|
expect(logWarningMock).toHaveBeenCalledTimes(1);
|
||||||
expect(logWarningMock).toHaveBeenCalledWith("HTTP Error Occurred");
|
expect(logWarningMock).toHaveBeenCalledWith("HTTP Error Occurred");
|
||||||
@ -289,18 +350,29 @@ test("save with valid inputs uploads a cache", async () => {
|
|||||||
const cachePath = path.resolve(inputPath);
|
const cachePath = path.resolve(inputPath);
|
||||||
testUtils.setInput(Inputs.Path, inputPath);
|
testUtils.setInput(Inputs.Path, inputPath);
|
||||||
|
|
||||||
|
const cacheId = 4;
|
||||||
|
const reserveCacheMock = jest
|
||||||
|
.spyOn(cacheHttpClient, "reserveCache")
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return Promise.resolve(cacheId);
|
||||||
|
});
|
||||||
|
|
||||||
const createTarMock = jest.spyOn(tar, "createTar");
|
const createTarMock = jest.spyOn(tar, "createTar");
|
||||||
|
|
||||||
const saveCacheMock = jest.spyOn(cacheHttpClient, "saveCache");
|
const saveCacheMock = jest.spyOn(cacheHttpClient, "saveCache");
|
||||||
|
|
||||||
await run();
|
await run();
|
||||||
|
|
||||||
|
expect(reserveCacheMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(reserveCacheMock).toHaveBeenCalledWith(primaryKey);
|
||||||
|
|
||||||
const archivePath = path.join("/foo/bar", "cache.tgz");
|
const archivePath = path.join("/foo/bar", "cache.tgz");
|
||||||
|
|
||||||
expect(createTarMock).toHaveBeenCalledTimes(1);
|
expect(createTarMock).toHaveBeenCalledTimes(1);
|
||||||
expect(createTarMock).toHaveBeenCalledWith(archivePath, cachePath);
|
expect(createTarMock).toHaveBeenCalledWith(archivePath, cachePath);
|
||||||
|
|
||||||
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
||||||
expect(saveCacheMock).toHaveBeenCalledWith(primaryKey, archivePath);
|
expect(saveCacheMock).toHaveBeenCalledWith(cacheId, archivePath);
|
||||||
|
|
||||||
expect(failedMock).toHaveBeenCalledTimes(0);
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name: 'Cache'
|
name: 'Cache'
|
||||||
description: 'Cache dependencies and build outputs to improve workflow execution time'
|
description: 'Cache artifacts like dependencies and build outputs to improve workflow execution time'
|
||||||
author: 'GitHub'
|
author: 'GitHub'
|
||||||
inputs:
|
inputs:
|
||||||
path:
|
path:
|
||||||
|
3091
dist/restore/index.js
vendored
Normal file
3091
dist/restore/index.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
3072
dist/save/index.js
vendored
Normal file
3072
dist/save/index.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
109
examples.md
109
examples.md
@ -3,16 +3,20 @@
|
|||||||
- [C# - NuGet](#c---nuget)
|
- [C# - NuGet](#c---nuget)
|
||||||
- [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)
|
||||||
- [Node - Yarn](#node---yarn)
|
- [Node - Yarn](#node---yarn)
|
||||||
- [PHP - Composer](#php---composer)
|
- [PHP - Composer](#php---composer)
|
||||||
- [Python - pip](#python---pip)
|
- [Python - pip](#python---pip)
|
||||||
- [Ruby - Gem](#ruby---gem)
|
- [R - renv](#r---renv)
|
||||||
|
- [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):
|
||||||
@ -62,6 +66,28 @@ 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@v1
|
||||||
|
name: Cache ~/.cabal/packages
|
||||||
|
with:
|
||||||
|
path: ~/.cabal/packages
|
||||||
|
key: ${{ runner.os }}-${{ matrix.ghc }}-cabal-packages
|
||||||
|
- uses: actions/cache@v1
|
||||||
|
name: Cache ~/.cabal/store
|
||||||
|
with:
|
||||||
|
path: ~/.cabal/store
|
||||||
|
key: ${{ runner.os }}-${{ matrix.ghc }}-cabal-store
|
||||||
|
- uses: actions/cache@v1
|
||||||
|
name: Cache dist-newstyle
|
||||||
|
with:
|
||||||
|
path: dist-newstyle
|
||||||
|
key: ${{ runner.os }}-${{ matrix.ghc }}-dist-newstyle
|
||||||
|
```
|
||||||
|
|
||||||
## Java - Gradle
|
## Java - Gradle
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
@ -224,15 +250,64 @@ Replace `~/.cache/pip` with the correct `path` if not using Ubuntu.
|
|||||||
${{ runner.os }}-pip-
|
${{ runner.os }}-pip-
|
||||||
```
|
```
|
||||||
|
|
||||||
## Ruby - Gem
|
## R - renv
|
||||||
|
|
||||||
|
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@v1
|
||||||
|
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
|
||||||
|
- 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@v1
|
||||||
|
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@v1
|
||||||
|
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
|
```yaml
|
||||||
- uses: actions/cache@v1
|
- uses: actions/cache@v1
|
||||||
with:
|
with:
|
||||||
path: vendor/bundle
|
path: vendor/bundle
|
||||||
key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
|
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-gem-
|
${{ runner.os }}-gems-
|
||||||
```
|
```
|
||||||
When dependencies are installed later in the workflow, we must specify the same path for the bundler.
|
When dependencies are installed later in the workflow, we must specify the same path for the bundler.
|
||||||
|
|
||||||
@ -263,6 +338,21 @@ When dependencies are installed later in the workflow, we must specify the same
|
|||||||
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
|
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Scala - SBT
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- name: Cache SBT ivy cache
|
||||||
|
uses: actions/cache@v1
|
||||||
|
with:
|
||||||
|
path: ~/.ivy2/cache
|
||||||
|
key: ${{ runner.os }}-sbt-ivy-cache-${{ hashFiles('**/build.sbt') }}
|
||||||
|
- name: Cache SBT
|
||||||
|
uses: actions/cache@v1
|
||||||
|
with:
|
||||||
|
path: ~/.sbt
|
||||||
|
key: ${{ runner.os }}-sbt-${{ hashFiles('**/build.sbt') }}
|
||||||
|
```
|
||||||
|
|
||||||
## Swift, Objective-C - Carthage
|
## Swift, Objective-C - Carthage
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
@ -284,3 +374,14 @@ When dependencies are installed later in the workflow, we must specify the same
|
|||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-pods-
|
${{ runner.os }}-pods-
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Swift - Swift Package Manager
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- uses: actions/cache@v1
|
||||||
|
with:
|
||||||
|
path: .build
|
||||||
|
key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-spm-
|
||||||
|
```
|
||||||
|
42
package-lock.json
generated
42
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "cache",
|
"name": "cache",
|
||||||
"version": "1.0.3",
|
"version": "1.1.2",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -14,6 +14,14 @@
|
|||||||
"resolved": "https://registry.npmjs.org/@actions/exec/-/exec-1.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/@actions/exec/-/exec-1.0.1.tgz",
|
||||||
"integrity": "sha512-nvFkxwiicvpzNiCBF4wFBDfnBvi7xp/as7LE1hBxBxKG2L29+gkIPBiLKMVORL+Hg3JNf07AKRfl0V5djoypjQ=="
|
"integrity": "sha512-nvFkxwiicvpzNiCBF4wFBDfnBvi7xp/as7LE1hBxBxKG2L29+gkIPBiLKMVORL+Hg3JNf07AKRfl0V5djoypjQ=="
|
||||||
},
|
},
|
||||||
|
"@actions/http-client": {
|
||||||
|
"version": "1.0.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.6.tgz",
|
||||||
|
"integrity": "sha512-LGmio4w98UyGX33b/W6V6Nx/sQHRXZ859YlMkn36wPsXPB82u8xTVlA/Dq2DXrm6lEq9RVmisRJa1c+HETAIJA==",
|
||||||
|
"requires": {
|
||||||
|
"tunnel": "0.0.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"@actions/io": {
|
"@actions/io": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/@actions/io/-/io-1.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/@actions/io/-/io-1.0.1.tgz",
|
||||||
@ -2854,9 +2862,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"handlebars": {
|
"handlebars": {
|
||||||
"version": "4.5.1",
|
"version": "4.5.3",
|
||||||
"resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.5.1.tgz",
|
"resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.5.3.tgz",
|
||||||
"integrity": "sha512-C29UoFzHe9yM61lOsIlCE5/mQVGrnIOrOq7maQl76L7tYPCgC1og0Ajt6uWnX4ZTxBPnjw+CUvawphwCfJgUnA==",
|
"integrity": "sha512-3yPecJoJHK/4c6aZhSvxOyG4vJKDshV36VHp0iVCDVh7o9w2vwi3NSnL2MMPj3YdduqaBcu7cGbggJQM0br9xA==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"neo-async": "^2.6.0",
|
"neo-async": "^2.6.0",
|
||||||
@ -5933,9 +5941,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"tunnel": {
|
"tunnel": {
|
||||||
"version": "0.0.4",
|
"version": "0.0.6",
|
||||||
"resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.4.tgz",
|
"resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz",
|
||||||
"integrity": "sha1-LTeFoVjBdMmhbcLARuxfxfF0IhM="
|
"integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg=="
|
||||||
},
|
},
|
||||||
"tunnel-agent": {
|
"tunnel-agent": {
|
||||||
"version": "0.6.0",
|
"version": "0.6.0",
|
||||||
@ -5973,15 +5981,6 @@
|
|||||||
"integrity": "sha512-DWkS49EQKVX//Tbupb9TFa19c7+MK1XmzkrZUR8TAktmE/DizXoaoJV6TZ/tSIPXipqNiRI6CyAe7x69Jb6RSw==",
|
"integrity": "sha512-DWkS49EQKVX//Tbupb9TFa19c7+MK1XmzkrZUR8TAktmE/DizXoaoJV6TZ/tSIPXipqNiRI6CyAe7x69Jb6RSw==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"typed-rest-client": {
|
|
||||||
"version": "1.5.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/typed-rest-client/-/typed-rest-client-1.5.0.tgz",
|
|
||||||
"integrity": "sha512-DVZRlmsfnTjp6ZJaatcdyvvwYwbWvR4YDNFDqb+qdTxpvaVP99YCpBkA8rxsLtAPjBVoDe4fNsnMIdZTiPuKWg==",
|
|
||||||
"requires": {
|
|
||||||
"tunnel": "0.0.4",
|
|
||||||
"underscore": "1.8.3"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"typescript": {
|
"typescript": {
|
||||||
"version": "3.7.3",
|
"version": "3.7.3",
|
||||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.3.tgz",
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.3.tgz",
|
||||||
@ -5989,9 +5988,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"uglify-js": {
|
"uglify-js": {
|
||||||
"version": "3.6.7",
|
"version": "3.7.3",
|
||||||
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.7.tgz",
|
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.7.3.tgz",
|
||||||
"integrity": "sha512-4sXQDzmdnoXiO+xvmTzQsfIiwrjUCSA95rSP4SEd8tDb51W2TiDOlL76Hl+Kw0Ie42PSItCW8/t6pBNCF2R48A==",
|
"integrity": "sha512-7tINm46/3puUA4hCkKYo4Xdts+JDaVC9ZPRcG8Xw9R4nhO/gZgUM3TENq8IF4Vatk8qCig4MzP/c8G4u2BkVQg==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
@ -5999,11 +5998,6 @@
|
|||||||
"source-map": "~0.6.1"
|
"source-map": "~0.6.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"underscore": {
|
|
||||||
"version": "1.8.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz",
|
|
||||||
"integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI="
|
|
||||||
},
|
|
||||||
"union-value": {
|
"union-value": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.1.tgz",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "cache",
|
"name": "cache",
|
||||||
"version": "1.0.3",
|
"version": "1.1.2",
|
||||||
"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",
|
||||||
@ -26,8 +26,8 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.2.0",
|
"@actions/core": "^1.2.0",
|
||||||
"@actions/exec": "^1.0.1",
|
"@actions/exec": "^1.0.1",
|
||||||
|
"@actions/http-client": "^1.0.6",
|
||||||
"@actions/io": "^1.0.1",
|
"@actions/io": "^1.0.1",
|
||||||
"typed-rest-client": "^1.5.0",
|
|
||||||
"uuid": "^3.3.3"
|
"uuid": "^3.3.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -1,26 +1,55 @@
|
|||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { BearerCredentialHandler } from "typed-rest-client/Handlers";
|
import { BearerCredentialHandler } from "@actions/http-client/auth";
|
||||||
import { HttpClient } from "typed-rest-client/HttpClient";
|
import { HttpClient, HttpCodes } from "@actions/http-client";
|
||||||
import { IHttpClientResponse } from "typed-rest-client/Interfaces";
|
import {
|
||||||
import { IRequestOptions, RestClient } from "typed-rest-client/RestClient";
|
IHttpClientResponse,
|
||||||
import { ArtifactCacheEntry } from "./contracts";
|
IRequestOptions,
|
||||||
|
ITypedResponse
|
||||||
|
} from "@actions/http-client/interfaces";
|
||||||
|
import {
|
||||||
|
ArtifactCacheEntry,
|
||||||
|
CommitCacheRequest,
|
||||||
|
ReserveCacheRequest,
|
||||||
|
ReserveCacheResponse
|
||||||
|
} from "./contracts";
|
||||||
|
import * as utils from "./utils/actionUtils";
|
||||||
|
|
||||||
function getCacheUrl(): string {
|
function isSuccessStatusCode(statusCode?: number): boolean {
|
||||||
|
if (!statusCode) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return statusCode >= 200 && statusCode < 300;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isRetryableStatusCode(statusCode?: number): boolean {
|
||||||
|
if (!statusCode) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const retryableStatusCodes = [
|
||||||
|
HttpCodes.BadGateway,
|
||||||
|
HttpCodes.ServiceUnavailable,
|
||||||
|
HttpCodes.GatewayTimeout
|
||||||
|
];
|
||||||
|
return retryableStatusCodes.includes(statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCacheApiUrl(resource: string): string {
|
||||||
// Ideally we just use ACTIONS_CACHE_URL
|
// Ideally we just use ACTIONS_CACHE_URL
|
||||||
const cacheUrl: string = (
|
const baseUrl: string = (
|
||||||
process.env["ACTIONS_CACHE_URL"] ||
|
process.env["ACTIONS_CACHE_URL"] ||
|
||||||
process.env["ACTIONS_RUNTIME_URL"] ||
|
process.env["ACTIONS_RUNTIME_URL"] ||
|
||||||
""
|
""
|
||||||
).replace("pipelines", "artifactcache");
|
).replace("pipelines", "artifactcache");
|
||||||
if (!cacheUrl) {
|
if (!baseUrl) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Cache Service Url not found, unable to restore cache."
|
"Cache Service Url not found, unable to restore cache."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
core.debug(`Cache Url: ${cacheUrl}`);
|
const url = `${baseUrl}_apis/artifactcache/${resource}`;
|
||||||
return cacheUrl;
|
core.debug(`Resource Url: ${url}`);
|
||||||
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createAcceptHeader(type: string, apiVersion: string): string {
|
function createAcceptHeader(type: string, apiVersion: string): string {
|
||||||
@ -29,42 +58,47 @@ function createAcceptHeader(type: string, apiVersion: string): string {
|
|||||||
|
|
||||||
function getRequestOptions(): IRequestOptions {
|
function getRequestOptions(): IRequestOptions {
|
||||||
const requestOptions: IRequestOptions = {
|
const requestOptions: IRequestOptions = {
|
||||||
acceptHeader: createAcceptHeader("application/json", "5.2-preview.1")
|
headers: {
|
||||||
|
Accept: createAcceptHeader("application/json", "6.0-preview.1")
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return requestOptions;
|
return requestOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCacheEntry(
|
function createHttpClient(): HttpClient {
|
||||||
keys: string[]
|
|
||||||
): Promise<ArtifactCacheEntry | null> {
|
|
||||||
const cacheUrl = getCacheUrl();
|
|
||||||
const token = process.env["ACTIONS_RUNTIME_TOKEN"] || "";
|
const token = process.env["ACTIONS_RUNTIME_TOKEN"] || "";
|
||||||
const bearerCredentialHandler = new BearerCredentialHandler(token);
|
const bearerCredentialHandler = new BearerCredentialHandler(token);
|
||||||
|
|
||||||
const resource = `_apis/artifactcache/cache?keys=${encodeURIComponent(
|
return new HttpClient(
|
||||||
keys.join(",")
|
"actions/cache",
|
||||||
)}`;
|
[bearerCredentialHandler],
|
||||||
|
|
||||||
const restClient = new RestClient("actions/cache", cacheUrl, [
|
|
||||||
bearerCredentialHandler
|
|
||||||
]);
|
|
||||||
|
|
||||||
const response = await restClient.get<ArtifactCacheEntry>(
|
|
||||||
resource,
|
|
||||||
getRequestOptions()
|
getRequestOptions()
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getCacheEntry(
|
||||||
|
keys: string[]
|
||||||
|
): Promise<ArtifactCacheEntry | null> {
|
||||||
|
const httpClient = createHttpClient();
|
||||||
|
const resource = `cache?keys=${encodeURIComponent(keys.join(","))}`;
|
||||||
|
|
||||||
|
const response = await httpClient.getJson<ArtifactCacheEntry>(
|
||||||
|
getCacheApiUrl(resource)
|
||||||
|
);
|
||||||
if (response.statusCode === 204) {
|
if (response.statusCode === 204) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (response.statusCode !== 200) {
|
if (!isSuccessStatusCode(response.statusCode)) {
|
||||||
throw new Error(`Cache service responded with ${response.statusCode}`);
|
throw new Error(`Cache service responded with ${response.statusCode}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const cacheResult = response.result;
|
const cacheResult = response.result;
|
||||||
if (!cacheResult || !cacheResult.archiveLocation) {
|
const cacheDownloadUrl = cacheResult?.archiveLocation;
|
||||||
|
if (!cacheDownloadUrl) {
|
||||||
throw new Error("Cache not found.");
|
throw new Error("Cache not found.");
|
||||||
}
|
}
|
||||||
core.setSecret(cacheResult.archiveLocation);
|
core.setSecret(cacheDownloadUrl);
|
||||||
core.debug(`Cache Result:`);
|
core.debug(`Cache Result:`);
|
||||||
core.debug(JSON.stringify(cacheResult));
|
core.debug(JSON.stringify(cacheResult));
|
||||||
|
|
||||||
@ -83,46 +117,181 @@ async function pipeResponseToStream(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function downloadCache(
|
export async function downloadCache(
|
||||||
cacheEntry: ArtifactCacheEntry,
|
archiveLocation: string,
|
||||||
archivePath: string
|
archivePath: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const stream = fs.createWriteStream(archivePath);
|
const stream = fs.createWriteStream(archivePath);
|
||||||
const httpClient = new HttpClient("actions/cache");
|
const httpClient = new HttpClient("actions/cache");
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
const downloadResponse = await httpClient.get(archiveLocation);
|
||||||
const downloadResponse = await httpClient.get(cacheEntry.archiveLocation!);
|
|
||||||
await pipeResponseToStream(downloadResponse, stream);
|
await pipeResponseToStream(downloadResponse, stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function saveCache(
|
// Reserve Cache
|
||||||
key: string,
|
export async function reserveCache(key: string): Promise<number> {
|
||||||
archivePath: string
|
const httpClient = createHttpClient();
|
||||||
|
|
||||||
|
const reserveCacheRequest: ReserveCacheRequest = {
|
||||||
|
key
|
||||||
|
};
|
||||||
|
const response = await httpClient.postJson<ReserveCacheResponse>(
|
||||||
|
getCacheApiUrl("caches"),
|
||||||
|
reserveCacheRequest
|
||||||
|
);
|
||||||
|
return response?.result?.cacheId ?? -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getContentRange(start: number, end: number): string {
|
||||||
|
// Format: `bytes start-end/filesize
|
||||||
|
// start and end are inclusive
|
||||||
|
// filesize can be *
|
||||||
|
// For a 200 byte chunk starting at byte 0:
|
||||||
|
// Content-Range: bytes 0-199/*
|
||||||
|
return `bytes ${start}-${end}/*`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function uploadChunk(
|
||||||
|
httpClient: HttpClient,
|
||||||
|
resourceUrl: string,
|
||||||
|
data: NodeJS.ReadableStream,
|
||||||
|
start: number,
|
||||||
|
end: number
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const stream = fs.createReadStream(archivePath);
|
core.debug(
|
||||||
|
`Uploading chunk of size ${end -
|
||||||
const cacheUrl = getCacheUrl();
|
start +
|
||||||
const token = process.env["ACTIONS_RUNTIME_TOKEN"] || "";
|
1} bytes at offset ${start} with content range: ${getContentRange(
|
||||||
const bearerCredentialHandler = new BearerCredentialHandler(token);
|
start,
|
||||||
|
end
|
||||||
const resource = `_apis/artifactcache/cache/${encodeURIComponent(key)}`;
|
)}`
|
||||||
const postUrl = cacheUrl + resource;
|
);
|
||||||
|
const additionalHeaders = {
|
||||||
const restClient = new RestClient("actions/cache", undefined, [
|
"Content-Type": "application/octet-stream",
|
||||||
bearerCredentialHandler
|
"Content-Range": getContentRange(start, end)
|
||||||
]);
|
|
||||||
|
|
||||||
const requestOptions = getRequestOptions();
|
|
||||||
requestOptions.additionalHeaders = {
|
|
||||||
"Content-Type": "application/octet-stream"
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await restClient.uploadStream<void>(
|
const uploadChunkRequest = async (): Promise<IHttpClientResponse> => {
|
||||||
"POST",
|
return await httpClient.sendStream(
|
||||||
postUrl,
|
"PATCH",
|
||||||
stream,
|
resourceUrl,
|
||||||
requestOptions
|
data,
|
||||||
|
additionalHeaders
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await uploadChunkRequest();
|
||||||
|
if (isSuccessStatusCode(response.message.statusCode)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isRetryableStatusCode(response.message.statusCode)) {
|
||||||
|
core.debug(
|
||||||
|
`Received ${response.message.statusCode}, retrying chunk at offset ${start}.`
|
||||||
|
);
|
||||||
|
const retryResponse = await uploadChunkRequest();
|
||||||
|
if (isSuccessStatusCode(retryResponse.message.statusCode)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(
|
||||||
|
`Cache service responded with ${response.message.statusCode} during chunk upload.`
|
||||||
);
|
);
|
||||||
if (response.statusCode !== 200) {
|
}
|
||||||
throw new Error(`Cache service responded with ${response.statusCode}`);
|
|
||||||
|
function parseEnvNumber(key: string): number | undefined {
|
||||||
|
const value = Number(process.env[key]);
|
||||||
|
if (Number.isNaN(value) || value < 0) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function uploadFile(
|
||||||
|
httpClient: HttpClient,
|
||||||
|
cacheId: number,
|
||||||
|
archivePath: string
|
||||||
|
): Promise<void> {
|
||||||
|
// Upload Chunks
|
||||||
|
const fileSize = fs.statSync(archivePath).size;
|
||||||
|
const resourceUrl = getCacheApiUrl(`caches/${cacheId.toString()}`);
|
||||||
|
const fd = fs.openSync(archivePath, "r");
|
||||||
|
|
||||||
|
const concurrency = parseEnvNumber("CACHE_UPLOAD_CONCURRENCY") ?? 4; // # of HTTP requests in parallel
|
||||||
|
const MAX_CHUNK_SIZE =
|
||||||
|
parseEnvNumber("CACHE_UPLOAD_CHUNK_SIZE") ?? 32 * 1024 * 1024; // 32 MB Chunks
|
||||||
|
core.debug(`Concurrency: ${concurrency} and Chunk Size: ${MAX_CHUNK_SIZE}`);
|
||||||
|
|
||||||
|
const parallelUploads = [...new Array(concurrency).keys()];
|
||||||
|
core.debug("Awaiting all uploads");
|
||||||
|
let offset = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await Promise.all(
|
||||||
|
parallelUploads.map(async () => {
|
||||||
|
while (offset < fileSize) {
|
||||||
|
const chunkSize = Math.min(
|
||||||
|
fileSize - offset,
|
||||||
|
MAX_CHUNK_SIZE
|
||||||
|
);
|
||||||
|
const start = offset;
|
||||||
|
const end = offset + chunkSize - 1;
|
||||||
|
offset += MAX_CHUNK_SIZE;
|
||||||
|
const chunk = fs.createReadStream(archivePath, {
|
||||||
|
fd,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
autoClose: false
|
||||||
|
});
|
||||||
|
|
||||||
|
await uploadChunk(
|
||||||
|
httpClient,
|
||||||
|
resourceUrl,
|
||||||
|
chunk,
|
||||||
|
start,
|
||||||
|
end
|
||||||
|
);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
fs.closeSync(fd);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function commitCache(
|
||||||
|
httpClient: HttpClient,
|
||||||
|
cacheId: number,
|
||||||
|
filesize: number
|
||||||
|
): Promise<ITypedResponse<null>> {
|
||||||
|
const commitCacheRequest: CommitCacheRequest = { size: filesize };
|
||||||
|
return await httpClient.postJson<null>(
|
||||||
|
getCacheApiUrl(`caches/${cacheId.toString()}`),
|
||||||
|
commitCacheRequest
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveCache(
|
||||||
|
cacheId: number,
|
||||||
|
archivePath: string
|
||||||
|
): Promise<void> {
|
||||||
|
const httpClient = createHttpClient();
|
||||||
|
|
||||||
|
core.debug("Upload cache");
|
||||||
|
await uploadFile(httpClient, cacheId, archivePath);
|
||||||
|
|
||||||
|
// Commit Cache
|
||||||
|
core.debug("Commiting cache");
|
||||||
|
const cacheSize = utils.getArchiveFileSize(archivePath);
|
||||||
|
const commitCacheResponse = await commitCache(
|
||||||
|
httpClient,
|
||||||
|
cacheId,
|
||||||
|
cacheSize
|
||||||
|
);
|
||||||
|
if (!isSuccessStatusCode(commitCacheResponse.statusCode)) {
|
||||||
|
throw new Error(
|
||||||
|
`Cache service responded with ${commitCacheResponse.statusCode} during commit cache.`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
core.info("Cache saved successfully");
|
core.info("Cache saved successfully");
|
||||||
|
13
src/contracts.d.ts
vendored
13
src/contracts.d.ts
vendored
@ -4,3 +4,16 @@ export interface ArtifactCacheEntry {
|
|||||||
creationTime?: string;
|
creationTime?: string;
|
||||||
archiveLocation?: string;
|
archiveLocation?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CommitCacheRequest {
|
||||||
|
size: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ReserveCacheRequest {
|
||||||
|
key: string;
|
||||||
|
version?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ReserveCacheResponse {
|
||||||
|
cacheId: number;
|
||||||
|
}
|
||||||
|
@ -60,7 +60,7 @@ async function run(): Promise<void> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const cacheEntry = await cacheHttpClient.getCacheEntry(keys);
|
const cacheEntry = await cacheHttpClient.getCacheEntry(keys);
|
||||||
if (!cacheEntry) {
|
if (!cacheEntry?.archiveLocation) {
|
||||||
core.info(
|
core.info(
|
||||||
`Cache not found for input keys: ${keys.join(", ")}.`
|
`Cache not found for input keys: ${keys.join(", ")}.`
|
||||||
);
|
);
|
||||||
@ -77,7 +77,10 @@ async function run(): Promise<void> {
|
|||||||
utils.setCacheState(cacheEntry);
|
utils.setCacheState(cacheEntry);
|
||||||
|
|
||||||
// Download the cache from the cache entry
|
// Download the cache from the cache entry
|
||||||
await cacheHttpClient.downloadCache(cacheEntry, archivePath);
|
await cacheHttpClient.downloadCache(
|
||||||
|
cacheEntry.archiveLocation,
|
||||||
|
archivePath
|
||||||
|
);
|
||||||
|
|
||||||
const archiveFileSize = utils.getArchiveFileSize(archivePath);
|
const archiveFileSize = utils.getArchiveFileSize(archivePath);
|
||||||
core.info(
|
core.info(
|
||||||
|
16
src/save.ts
16
src/save.ts
@ -34,6 +34,15 @@ async function run(): Promise<void> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
core.debug("Reserving Cache");
|
||||||
|
const cacheId = await cacheHttpClient.reserveCache(primaryKey);
|
||||||
|
if (cacheId == -1) {
|
||||||
|
core.info(
|
||||||
|
`Unable to reserve cache with key ${primaryKey}, another job may be creating this cache.`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
core.debug(`Cache ID: ${cacheId}`);
|
||||||
const cachePath = utils.resolvePath(
|
const cachePath = utils.resolvePath(
|
||||||
core.getInput(Inputs.Path, { required: true })
|
core.getInput(Inputs.Path, { required: true })
|
||||||
);
|
);
|
||||||
@ -47,19 +56,20 @@ async function run(): Promise<void> {
|
|||||||
|
|
||||||
await createTar(archivePath, cachePath);
|
await createTar(archivePath, cachePath);
|
||||||
|
|
||||||
const fileSizeLimit = 400 * 1024 * 1024; // 400MB
|
const fileSizeLimit = 5 * 1024 * 1024 * 1024; // 5GB per repo limit
|
||||||
const archiveFileSize = utils.getArchiveFileSize(archivePath);
|
const archiveFileSize = utils.getArchiveFileSize(archivePath);
|
||||||
core.debug(`File Size: ${archiveFileSize}`);
|
core.debug(`File Size: ${archiveFileSize}`);
|
||||||
if (archiveFileSize > fileSizeLimit) {
|
if (archiveFileSize > fileSizeLimit) {
|
||||||
utils.logWarning(
|
utils.logWarning(
|
||||||
`Cache size of ~${Math.round(
|
`Cache size of ~${Math.round(
|
||||||
archiveFileSize / (1024 * 1024)
|
archiveFileSize / (1024 * 1024)
|
||||||
)} MB (${archiveFileSize} B) is over the 400MB limit, not saving cache.`
|
)} MB (${archiveFileSize} B) is over the 5GB limit, not saving cache.`
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await cacheHttpClient.saveCache(primaryKey, archivePath);
|
core.debug(`Saving Cache (ID: ${cacheId})`);
|
||||||
|
await cacheHttpClient.saveCache(cacheId, archivePath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
utils.logWarning(error.message);
|
utils.logWarning(error.message);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user