Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix exit code for each exec command #1065

Merged
merged 20 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
609b722
fix exit code for each exec command
samtholiya Feb 14, 2025
a72b78c
add tests
samtholiya Feb 14, 2025
a97c72f
updated test cases
samtholiya Feb 15, 2025
34d9a73
add more test cases for exit codes
samtholiya Feb 15, 2025
2b0354d
Update tests/test-cases/exec-command.yaml
samtholiya Feb 15, 2025
4296c2b
Update tests/fixtures/scenarios/exitCode/components/terraform/hook-an…
samtholiya Feb 15, 2025
a897749
Update tests/fixtures/scenarios/exitCode/components/terraform/hook-an…
samtholiya Feb 15, 2025
a10719a
updated test cases
samtholiya Feb 16, 2025
954352d
Merge branch 'main' into feature/dev-3051-fix-terraform-exit-code-mis…
aknysh Feb 17, 2025
db7efcf
update test
samtholiya Feb 17, 2025
210a477
Update tests/fixtures/scenarios/exitCode/components/terraform/hook-an…
samtholiya Feb 17, 2025
6f6b236
update test
samtholiya Feb 17, 2025
f04691a
Merge branch 'feature/dev-3051-fix-terraform-exit-code-mismatch' of h…
samtholiya Feb 18, 2025
4afbabf
Merge branch 'main' into feature/dev-3051-fix-terraform-exit-code-mis…
samtholiya Feb 18, 2025
b9bbbf9
update the folder structure
samtholiya Feb 19, 2025
773a516
Merge branch 'feature/dev-3051-fix-terraform-exit-code-mismatch' of h…
samtholiya Feb 19, 2025
13d0679
Merge branch 'main' into feature/dev-3051-fix-terraform-exit-code-mis…
samtholiya Feb 19, 2025
8f9d431
Move to exit-code
samtholiya Feb 19, 2025
5b48333
Merge branch 'feature/dev-3051-fix-terraform-exit-code-mismatch' of h…
samtholiya Feb 19, 2025
1fddf1b
Merge branch 'main' into feature/dev-3051-fix-terraform-exit-code-mis…
samtholiya Feb 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/utils/markdown_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
package utils

import (
"errors"
"fmt"
"os"
"os/exec"
"runtime/debug"

"github.com/cloudposse/atmos/pkg/schema"
Expand Down Expand Up @@ -66,6 +68,13 @@ func PrintfErrorMarkdown(format string, a ...interface{}) {

func PrintErrorMarkdownAndExit(title string, err error, suggestion string) {
PrintErrorMarkdown(title, err, suggestion)

// Find the executed command's exit code from the error
var exitError *exec.ExitError
if errors.As(err, &exitError) {
os.Exit(exitError.ExitCode())
}

os.Exit(1)
}

Expand Down
21 changes: 21 additions & 0 deletions tests/fixtures/scenarios/exitCode/atmos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
base_path: "./"

components:
terraform:
base_path: "components/terraform"
apply_auto_approve: false
deploy_run_init: true
init_run_reconfigure: true
auto_generate_backend_file: false

stacks:
base_path: "."
included_paths:
- "stacks/**/*"
excluded_paths:
- "**/_defaults.yaml"
name_pattern: "{stage}"

logs:
file: "/dev/stderr"
level: Info
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "stage" {
description = "Stage where it will be deployed"
type = string
}

terraform {
required_version = ">= 0.12"
}

# Invalid resource block with a missing argument to cause terraform plan to fail
resource "null_resource" "example" {
provisioner "local-exec" {
command = "echo ${var.stage}"
}
}
7 changes: 7 additions & 0 deletions tests/fixtures/scenarios/exitCode/stacks/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
components:
terraform:
component1:
metadata:
component: hook-and-store
vars:
stage: test
16 changes: 16 additions & 0 deletions tests/test-cases/exec-command.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tests:
- name: atmos exit code should be same as command exit code
enabled: true
description: "Ensure the exit code equals the command exit code"
workdir: "fixtures/scenarios/exitCode"
command: "atmos"
args:
- terraform
- plan
- component1
- "-s"
- test
- "--"
- "-detailed-exitcode"
expect:
exit_code: 2
Loading