Skip to content

Commit

Permalink
feat: add test for cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sfreydin committed Dec 19, 2023
1 parent f6ec851 commit b3d0f67
Show file tree
Hide file tree
Showing 7 changed files with 26,003 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ The action supports the following inputs:
the `terraform` binary and expose its STDOUT, STDERR, and exit code as outputs
named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.
- `cleanup_workspace` - (optional) The Terraform binary file is downloaded to a temporary directory.
This parameter will be used to clean that directory. Defaults to `true`.
This parameter controls whether to clean that directory. Defaults to `false`.

## Outputs

Expand Down
6 changes: 3 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ inputs:
default: 'true'
required: false
cleanup_workspace:
description: 'The Terraform binary file is downloaded to a temporary directory. This parameter will be used to clean that directory. Defaults to `true`.'
description: 'The Terraform binary file is downloaded to a temporary directory. This parameter controls whether to clean that directory. Defaults to `false`.'
default: 'false'
required: false
runs:
using: 'node20'
main: 'dist/index.js'
post: 'dist/cleanup.js'
post-if: cleanup_workspace
post: 'cleanup/dist/index.js'
# post-if: "github.events.inputs.cleanup_workspace"
branding:
icon: 'terminal'
color: 'purple'
42 changes: 42 additions & 0 deletions cleanup/cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

const fs = require('fs');
const path = require('path');
const core = require('@actions/core');

async function run () {
// Retrieve environment variables and parameters
const terraformCliPath = process.env.TERRAFORM_CLI_PATH;
// This parameter should be set in `action.yaml` to the `runs.post-if` condition after solving issue https://github.com/actions/runner/issues/2800
const cleanup = core.getInput('cleanup_workspace');

// Function to recursively delete a directory
const deleteDirectoryRecursive = function (directoryPath) {
if (fs.existsSync(directoryPath)) {
fs.readdirSync(directoryPath).forEach((file) => {
const curPath = path.join(directoryPath, file);
if (fs.lstatSync(curPath).isDirectory()) {
// Recurse
deleteDirectoryRecursive(curPath);
} else {
// Delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(directoryPath);
}
};

// Check if cleanup is required
if (cleanup === 'true' && terraformCliPath) {
console.log(`Cleaning up directory: ${terraformCliPath}`);
deleteDirectoryRecursive(terraformCliPath);
console.log('Cleanup completed.');
} else {
console.log('No cleanup required.');
}
}
run();
Loading

0 comments on commit b3d0f67

Please sign in to comment.