Compare commits

..

11 Commits

Author SHA1 Message Date
f46bb21315 Merge branch 'main' into kotewar/states-input-provider 2023-01-09 11:42:34 +05:30
58c146cc91 Release support for cross-os caching as opt-in feature (#1060)
* Release support for cross-os caching as opt-in feature
Add documentation for cross-os caching

* Apply suggestions from code review

Co-authored-by: Lovepreet Singh <pdotl@github.com>

* Address review comments

Co-authored-by: Lovepreet Singh <pdotl@github.com>
2023-01-09 11:01:52 +05:30
6fd2d4538c Add support to opt-in enable cross-os caching on windows (#1056)
* Add support to opt-in enable cross-os caching on windows

* Fix tests

* Address review comments and update tests

* Fix tests

* Address review comments

* Address review comments
2023-01-05 16:49:13 +05:30
2d5e079d9e Merge remote-tracking branch 'origin/master' into kotewar/states-input-provider 2023-01-03 10:22:58 +00:00
667e98af5a Fixed test cases 2023-01-03 10:22:38 +00:00
5b7eeecaeb stateinputprovider with pending test cases fix 2023-01-02 07:29:20 +00:00
0769f2e443 Merge branch 'main' into master 2022-11-28 23:50:15 -08:00
515d10b4fd Merge pull request #746 from actions/revert-173-add-stack-example
Revert "Add example for Haskell Stack"
2022-02-22 12:44:25 +05:30
669e7536d9 Revert "Add example for Haskell Stack" 2022-02-22 12:17:37 +05:30
29dbbce762 Merge pull request #173 from malob/add-stack-example
Add example for Haskell Stack
2022-02-22 12:11:06 +05:30
ea5981db97 Add example for Haskell Stack 2022-02-21 14:59:28 -08:00
14 changed files with 111 additions and 59 deletions

View File

@ -28,6 +28,7 @@ See ["Caching dependencies to speed up workflows"](https://docs.github.com/en/ac
* Fix zstd not working for windows on gnu tar in issues.
* Allowing users to provide a custom timeout as input for aborting download of a cache segment using an environment variable `SEGMENT_DOWNLOAD_TIMEOUT_MINS`. Default is 60 minutes.
* Two new actions available for granular control over caches - [restore](restore/action.yml) and [save](save/action.yml)
* Support cross-os caching as an opt-in feature. See [Cross OS caching](./tips-and-workarounds.md#cross-os-cache) for more info.
Refer [here](https://github.com/actions/cache/blob/v2/README.md) for previous versions
@ -43,6 +44,7 @@ If you are using this inside a container, a POSIX-compliant `tar` needs to be in
* `path` - A list of files, directories, and wildcard patterns to cache and restore. See [`@actions/glob`](https://github.com/actions/toolkit/tree/main/packages/glob) for supported patterns.
* `key` - An explicit key for restoring and saving the cache
* `restore-keys` - An ordered list of prefix-matched keys to use for restoring stale cache if no cache hit occurred for key.
* `enableCrossOsArchive` - An optional boolean when enabled, allows Windows runners to save or restore caches that can be restored or saved respectively on other platforms. Default: false
#### Environment Variables
* `SEGMENT_DOWNLOAD_TIMEOUT_MINS` - Segment download timeout (in minutes, default `60`) to abort download of the segment if not completed in the defined number of minutes. [Read more](https://github.com/actions/cache/blob/main/tips-and-workarounds.md#cache-segment-restore-timeout)
@ -245,7 +247,7 @@ Following are some of the known practices/workarounds which community has used t
- [Cache segment restore timeout](./tips-and-workarounds.md#cache-segment-restore-timeout)
- [Update a cache](./tips-and-workarounds.md#update-a-cache)
- [Use cache across feature branches](./tips-and-workarounds.md#use-cache-across-feature-branches)
- [Improving cache restore performance on Windows/Using cross-os caching](./tips-and-workarounds.md#improving-cache-restore-performance-on-windows-using-cross-os-caching)
- [Cross OS cache](./tips-and-workarounds.md#cross-os-cache)
- [Force deletion of caches overriding default cache eviction policy](./tips-and-workarounds.md#force-deletion-of-caches-overriding-default-cache-eviction-policy)
#### Windows environment variables

View File

@ -62,4 +62,8 @@
- Added logs for cache version in case of a cache miss.
### 3.2.2
- Reverted the changes made in 3.2.1 to use gnu tar and zstd by default on windows.
- Reverted the changes made in 3.2.1 to use gnu tar and zstd by default on windows.
### 3.2.3
- Support cross os caching on Windows as an opt-in feature.
- Fix issue with symlink restoration on Windows for cross-os caches.

View File

@ -11,12 +11,12 @@ jest.mock("@actions/cache");
jest.mock("../src/utils/actionUtils");
beforeAll(() => {
jest.spyOn(core, "getInput").mockImplementation((name, options) => {
return jest.requireActual("@actions/core").getInput(name, options);
jest.spyOn(core, "getInput").mockImplementation(name => {
return testUtils.getInput(name);
});
jest.spyOn(core, "setOutput").mockImplementation((key, value) => {
return jest.requireActual("@actions/core").getInput(key, value);
jest.spyOn(core, "getState").mockImplementation(name => {
return jest.requireActual("@actions/core").getState(name);
});
jest.spyOn(actionUtils, "getInputAsArray").mockImplementation(

View File

@ -1,11 +1,12 @@
import * as core from "@actions/core";
import { Events, RefKey, State } from "../src/constants";
import { Events, Inputs, RefKey, State } from "../src/constants";
import {
IStateProvider,
NullStateProvider,
StateProvider
} from "../src/stateProvider";
import * as testUtils from "../src/utils/testUtils";
jest.mock("@actions/core");
@ -58,32 +59,43 @@ test("StateProvider saves states", async () => {
});
test("NullStateProvider saves outputs", async () => {
const states = new Map<string, string>();
const getInputMock = jest
.spyOn(core, "getInput")
.mockImplementation(key => testUtils.getInput(key));
const getStateMock = jest
.spyOn(core, "getState")
.mockImplementation(name =>
jest.requireActual("@actions/core").getState(name)
);
.mockImplementation(key => {
return jest.requireActual("@actions/core").getState(key);
});
const setOutputMock = jest
.spyOn(core, "setOutput")
.mockImplementation((key, value) => {
return jest.requireActual("@actions/core").setOutput(key, value);
states.set(key, value);
});
const saveStateMock = jest
.spyOn(core, "saveState")
.mockImplementation((key, value) => {
return jest.requireActual("@actions/core").saveState(key, value);
states.set(key, value);
});
const cacheMatchedKey = "node-cache";
const cachePrimaryKey = "primary-key";
const nullStateProvider: IStateProvider = new NullStateProvider();
nullStateProvider.setState(State.CacheMatchedKey, "outputValue");
nullStateProvider.setState(State.CachePrimaryKey, cacheMatchedKey);
nullStateProvider.getState("outputKey");
nullStateProvider.getCacheState();
testUtils.setInput(Inputs.Key, cachePrimaryKey);
nullStateProvider.setState(State.CachePrimaryKey, cachePrimaryKey);
nullStateProvider.setState(State.CacheMatchedKey, cacheMatchedKey);
const output1 = nullStateProvider.getState(State.CachePrimaryKey);
const output2 = nullStateProvider.getCacheState();
expect(getStateMock).toHaveBeenCalledTimes(0);
expect(getInputMock).toHaveBeenCalledTimes(1);
expect(output1).toBe("primary-key");
expect(output2).toBe(undefined);
expect(setOutputMock).toHaveBeenCalledTimes(2);
expect(saveStateMock).toHaveBeenCalledTimes(0);
});

View File

@ -9413,15 +9413,24 @@ exports.StateProvider = StateProvider;
class NullStateProvider extends StateProviderBase {
constructor() {
super(...arguments);
this.stateToInputMap = new Map([
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
]);
this.stateToOutputMap = new Map([
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
]);
this.setState = (key, value) => {
core.setOutput(this.stateToOutputMap.get(key), value);
if (this.stateToOutputMap.has(key)) {
core.setOutput(this.stateToOutputMap.get(key), value);
}
};
this.getState = (key) => {
if (!this.stateToInputMap.has(key)) {
return "";
}
return core.getInput(this.stateToInputMap.get(key));
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
this.getState = (key) => "";
}
}
exports.NullStateProvider = NullStateProvider;

15
dist/restore/index.js vendored
View File

@ -9413,15 +9413,24 @@ exports.StateProvider = StateProvider;
class NullStateProvider extends StateProviderBase {
constructor() {
super(...arguments);
this.stateToInputMap = new Map([
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
]);
this.stateToOutputMap = new Map([
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
]);
this.setState = (key, value) => {
core.setOutput(this.stateToOutputMap.get(key), value);
if (this.stateToOutputMap.has(key)) {
core.setOutput(this.stateToOutputMap.get(key), value);
}
};
this.getState = (key) => {
if (!this.stateToInputMap.has(key)) {
return "";
}
return core.getInput(this.stateToInputMap.get(key));
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
this.getState = (key) => "";
}
}
exports.NullStateProvider = NullStateProvider;

View File

@ -9469,15 +9469,24 @@ exports.StateProvider = StateProvider;
class NullStateProvider extends StateProviderBase {
constructor() {
super(...arguments);
this.stateToInputMap = new Map([
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
]);
this.stateToOutputMap = new Map([
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
]);
this.setState = (key, value) => {
core.setOutput(this.stateToOutputMap.get(key), value);
if (this.stateToOutputMap.has(key)) {
core.setOutput(this.stateToOutputMap.get(key), value);
}
};
this.getState = (key) => {
if (!this.stateToInputMap.has(key)) {
return "";
}
return core.getInput(this.stateToInputMap.get(key));
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
this.getState = (key) => "";
}
}
exports.NullStateProvider = NullStateProvider;
@ -41164,8 +41173,7 @@ function saveImpl(stateProvider) {
}
// If restore has stored a primary key in state, reuse that
// Else re-evaluate from inputs
const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey) ||
core.getInput(constants_1.Inputs.Key);
const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey);
if (!primaryKey) {
utils.logWarning(`Key is not specified.`);
return;

18
dist/save/index.js vendored
View File

@ -9413,15 +9413,24 @@ exports.StateProvider = StateProvider;
class NullStateProvider extends StateProviderBase {
constructor() {
super(...arguments);
this.stateToInputMap = new Map([
[constants_1.State.CachePrimaryKey, constants_1.Inputs.Key]
]);
this.stateToOutputMap = new Map([
[constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey],
[constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey]
]);
this.setState = (key, value) => {
core.setOutput(this.stateToOutputMap.get(key), value);
if (this.stateToOutputMap.has(key)) {
core.setOutput(this.stateToOutputMap.get(key), value);
}
};
this.getState = (key) => {
if (!this.stateToInputMap.has(key)) {
return "";
}
return core.getInput(this.stateToInputMap.get(key));
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
this.getState = (key) => "";
}
}
exports.NullStateProvider = NullStateProvider;
@ -41108,8 +41117,7 @@ function saveImpl(stateProvider) {
}
// If restore has stored a primary key in state, reuse that
// Else re-evaluate from inputs
const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey) ||
core.getInput(constants_1.Inputs.Key);
const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey);
if (!primaryKey) {
utils.logWarning(`Key is not specified.`);
return;

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "cache",
"version": "3.2.2",
"version": "3.2.3",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "cache",
"version": "3.2.2",
"version": "3.2.3",
"license": "MIT",
"dependencies": {
"@actions/cache": "^3.1.2",

View File

@ -1,6 +1,6 @@
{
"name": "cache",
"version": "3.2.2",
"version": "3.2.3",
"private": true,
"description": "Cache dependencies and build outputs",
"main": "dist/restore/index.js",

View File

@ -28,9 +28,7 @@ async function saveImpl(stateProvider: IStateProvider): Promise<number | void> {
// If restore has stored a primary key in state, reuse that
// Else re-evaluate from inputs
const primaryKey =
stateProvider.getState(State.CachePrimaryKey) ||
core.getInput(Inputs.Key);
const primaryKey = stateProvider.getState(State.CachePrimaryKey);
if (!primaryKey) {
utils.logWarning(`Key is not specified.`);

View File

@ -1,6 +1,6 @@
import * as core from "@actions/core";
import { Outputs, State } from "./constants";
import { Inputs, Outputs, State } from "./constants";
export interface IStateProvider {
setState(key: string, value: string): void;
@ -33,14 +33,25 @@ export class StateProvider extends StateProviderBase {
}
export class NullStateProvider extends StateProviderBase {
stateToInputMap = new Map<string, string>([
[State.CachePrimaryKey, Inputs.Key]
]);
stateToOutputMap = new Map<string, string>([
[State.CacheMatchedKey, Outputs.CacheMatchedKey],
[State.CachePrimaryKey, Outputs.CachePrimaryKey]
]);
setState = (key: string, value: string) => {
core.setOutput(this.stateToOutputMap.get(key) as string, value);
if (this.stateToOutputMap.has(key)) {
core.setOutput(this.stateToOutputMap.get(key) as string, value);
}
};
getState = (key: string) => {
if (!this.stateToInputMap.has(key)) {
return "";
}
return core.getInput(this.stateToInputMap.get(key) as string);
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getState = (key: string) => "";
}

View File

@ -9,6 +9,10 @@ export function setInput(name: string, value: string): void {
process.env[getInputName(name)] = value;
}
export function getInput(name: string): string {
return process.env[getInputName(name)] as string;
}
interface CacheInput {
path: string;
key: string;

View File

@ -19,23 +19,10 @@ A cache today is immutable and cannot be updated. But some use cases require the
## Use cache across feature branches
Reusing cache across feature branches is not allowed today to provide cache [isolation](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache). However if both feature branches are from the default branch, a good way to achieve this is to ensure that the default branch has a cache. This cache will then be consumable by both feature branches.
## Improving cache restore performance on Windows/Using cross-os caching
Currently, cache restore is slow on Windows due to tar being inherently slow and the compression algorithm `gzip` in use. `zstd` is the default algorithm in use on linux and macos. It was disabled on Windows due to issues with bsd tar(libarchive), the tar implementation in use on Windows.
To improve cache restore performance, we can re-enable `zstd` as the compression algorithm using the following workaround. Add the following step to your workflow before the cache step:
```yaml
- if: ${{ runner.os == 'Windows' }}
name: Use GNU tar
shell: cmd
run: |
echo "Adding GNU tar to PATH"
echo C:\Program Files\Git\usr\bin>>"%GITHUB_PATH%"
```
The `cache` action will use GNU tar instead of bsd tar on Windows. This should work on all Github Hosted runners as it is. For self-hosted runners, please ensure you have GNU tar and `zstd` installed.
The above workaround is also needed if you wish to use cross-os caching since difference of compression algorithms will result in different cache versions for the same cache key. So the above workaround will ensure `zstd` is used for caching on all platforms thus resulting in the same cache version for the same cache key.
## Cross OS cache
From `v3.2.3` cache is cross-os compatible when `enableCrossOsArchive` input is passed as true. This means that a cache created on `ubuntu-latest` or `mac-latest` can be used by `windows-latest` and vice versa, provided the workflow which runs on `windows-latest` have input `enableCrossOsArchive` as true. This is useful to cache dependencies which are independent of the runner platform. This will help reduce the consumption of the cache quota and help build for multiple platforms from the same cache. Things to keep in mind while using this feature:
- Only cache those files which are compatible across OSs.
- Caching symlinks might cause issues while restoration as they work differently on different OSs.
## Force deletion of caches overriding default cache eviction policy
Caches have [branch scope restriction](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache) in place. This means that if caches for a specific branch are using a lot of storage quota, it may result into more frequently used caches from `default` branch getting thrashed. For example, if there are many pull requests happening on a repo and are creating caches, these cannot be used in default branch scope but will still occupy a lot of space till they get cleaned up by [eviction policy](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#usage-limits-and-eviction-policy). But sometime we want to clean them up on a faster cadence so as to ensure default branch is not thrashing. In order to achieve this, [gh-actions-cache cli](https://github.com/actions/gh-actions-cache/) can be used to delete caches for specific branches.