|
| 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(); |
0 commit comments