mirror of
https://github.com/hashicorp/setup-terraform.git
synced 2025-12-26 04:53:38 +00:00
Merge 7b7ad1617b into e12a01440e
This commit is contained in:
commit
faf2504721
6 changed files with 26015 additions and 1 deletions
11
README.md
11
README.md
|
|
@ -75,6 +75,15 @@ steps:
|
||||||
- run: echo ${{ steps.plan.outputs.exitcode }}
|
- 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:
|
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).
|
> **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
|
- `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
|
the `terraform` binary and expose its STDOUT, STDERR, and exit code as outputs
|
||||||
named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.
|
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
|
## Outputs
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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`.'
|
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'
|
default: 'true'
|
||||||
required: false
|
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:
|
runs:
|
||||||
using: 'node20'
|
using: 'node20'
|
||||||
main: 'dist/index.js'
|
main: 'dist/index.js'
|
||||||
|
post: 'cleanup/dist/index.js'
|
||||||
branding:
|
branding:
|
||||||
icon: 'terminal'
|
icon: 'terminal'
|
||||||
color: 'purple'
|
color: 'purple'
|
||||||
|
|
|
||||||
42
cleanup/cleanup.js
Normal file
42
cleanup/cleanup.js
Normal file
|
|
@ -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();
|
||||||
25920
cleanup/dist/index.js
vendored
Normal file
25920
cleanup/dist/index.js
vendored
Normal file
File diff suppressed because one or more lines are too long
36
cleanup/test/cleanup.test.js
Normal file
36
cleanup/test/cleanup.test.js
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) HashiCorp, Inc.
|
||||||
|
* SPDX-License-Identifier: MPL-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { execSync } = require('child_process');
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const assert = require('assert');
|
||||||
|
|
||||||
|
describe('Cleanup Test Suite', () => {
|
||||||
|
it('post-jobs cleanup step', async () => {
|
||||||
|
// Create test directory structure
|
||||||
|
const testDir = 'testDir';
|
||||||
|
if (!fs.existsSync(testDir)) {
|
||||||
|
fs.mkdirSync(testDir, { recursive: true });
|
||||||
|
}
|
||||||
|
fs.writeFileSync(path.join(testDir, 'testFile.txt'), 'test content');
|
||||||
|
|
||||||
|
// Call your cleanup function
|
||||||
|
execSync('node cleanup/cleanup', {
|
||||||
|
env: {
|
||||||
|
...process.env,
|
||||||
|
TERRAFORM_CLI_PATH: testDir,
|
||||||
|
INPUT_CLEANUP_WORKSPACE: 'true'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test assertions
|
||||||
|
try {
|
||||||
|
assert.strictEqual(fs.existsSync(testDir), false, 'Directory should be deleted');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Test failed:', error.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "semistandard --env jest && jest --coverage",
|
"test": "semistandard --env jest && jest --coverage",
|
||||||
"lint": "semistandard --env jest --fix",
|
"lint": "semistandard --env jest --fix",
|
||||||
"build": "ncc build wrapper/terraform.js --out wrapper/dist && ncc build index.js --out dist",
|
"build": "ncc build cleanup/cleanup.js --out cleanup/dist && ncc build wrapper/terraform.js --out wrapper/dist && ncc build index.js --out dist",
|
||||||
"prepare": "husky install",
|
"prepare": "husky install",
|
||||||
"format-check": "echo \"unimplemented for actions/reusable-workflows basic-validation\""
|
"format-check": "echo \"unimplemented for actions/reusable-workflows basic-validation\""
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue