Skip to content

Commit 88008c0

Browse files
committed
feat: add test for cleanup
1 parent f6ec851 commit 88008c0

9 files changed

+26005
-42
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ The action supports the following inputs:
264264
the `terraform` binary and expose its STDOUT, STDERR, and exit code as outputs
265265
named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.
266266
- `cleanup_workspace` - (optional) The Terraform binary file is downloaded to a temporary directory.
267-
This parameter will be used to clean that directory. Defaults to `true`.
267+
This parameter controls whether to clean that directory. Defaults to `false`.
268268

269269
## Outputs
270270

action.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ inputs:
1818
default: 'true'
1919
required: false
2020
cleanup_workspace:
21-
description: 'The Terraform binary file is downloaded to a temporary directory. This parameter will be used to clean that directory. Defaults to `true`.'
21+
description: 'The Terraform binary file is downloaded to a temporary directory. This parameter controls whether to clean that directory. Defaults to `false`.'
2222
default: 'false'
2323
required: false
2424
runs:
2525
using: 'node20'
2626
main: 'dist/index.js'
27-
post: 'dist/cleanup.js'
28-
post-if: cleanup_workspace
27+
post: 'cleanup/dist/index.js'
28+
# post-if: "github.events.inputs.cleanup_workspace"
2929
branding:
3030
icon: 'terminal'
3131
color: 'purple'

cleanup/cleanup.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
const fs = require('fs');
7+
const path = require('path');
8+
const core = require('@actions/core');
9+
10+
async function run () {
11+
// Retrieve environment variables and parameters
12+
const terraformCliPath = process.env.TERRAFORM_CLI_PATH;
13+
// This parameter should be set in `action.yaml` to the `runs.post-if` condition after solving issue https://github.com/actions/runner/issues/2800
14+
const cleanup = core.getInput('cleanup_workspace');
15+
16+
// Function to recursively delete a directory
17+
const deleteDirectoryRecursive = function (directoryPath) {
18+
if (fs.existsSync(directoryPath)) {
19+
fs.readdirSync(directoryPath).forEach((file) => {
20+
const curPath = path.join(directoryPath, file);
21+
if (fs.lstatSync(curPath).isDirectory()) {
22+
// Recurse
23+
deleteDirectoryRecursive(curPath);
24+
} else {
25+
// Delete file
26+
fs.unlinkSync(curPath);
27+
}
28+
});
29+
fs.rmdirSync(directoryPath);
30+
}
31+
};
32+
33+
// Check if cleanup is required
34+
if (cleanup === 'true' && terraformCliPath) {
35+
console.log(`Cleaning up directory: ${terraformCliPath}`);
36+
deleteDirectoryRecursive(terraformCliPath);
37+
console.log('Cleanup completed.');
38+
} else {
39+
console.log('No cleanup required.');
40+
}
41+
}
42+
run();

cleanup/dist/debugfile.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraformCliPath: testDir

0 commit comments

Comments
 (0)