Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default cache input to false for self-hosted runners #412

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions __tests__/cache-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as cacheUtils from '../src/cache-utils';
import {PackageManagerInfo} from '../src/package-managers';
import * as utils from '../src/utils';

describe('getCommandOutput', () => {
//Arrange
Expand Down Expand Up @@ -209,3 +210,76 @@ describe('isCacheFeatureAvailable', () => {
expect(warningSpy).toHaveBeenCalledWith(warningMessage);
});
});

describe('Detect environment', () => {
it('"RUNNER_ENVIRONMENT" = "github-hosted" should be hosted environment', () => {
delete process.env['AGENT_ISSELFHOSTED'];
process.env['RUNNER_ENVIRONMENT'] = 'github-hosted';
expect(utils.isSelfHosted()).toBeFalsy();
});

it('"RUNNER_ENVIRONMENT" = "hosted" should be self-hosted environment', () => {
delete process.env['AGENT_ISSELFHOSTED'];
process.env['RUNNER_ENVIRONMENT'] = 'hosted';
expect(utils.isSelfHosted()).toBeTruthy();
});

it('"AGENT_ISSELFHOSTED" = "0" should be hosted environment', () => {
process.env['AGENT_ISSELFHOSTED'] = '0';
delete process.env['RUNNER_ENVIRONMENT'];
expect(utils.isSelfHosted()).toBeFalsy();
});

it('"AGENT_ISSELFHOSTED" = "0" should be self-hosted environment', () => {
process.env['AGENT_ISSELFHOSTED'] = '1';
delete process.env['RUNNER_ENVIRONMENT'];
expect(utils.isSelfHosted()).toBeTruthy();
});

it('unset "RUNNER_ENVIRONMENT" and "AGENT_ISSELFHOSTED" should be self-hosted environment', () => {
delete process.env['AGENT_ISSELFHOSTED'];
delete process.env['RUNNER_ENVIRONMENT'];
expect(utils.isSelfHosted()).toBeTruthy();
});
});
describe('Default cache values', () => {
const inputSpy = jest.spyOn(utils, 'isSelfHosted');

beforeEach(() => {
delete process.env['INPUT_CACHE'];
});

it('default cache should be false in self-hosted environment', () => {
inputSpy.mockReturnValueOnce(true);
expect(cacheUtils.getCacheInput()).toBeFalsy();
});

it('cache should be false if set to false in self-hosted environment', () => {
inputSpy.mockReturnValueOnce(true);
process.env['INPUT_CACHE'] = 'false';
expect(cacheUtils.getCacheInput()).toBeFalsy();
});

it('cache should be tue if set to true in self-hosted environment', () => {
inputSpy.mockReturnValueOnce(true);
process.env['INPUT_CACHE'] = 'true';
expect(cacheUtils.getCacheInput()).toBeTruthy();
});

it('default cache should be handled by action.yml default in hosted environment', () => {
inputSpy.mockReturnValueOnce(false);
expect(() => cacheUtils.getCacheInput()).toThrow();
});

it('cache should be false if set to false in hosted environment', () => {
inputSpy.mockReturnValueOnce(false);
process.env['INPUT_CACHE'] = 'true';
expect(cacheUtils.getCacheInput()).toBeTruthy();
});

it('cache should be tue if set to true in hosted environment', () => {
inputSpy.mockReturnValueOnce(false);
process.env['INPUT_CACHE'] = 'false';
expect(cacheUtils.getCacheInput()).toBeFalsy();
});
});
33 changes: 29 additions & 4 deletions dist/cache-save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58521,10 +58521,8 @@ function run() {
}
exports.run = run;
const cachePackages = () => __awaiter(void 0, void 0, void 0, function* () {
const cacheInput = core.getBooleanInput('cache');
if (!cacheInput) {
if (!cache_utils_1.getCacheInput())
return;
}
const packageManager = 'default';
const state = core.getState(constants_1.State.CacheMatchedKey);
const primaryKey = core.getState(constants_1.State.CachePrimaryKey);
Expand Down Expand Up @@ -58595,11 +58593,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0;
exports.getCacheInput = exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0;
const cache = __importStar(__nccwpck_require__(7799));
const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const package_managers_1 = __nccwpck_require__(6663);
const utils_1 = __nccwpck_require__(1314);
const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true });
if (exitCode) {
Expand Down Expand Up @@ -58654,6 +58653,13 @@ function isCacheFeatureAvailable() {
return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
const getCacheInput = () => {
// for self-hosted environment turn off cache by default
if (utils_1.isSelfHosted() && core.getInput('cache') === '')
return false;
return core.getBooleanInput('cache');
};
exports.getCacheInput = getCacheInput;


/***/ }),
Expand Down Expand Up @@ -58693,6 +58699,25 @@ exports.supportedPackageManagers = {
};


/***/ }),

/***/ 1314:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isSelfHosted = exports.StableReleaseAlias = void 0;
var StableReleaseAlias;
(function (StableReleaseAlias) {
StableReleaseAlias["Stable"] = "stable";
StableReleaseAlias["OldStable"] = "oldstable";
})(StableReleaseAlias = exports.StableReleaseAlias || (exports.StableReleaseAlias = {}));
const isSelfHosted = () => process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' &&
process.env['AGENT_ISSELFHOSTED'] !== '0';
exports.isSelfHosted = isSelfHosted;


/***/ }),

/***/ 2877:
Expand Down
20 changes: 15 additions & 5 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61265,11 +61265,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0;
exports.getCacheInput = exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0;
const cache = __importStar(__nccwpck_require__(7799));
const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const package_managers_1 = __nccwpck_require__(6663);
const utils_1 = __nccwpck_require__(1314);
const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true });
if (exitCode) {
Expand Down Expand Up @@ -61324,6 +61325,13 @@ function isCacheFeatureAvailable() {
return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
const getCacheInput = () => {
// for self-hosted environment turn off cache by default
if (utils_1.isSelfHosted() && core.getInput('cache') === '')
return false;
return core.getBooleanInput('cache');
};
exports.getCacheInput = getCacheInput;


/***/ }),
Expand Down Expand Up @@ -61495,8 +61503,7 @@ function cacheWindowsDir(extPath, tool, version, arch) {
if (os_1.default.platform() !== 'win32')
return false;
// make sure the action runs in the hosted environment
if (process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' &&
process.env['AGENT_ISSELFHOSTED'] === '1')
if (utils_1.isSelfHosted())
return false;
const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'];
if (!defaultToolCacheRoot)
Expand Down Expand Up @@ -61785,7 +61792,7 @@ function run() {
// If not supplied then problem matchers will still be setup. Useful for self-hosted.
//
const versionSpec = resolveVersionInput();
const cache = core.getBooleanInput('cache');
const cache = cache_utils_1.getCacheInput();
core.info(`Setup go version spec ${versionSpec}`);
let arch = core.getInput('architecture');
if (!arch) {
Expand Down Expand Up @@ -61971,12 +61978,15 @@ exports.getArch = getArch;
"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.StableReleaseAlias = void 0;
exports.isSelfHosted = exports.StableReleaseAlias = void 0;
var StableReleaseAlias;
(function (StableReleaseAlias) {
StableReleaseAlias["Stable"] = "stable";
StableReleaseAlias["OldStable"] = "oldstable";
})(StableReleaseAlias = exports.StableReleaseAlias || (exports.StableReleaseAlias = {}));
const isSelfHosted = () => process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' &&
process.env['AGENT_ISSELFHOSTED'] !== '0';
exports.isSelfHosted = isSelfHosted;


/***/ }),
Expand Down
4 changes: 2 additions & 2 deletions docs/adrs/0000-caching-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ We don't pursue the goal to provide wide customization of caching in scope of `a

# Decision
- Add `cache` input parameter to `actions/setup-go`. For now, input will accept the following values:
- `true` - enable caching for go dependencies
- `false`- disable caching for go dependencies. This value will be set as default value
- `true` - enable caching for go dependencies. This value will be set as default value for hosted runners.
- `false`- disable caching for go dependencies. This value will be set as default value for self-hosted runners.
- Cache feature will be disabled by default to make sure that we don't break existing customers. We will consider enabling cache by default in next major releases
- Action will try to search a go.sum files in the repository and throw error in the scenario that it was not found
- The hash of found file will be used as cache key (the same approach like [actions/cache](https://github.com/actions/cache/blob/main/examples.md#go---modules) recommends)
Expand Down
11 changes: 6 additions & 5 deletions src/cache-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import * as core from '@actions/core';
import * as cache from '@actions/cache';
import fs from 'fs';
import {State} from './constants';
import {getCacheDirectoryPath, getPackageManagerInfo} from './cache-utils';
import {
getCacheDirectoryPath,
getPackageManagerInfo,
getCacheInput
} from './cache-utils';

// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
Expand All @@ -28,10 +32,7 @@ export async function run() {
}

const cachePackages = async () => {
const cacheInput = core.getBooleanInput('cache');
if (!cacheInput) {
return;
}
if (!getCacheInput()) return;

const packageManager = 'default';

Expand Down
7 changes: 7 additions & 0 deletions src/cache-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import {supportedPackageManagers, PackageManagerInfo} from './package-managers';
import {isSelfHosted} from './utils';

export const getCommandOutput = async (toolCommand: string) => {
let {stdout, stderr, exitCode} = await exec.getExecOutput(
Expand Down Expand Up @@ -83,3 +84,9 @@ export function isCacheFeatureAvailable(): boolean {
);
return false;
}
export const getCacheInput = (): boolean => {
// for self-hosted environment turn off cache by default
if (isSelfHosted() && core.getInput('cache') === '') return false;

return core.getBooleanInput('cache');
};
8 changes: 2 additions & 6 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as httpm from '@actions/http-client';
import * as sys from './system';
import fs from 'fs';
import os from 'os';
import {StableReleaseAlias} from './utils';
import {isSelfHosted, StableReleaseAlias} from './utils';

type InstallationType = 'dist' | 'manifest';

Expand Down Expand Up @@ -175,11 +175,7 @@ async function cacheWindowsDir(
if (os.platform() !== 'win32') return false;

// make sure the action runs in the hosted environment
if (
process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' &&
process.env['AGENT_ISSELFHOSTED'] === '1'
)
return false;
if (isSelfHosted()) return false;

const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'];
if (!defaultToolCacheRoot) return false;
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as installer from './installer';
import * as semver from 'semver';
import path from 'path';
import {restoreCache} from './cache-restore';
import {isCacheFeatureAvailable} from './cache-utils';
import {isCacheFeatureAvailable, getCacheInput} from './cache-utils';
import cp from 'child_process';
import fs from 'fs';
import os from 'os';
Expand All @@ -17,7 +17,7 @@ export async function run() {
//
const versionSpec = resolveVersionInput();

const cache = core.getBooleanInput('cache');
const cache = getCacheInput();
core.info(`Setup go version spec ${versionSpec}`);

let arch = core.getInput('architecture');
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export enum StableReleaseAlias {
Stable = 'stable',
OldStable = 'oldstable'
}

export const isSelfHosted = (): boolean =>
process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' &&
process.env['AGENT_ISSELFHOSTED'] !== '0';
Loading