diff --git a/README.md b/README.md index c5cbc3ac..5be1765d 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,15 @@ steps: - run: echo ${{ steps.plan.outputs.exitcode }} ``` +Temporary directory could be deleted after all steps by setting the `cleanup_workspace` variable to `true`: + +```yaml +steps: +- uses: hashicorp/setup-terraform@v3 + with: + cleanup_workspace: true +``` + Outputs can be used in subsequent steps to comment on the pull request: > **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: - `terraform_wrapper` - (optional) Whether 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`. +- `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`. ## Outputs diff --git a/action.yml b/action.yml index 9009be1c..fa6d4d10 100644 --- a/action.yml +++ b/action.yml @@ -17,9 +17,15 @@ inputs: 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`.' 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`.' + default: 'false' + required: false runs: using: 'node20' main: 'dist/index.js' + post: 'dist/cleanup.js' + post-if: cleanup_workspace branding: icon: 'terminal' color: 'purple' diff --git a/dist/cleanup.js b/dist/cleanup.js new file mode 100644 index 00000000..789c829a --- /dev/null +++ b/dist/cleanup.js @@ -0,0 +1,37 @@ +/** + * Copyright (c) HashiCorp, Inc. + * SPDX-License-Identifier: MPL-2.0 + */ + +const fs = require('fs'); +const path = require('path'); + +// Retrieve environment variables and parameters +const terraformCliPath = process.env.TERRAFORM_CLI_PATH; + +// 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 (terraformCliPath) { + console.log(`Cleaning up directory: ${terraformCliPath}`); + deleteDirectoryRecursive(terraformCliPath); + console.log('Cleanup completed.'); +} else { + console.log('No cleanup required.'); +}