diff --git a/core/src/plugins/terraform/common.ts b/core/src/plugins/terraform/common.ts index 716647b54f..4222cba5e8 100644 --- a/core/src/plugins/terraform/common.ts +++ b/core/src/plugins/terraform/common.ts @@ -68,7 +68,7 @@ export async function tfValidate(params: TerraformParams) { ) { // We need to run `terraform init` and retry validation log.debug("Initializing Terraform") - await terraform(ctx, provider).exec({ log, args: ["init"], cwd: root, timeoutSec: 300 }) + await tfInit(params) const retryRes = await terraform(ctx, provider).json({ log, @@ -175,10 +175,10 @@ export async function getStackStatus(params: TerraformParamsWithVariables): Prom export async function applyStack(params: TerraformParamsWithVariables) { const { ctx, log, provider, root, variables } = params - const args = ["apply", "-auto-approve", "-input=false", ...(await prepareVariables(root, variables))] await setWorkspace(params) + const args = ["apply", "-auto-approve", "-input=false", ...(await prepareVariables(root, variables))] const proc = await terraform(ctx, provider).spawn({ log, args, cwd: root }) const statusLine = log.info("→ Applying Terraform stack...") @@ -241,7 +241,12 @@ export async function prepareVariables(targetDir: string, variables?: object): P /** * Lists the created workspaces for the given Terraform `root`, and returns which one is selected. */ -export async function getWorkspaces({ ctx, log, provider, root }: TerraformParams) { +export async function getWorkspaces(params: TerraformParams) { + const { ctx, log, provider, root } = params + + // Must in some cases ensure init is complete before listing workspaces + await tfInit(params) + const res = await terraform(ctx, provider).stdout({ args: ["workspace", "list"], cwd: root, log }) let selected = "default" @@ -287,3 +292,7 @@ export async function setWorkspace(params: TerraformParamsWithWorkspace) { await terraform(ctx, provider).stdout({ args: ["workspace", "new", workspace], cwd: root, log }) } } + +export async function tfInit({ ctx, log, provider, root }: TerraformParams) { + await terraform(ctx, provider).exec({ log, args: ["init"], cwd: root, timeoutSec: 600 }) +} diff --git a/core/test/unit/src/plugins/terraform/common.ts b/core/test/unit/src/plugins/terraform/common.ts index 08556b1a89..f9faa529b9 100644 --- a/core/test/unit/src/plugins/terraform/common.ts +++ b/core/test/unit/src/plugins/terraform/common.ts @@ -20,6 +20,7 @@ import { terraform } from "../../../../../src/plugins/terraform/cli" describe("Terraform common", () => { const testRoot = getDataDir("test-projects", "terraform-provider") const root = join(testRoot, "tf") + const terraformDirPath = join(root, ".terraform") const stateDirPath = join(root, "terraform.tfstate.d") const testFilePath = join(root, "test.log") @@ -29,6 +30,9 @@ describe("Terraform common", () => { let provider: TerraformProvider async function reset() { + if (await pathExists(terraformDirPath)) { + await remove(terraformDirPath) + } if (await pathExists(testFilePath)) { await remove(testFilePath) }