Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add post-job step for cleaning temp dir #384

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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 controls whether to clean that directory. Defaults to `false`.

## Outputs

Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ 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 controls whether to clean that directory. Defaults to `false`.'
default: 'false'
required: false
runs:
using: 'node20'
main: 'dist/index.js'
post: 'cleanup/dist/index.js'
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