diff --git a/packages/cactus-test-tooling/src/main/typescript/github-actions/is-running-in-github-action.ts b/packages/cactus-test-tooling/src/main/typescript/github-actions/is-running-in-github-action.ts new file mode 100644 index 0000000000..2f19c01c09 --- /dev/null +++ b/packages/cactus-test-tooling/src/main/typescript/github-actions/is-running-in-github-action.ts @@ -0,0 +1,27 @@ +import { Checks } from "@hyperledger/cactus-common"; + +/** + * Utility/helper function that examines if the current code is running on Github Actions + * or not. + * + * Important note: Do not use this in production code it is meant to be used for tests + * only. Do not depend on this function in your code outside of the test cases. + * + * Uses the environment variable `GITHUB_ACTIONS` to determine its output which is always + * set to `"true"` when GitHub Actions is running the workflow. + * You can use this variable to differentiate when tests are being run locally or by GitHub Actions. + * + * @see https://docs.github.com/en/actions/reference/environment-variables + * + * @param env The process environment variables object to look into when attempting to + * determine if the current execution environment appears to be a GitHub Action VM or + * not. + * @returns + */ +export function isRunningInGithubAction( + env: NodeJS.ProcessEnv = process.env, +): boolean { + Checks.truthy(env, "isRunningInGithubAction():env"); + + return env.GITHUB_ACTIONS === "true"; +} diff --git a/packages/cactus-test-tooling/src/main/typescript/github-actions/prune-docker-all-if-github-action.ts b/packages/cactus-test-tooling/src/main/typescript/github-actions/prune-docker-all-if-github-action.ts new file mode 100644 index 0000000000..11ac18a7d1 --- /dev/null +++ b/packages/cactus-test-tooling/src/main/typescript/github-actions/prune-docker-all-if-github-action.ts @@ -0,0 +1,27 @@ +import { Optional } from "typescript-optional"; +import { + Containers, + IPruneDockerResourcesRequest, + IPruneDockerResourcesResponse, +} from "../common/containers"; +import { isRunningInGithubAction } from "./is-running-in-github-action"; + +/** + * Github Actions started to run out of disk space recently (as of March, 2021) so we have this + * hack here to attempt to free up disk space when running inside a VM of the CI system. + * The idea is that tests can call this function before and after their execution so that + * their container images/volumes get freed up. + */ +export async function pruneDockerAllIfGithubAction( + req?: IPruneDockerResourcesRequest, +): Promise> { + if (!isRunningInGithubAction()) { + return Optional.empty(); + } + console.log( + "Detected current process to be running " + + "inside a Github Action. Pruning all docker resources...", + ); + const res = await Containers.pruneDockerResources(req); + return Optional.of(res); +} diff --git a/packages/cactus-test-tooling/src/main/typescript/public-api.ts b/packages/cactus-test-tooling/src/main/typescript/public-api.ts index baeb8bf1a8..75b77ee5a2 100755 --- a/packages/cactus-test-tooling/src/main/typescript/public-api.ts +++ b/packages/cactus-test-tooling/src/main/typescript/public-api.ts @@ -73,3 +73,6 @@ export { } from "./corda/sample-cordapp-enum"; export { Streams } from "./common/streams"; + +export { isRunningInGithubAction } from "./github-actions/is-running-in-github-action"; +export { pruneDockerAllIfGithubAction } from "./github-actions/prune-docker-all-if-github-action";