Skip to content

Commit 2784ceb

Browse files
committed
feat(test-tooling): utility function docker prune in GH action #696
Primary change -------------- Added a convenience function that tests can call in order to easily get rid of dangling docker resources that could be eating up the file system's available space on disk (which is an issue that we've started hitting more and more as we kept increasing the number of test cases that use containers for end to end testing). A good problem to have but nevertheless it is a problem that we need to be vigilant about cleaning up after our test cases to avoid overusing the resources of the CI runner. Miscellaneous change(s) ----------------------- Also added another utility function to determine if the current code is being executed in a GitHub Action Workflow runner or not. The presence and value of an environment variable is used to achieve this feat so it definitely is not bullet proof but the trade- off seemed fair given that these utility functions are only meant to be used by the test code anyway. Signed-off-by: Peter Somogyvari <[email protected]>
1 parent d92760f commit 2784ceb

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Checks } from "@hyperledger/cactus-common";
2+
3+
/**
4+
* Utility/helper function that examines if the current code is running on Github Actions
5+
* or not.
6+
*
7+
* Important note: Do not use this in production code it is meant to be used for tests
8+
* only. Do not depend on this function in your code outside of the test cases.
9+
*
10+
* Uses the environment variable `GITHUB_ACTIONS` to determine its output which is always
11+
* set to `"true"` when GitHub Actions is running the workflow.
12+
* You can use this variable to differentiate when tests are being run locally or by GitHub Actions.
13+
*
14+
* @see https://docs.github.com/en/actions/reference/environment-variables
15+
*
16+
* @param env The process environment variables object to look into when attempting to
17+
* determine if the current execution environment appears to be a GitHub Action VM or
18+
* not.
19+
* @returns
20+
*/
21+
export function isRunningInGithubAction(
22+
env: NodeJS.ProcessEnv = process.env,
23+
): boolean {
24+
Checks.truthy(env, "isRunningInGithubAction():env");
25+
26+
return env.GITHUB_ACTIONS === "true";
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Optional } from "typescript-optional";
2+
import {
3+
Containers,
4+
IPruneDockerResourcesRequest,
5+
IPruneDockerResourcesResponse,
6+
} from "../common/containers";
7+
import { isRunningInGithubAction } from "./is-running-in-github-action";
8+
9+
/**
10+
* Github Actions started to run out of disk space recently (as of March, 2021) so we have this
11+
* hack here to attempt to free up disk space when running inside a VM of the CI system.
12+
* The idea is that tests can call this function before and after their execution so that
13+
* their container images/volumes get freed up.
14+
*/
15+
export async function pruneDockerAllIfGithubAction(
16+
req?: IPruneDockerResourcesRequest,
17+
): Promise<Optional<IPruneDockerResourcesResponse>> {
18+
if (!isRunningInGithubAction()) {
19+
return Optional.empty();
20+
}
21+
console.log(
22+
"Detected current process to be running " +
23+
"inside a Github Action. Pruning all docker resources...",
24+
);
25+
const res = await Containers.pruneDockerResources(req);
26+
return Optional.of(res);
27+
}

packages/cactus-test-tooling/src/main/typescript/public-api.ts

+3
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ export {
7373
} from "./corda/sample-cordapp-enum";
7474

7575
export { Streams } from "./common/streams";
76+
77+
export { isRunningInGithubAction } from "./github-actions/is-running-in-github-action";
78+
export { pruneDockerAllIfGithubAction } from "./github-actions/prune-docker-all-if-github-action";

0 commit comments

Comments
 (0)