-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <peter.somogyvari@accenture.com>
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...ges/cactus-test-tooling/src/main/typescript/github-actions/is-running-in-github-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} |
27 changes: 27 additions & 0 deletions
27
...ctus-test-tooling/src/main/typescript/github-actions/prune-docker-all-if-github-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Optional<IPruneDockerResourcesResponse>> { | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters