Skip to content

Commit f6ec851

Browse files
committed
feat: add post-job step for cleaning temp dir
1 parent 69c0085 commit f6ec851

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ steps:
7575
- run: echo ${{ steps.plan.outputs.exitcode }}
7676
```
7777

78+
Temporary directory could be deleted after all steps by setting the `cleanup_workspace` variable to `true`:
79+
80+
```yaml
81+
steps:
82+
- uses: hashicorp/setup-terraform@v3
83+
with:
84+
cleanup_workspace: true
85+
```
86+
7887
Outputs can be used in subsequent steps to comment on the pull request:
7988

8089
> **Notice:** There's a limit to the number of characters inside a GitHub comment (65535).
@@ -254,6 +263,8 @@ The action supports the following inputs:
254263
- `terraform_wrapper` - (optional) Whether to install a wrapper to wrap subsequent calls of
255264
the `terraform` binary and expose its STDOUT, STDERR, and exit code as outputs
256265
named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.
266+
- `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`.
257268

258269
## Outputs
259270

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ inputs:
1717
description: 'Whether or not to install a wrapper to wrap subsequent calls of the `terraform` binary and expose its STDOUT, STDERR, and exit code as outputs named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.'
1818
default: 'true'
1919
required: false
20+
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`.'
22+
default: 'false'
23+
required: false
2024
runs:
2125
using: 'node20'
2226
main: 'dist/index.js'
27+
post: 'dist/cleanup.js'
28+
post-if: cleanup_workspace
2329
branding:
2430
icon: 'terminal'
2531
color: 'purple'

dist/cleanup.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
9+
// Retrieve environment variables and parameters
10+
const terraformCliPath = process.env.TERRAFORM_CLI_PATH;
11+
12+
// Function to recursively delete a directory
13+
const deleteDirectoryRecursive = function(directoryPath) {
14+
if (fs.existsSync(directoryPath)) {
15+
fs.readdirSync(directoryPath).forEach((file) => {
16+
const curPath = path.join(directoryPath, file);
17+
if (fs.lstatSync(curPath).isDirectory()) {
18+
// Recurse
19+
deleteDirectoryRecursive(curPath);
20+
} else {
21+
// Delete file
22+
fs.unlinkSync(curPath);
23+
}
24+
});
25+
fs.rmdirSync(directoryPath);
26+
}
27+
};
28+
29+
30+
// Check if cleanup is required
31+
if (terraformCliPath) {
32+
console.log(`Cleaning up directory: ${terraformCliPath}`);
33+
deleteDirectoryRecursive(terraformCliPath);
34+
console.log('Cleanup completed.');
35+
} else {
36+
console.log('No cleanup required.');
37+
}

0 commit comments

Comments
 (0)