Skip to content

Commit

Permalink
feat: use preinstalled Terraform if available with specific version (#…
Browse files Browse the repository at this point in the history
…179)

<!--

Unless this is a very simple 1-line-of-code change, please create a new
issue describing the change you're proposing first, then link to it from
this PR.

Read more about our process in our contributing guide:
https://github.com/hashicorp/terraform-cdk-action/blob/main/.github/CONTRIBUTING.md

-->

### Related issue

Fixes #178 

### Description

Action will check availability of Terraform in PATH with version
configured to action. If there's suitable version available, it doesn't
download Terraform.

### Checklist

- [x] I have updated the PR title to match [CDKTF's style
guide](https://github.com/hashicorp/terraform-cdk-action/blob/main/.github/CONTRIBUTING.md#pull-requests-1)
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works if applicable
- [x] New and existing unit tests pass locally with my changes

<!-- If this is still a work in progress, feel free to open a draft PR
until you're able to check off all the items on the list above -->
  • Loading branch information
Hi-Fi committed May 6, 2024
1 parent d10c37f commit 1ee9030
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
30 changes: 29 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions src/setup-terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Removed the credentials and wrapper part, we don't need them we solve it through CDKTF directly

// Node.js core
import { execSync } from "child_process";
import * as os from "os";

// External
Expand Down Expand Up @@ -60,7 +61,40 @@ async function downloadCLI(url: string) {
return pathToCLI;
}

/**
* Check if wanted version of Terraform is already available to prevent extra downloads
*
* @param version expected version of Terraform
*/
async function checkVersionAvailability(version: string): Promise<boolean> {
try {
const terraformVersion = JSON.parse(
execSync("terraform version -json", {
encoding: "utf8",
// Don't print out error if command is not found
stdio: "pipe",
})
).terraform_version;
if (terraformVersion !== version) {
core.debug(
`Installed Terraform version is ${terraformVersion} while expecting ${version}`
);
return false;
}
core.debug(
`Configured Terraform version ${terraformVersion} already available in PATH`
);
return true;
} catch (e) {
core.debug("Terraform installation not found from PATH");
return false;
}
}

export async function setupTerraform(version: string) {
if (await checkVersionAvailability(version)) {
return;
}
// Gather OS details
const osPlatform = os.platform();
const osArch = os.arch();
Expand Down

0 comments on commit 1ee9030

Please sign in to comment.