diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index b782d325e..7b3153fd4 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -238,19 +238,6 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error { } } - // Run `terraform init` before running other commands - runTerraformInit := true - if info.SubCommand == "init" || - info.SubCommand == "clean" || - (info.SubCommand == "deploy" && !cliConfig.Components.Terraform.DeployRunInit) { - runTerraformInit = false - } - - if info.SkipInit { - u.LogDebug(cliConfig, "Skipping over 'terraform init' due to '--skip-init' flag being passed") - runTerraformInit = false - } - // Set `TF_IN_AUTOMATION` ENV var to `true` to suppress verbose instructions after terraform commands // https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_in_automation info.ComponentEnvList = append(info.ComponentEnvList, "TF_IN_AUTOMATION=true") @@ -273,11 +260,28 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error { } } + // Run `terraform init` before running other commands + runTerraformInit := true + if info.SubCommand == "init" || + info.SubCommand == "clean" || + (info.SubCommand == "deploy" && !cliConfig.Components.Terraform.DeployRunInit) { + runTerraformInit = false + } + + if info.SkipInit { + u.LogDebug(cliConfig, "Skipping over 'terraform init' due to '--skip-init' flag being passed") + runTerraformInit = false + } + if runTerraformInit { initCommandWithArguments := []string{"init"} if info.SubCommand == "workspace" || cliConfig.Components.Terraform.InitRunReconfigure { initCommandWithArguments = []string{"init", "-reconfigure"} } + + // Before executing `terraform init`, delete the `.terraform/environment` file from the component directory + cleanTerraformWorkspace(componentPath) + err = ExecuteShellCommand( cliConfig, info.Command, @@ -359,6 +363,9 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error { allArgsAndFlags = append(allArgsAndFlags, []string{varFileFlag, varFile}...) } case "init": + // Before executing `terraform init`, delete the `.terraform/environment` file from the component directory + cleanTerraformWorkspace(componentPath) + if cliConfig.Components.Terraform.InitRunReconfigure { allArgsAndFlags = append(allArgsAndFlags, []string{"-reconfigure"}...) } diff --git a/internal/exec/terraform_utils.go b/internal/exec/terraform_utils.go index 2228208d0..db92103d6 100644 --- a/internal/exec/terraform_utils.go +++ b/internal/exec/terraform_utils.go @@ -1,6 +1,9 @@ package exec import ( + "os" + "path" + "github.com/pkg/errors" "github.com/cloudposse/atmos/pkg/schema" @@ -14,3 +17,12 @@ func checkTerraformConfig(cliConfig schema.CliConfiguration) error { return nil } + +// cleanTerraformWorkspace deletes the `.terraform/environment` file from the component directory. +// The `.terraform/environment` file contains the name of the currently selected workspace, +// helping Terraform identify the active workspace context for managing your infrastructure. +// We delete the file to prevent the Terraform prompt asking o select the default or the +// previously used workspace. This happens when different backends are used for the same component. +func cleanTerraformWorkspace(componentPath string) { + _ = os.Remove(path.Join(componentPath, ".terraform", "environment")) +} diff --git a/pkg/utils/file_utils.go b/pkg/utils/file_utils.go index 4c0d752a1..2cdb7204f 100644 --- a/pkg/utils/file_utils.go +++ b/pkg/utils/file_utils.go @@ -197,20 +197,21 @@ func SearchConfigFile(path string) (string, bool) { // IsURL checks if a string is a URL func IsURL(s string) bool { - url, err := url.Parse(s) + u, err := url.Parse(s) if err != nil { return false } + validSchemes := []string{"http", "https"} schemeValid := false for _, scheme := range validSchemes { - if url.Scheme == scheme { + if u.Scheme == scheme { schemeValid = true break } } - return schemeValid + return schemeValid } // GetFileNameFromURL extracts the file name from a URL @@ -218,16 +219,19 @@ func GetFileNameFromURL(rawURL string) (string, error) { if rawURL == "" { return "", fmt.Errorf("empty URL provided") } + parsedURL, err := url.Parse(rawURL) if err != nil { return "", err } + // Extract the path from the URL urlPath := parsedURL.Path + + // Get the base name of the path fileName := path.Base(urlPath) if fileName == "/" || fileName == "." { return "", fmt.Errorf("unable to extract filename from URL: %s", rawURL) } - // Get the base name of the path return fileName, nil } diff --git a/website/package-lock.json b/website/package-lock.json index 8ffdc2112..42286b623 100644 --- a/website/package-lock.json +++ b/website/package-lock.json @@ -30,7 +30,7 @@ "marked": "^14.1.3", "node-fetch": "^3.3.2", "posthog-docusaurus": "^2.0.1", - "posthog-js": "^1.178.0", + "posthog-js": "^1.180.1", "prism-react-renderer": "^2.4.0", "raw-loader": "^4.0.2", "react": "^18.3.1", @@ -13652,9 +13652,9 @@ } }, "node_modules/posthog-js": { - "version": "1.178.0", - "resolved": "https://registry.npmjs.org/posthog-js/-/posthog-js-1.178.0.tgz", - "integrity": "sha512-ILD4flNh72d5dycc4ZouKORlaVr+pDzl5TlZr1JgP0NBAoduHjhE7XZYwk7zdYkry7u0qAIpfv2306zJCW2vGg==", + "version": "1.180.1", + "resolved": "https://registry.npmjs.org/posthog-js/-/posthog-js-1.180.1.tgz", + "integrity": "sha512-LV65maVrpqkAh0wu32YvU7FpCSEjg6o+sZFYCs1+6tnEa9VvXuz8J6ReLiyRpJABI4j1qX/PB2jaVY5tDbLalQ==", "license": "MIT", "dependencies": { "core-js": "^3.38.1", diff --git a/website/package.json b/website/package.json index bdec0e610..6fc1cddcd 100644 --- a/website/package.json +++ b/website/package.json @@ -36,7 +36,7 @@ "marked": "^14.1.3", "node-fetch": "^3.3.2", "posthog-docusaurus": "^2.0.1", - "posthog-js": "^1.178.0", + "posthog-js": "^1.180.1", "prism-react-renderer": "^2.4.0", "raw-loader": "^4.0.2", "react": "^18.3.1",