Skip to content

Commit

Permalink
chore!: Require setup-terraform to be run before action (#255)
Browse files Browse the repository at this point in the history
<!--

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 #243 

### Description

This PR makes it so that this action doesn't install Terraform
automatically. The expectation is that terraform with the specific
version is already installed on the runner. This can be done with the
[setup-terraform
action](https://github.com/marketplace/actions/hashicorp-setup-terraform).
The examples are updated to explain the usage.

### Checklist

- [ ] 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)
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works if applicable
- [ ] 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 -->

---------

Signed-off-by: team-tf-cdk <[email protected]>
Co-authored-by: team-tf-cdk <[email protected]>
Co-authored-by: Ansgar Mertens <[email protected]>
  • Loading branch information
3 people authored Jan 23, 2025
1 parent 2c45f1e commit 93a2633
Show file tree
Hide file tree
Showing 7 changed files with 24,042 additions and 46,332 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ jobs:
with:
node-version: 20

- use: actions/setup-terraform@v3
with:
terraform_version: 1.10.4

- name: Install dependencies
run: yarn install

Expand Down Expand Up @@ -96,6 +100,10 @@ jobs:
with:
node-version: 20

- use: actions/setup-terraform@v3
with:
terraform_version: 1.10.4

- name: Install dependencies
run: yarn install

Expand Down Expand Up @@ -140,6 +148,10 @@ jobs:
with:
node-version: 20

- use: actions/setup-terraform@v3
with:
terraform_version: 1.10.4

- name: Install dependencies
run: yarn install

Expand Down
69,193 changes: 24,020 additions & 45,173 deletions dist/index.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

1,074 changes: 0 additions & 1,074 deletions dist/licenses.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions scripts/update-terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ CI=0 npx projen
npx projen build

echo "Updating README"
sed -i 's/terraform_version: .*/terraform_version: '"$TERRAFORM_VERSION"'/' "$PROJECT_ROOT/README.md"
sed -i 's/terraformVersion: .*/terraformVersion: '"$TERRAFORM_VERSION"'/' "$PROJECT_ROOT/README.md"
6 changes: 3 additions & 3 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as github from "@actions/github";
import { CommentController } from "./comment";
import * as input from "./inputs";
import { Inputs } from "./models";
import { setupTerraform } from "./setup-terraform";
import { ensureTerraform } from "./setup-terraform";

function hasTerraformChanges(output: string): Boolean {
return !output.includes(
Expand Down Expand Up @@ -76,8 +76,8 @@ async function execute(
runUrl?: string
) => Promise<void>
): Promise<void> {
core.debug(`Installing terraform`);
await setupTerraform(inputs.terraformVersion);
core.debug(`Checking if terraform is installed`);
await ensureTerraform(inputs.terraformVersion);

const mainCommand = `${cdktfCommand} ${inputs.cdktfArgs}`;
const fullCdktfCommand = inputs.customNpxArgs
Expand Down
86 changes: 5 additions & 81 deletions src/setup-terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,12 @@

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

// External
import * as core from "@actions/core";
import * as io from "@actions/io";
import * as tc from "@actions/tool-cache";
import * as releases from "@hashicorp/js-releases";

// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
// return value in [amd64, 386, arm]
function mapArch(arch: string) {
const mappings: Record<string, string> = {
x32: "386",
x64: "amd64",
};
return mappings[arch] || arch;
}

// os in [darwin, linux, win32...] (https://nodejs.org/api/os.html#os_os_platform)
// return value in [darwin, linux, windows]
function mapOS(operatingSystem: string) {
const mappings: Record<string, string> = {
win32: "windows",
};
return mappings[operatingSystem] || operatingSystem;
}

async function downloadCLI(url: string) {
core.debug(`Downloading Terraform CLI from ${url}`);
const pathToCLIZip = await tc.downloadTool(url);

let pathToCLI = "";

core.debug("Extracting Terraform CLI zip file");
if (os.platform().startsWith("win")) {
core.debug(`Terraform CLI Download Path is ${pathToCLIZip}`);
const fixedPathToCLIZip = `${pathToCLIZip}.zip`;
await io.mv(pathToCLIZip, fixedPathToCLIZip);
core.debug(`Moved download to ${fixedPathToCLIZip}`);
pathToCLI = await tc.extractZip(fixedPathToCLIZip);
} else {
pathToCLI = await tc.extractZip(pathToCLIZip);
}

core.debug(`Terraform CLI path is ${pathToCLI}.`);

if (!pathToCLIZip || !pathToCLI) {
throw new Error(`Unable to download Terraform from ${url}`);
}

return pathToCLI;
}

/**
* Check if wanted version of Terraform is already available to prevent extra downloads
* Check if wanted version of Terraform is already available
*
* @param version expected version of Terraform
*/
Expand All @@ -75,6 +26,7 @@ async function checkVersionAvailability(version: string): Promise<boolean> {
stdio: "pipe",
})
).terraform_version;

if (terraformVersion !== version) {
core.debug(
`Installed Terraform version is ${terraformVersion} while expecting ${version}`
Expand All @@ -91,38 +43,10 @@ async function checkVersionAvailability(version: string): Promise<boolean> {
}
}

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

core.debug(`Finding releases for Terraform version ${version}`);
const release = await releases.getRelease(
"terraform",
version,
"GitHub Action: Terraform CDK"
);
const platform = mapOS(osPlatform);
const arch = mapArch(osArch);
core.debug(
`Getting build for Terraform version ${release.version}: ${platform} ${arch}`
);
const build = release.getBuild(platform, arch);
if (!build) {
export async function ensureTerraform(version: string) {
if (!(await checkVersionAvailability(version))) {
throw new Error(
`Terraform version ${version} not available for ${platform} and ${arch}`
`Terraform not installed, please use the setup-terraform action with the specified version to install terraform. You can find it at github.com/hashicorp/setup-terraform`
);
}

// Download requested version
const pathToCLI = await downloadCLI(build.url);
// cache directory
const cachedCLIPath = await tc.cacheDir(pathToCLI, "terraform", version);
// Add to path
core.addPath(cachedCLIPath);

return cachedCLIPath;
}

0 comments on commit 93a2633

Please sign in to comment.