Skip to content

Commit

Permalink
Ensure workflow status failure if command fails in backendless mode
Browse files Browse the repository at this point in the history
Error handling in backendless mode was incorrect, resulting in a
workflow exit status of success even when command execution failed.

The fix points are as follows:
- `ReportErrorAndExit()` should be called even if `allAppliesSuccessful`
  is false.
- Because `ReportErrorAndExit()` invokes `os.Exit()` immediately, it
  should be called after setting the status of the pull request.
- Contrary to intuition, `atLeastOneApply` is counted even if plan
  command, so we need to use `scheduler.IsPlanJobs()` to determine if
  the current workflow is plan or apply.
  • Loading branch information
minamijoyo committed Oct 10, 2024
1 parent bbc1931 commit 82646f0
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions cli/pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,16 @@ func GitHubCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh
jobs = digger.SortedCommandsByDependency(jobs, &dependencyGraph)

allAppliesSuccessful, atLeastOneApply, err := digger.RunJobs(jobs, &githubPrService, &githubPrService, lock, reporter, planStorage, policyChecker, comment_updater.NoopCommentUpdater{}, backendApi, "", false, false, "0", currentDir)
if err != nil {
usage.ReportErrorAndExit(githubActor, fmt.Sprintf("Failed to run commands. %s", err), 8)
if !allAppliesSuccessful || err != nil {
// aggregate status checks: failure
if allAppliesSuccessful {
if atLeastOneApply {
githubPrService.SetStatus(prNumber, "failure", "digger/apply")
} else {
if !allAppliesSuccessful {
if scheduler.IsPlanJobs(jobs) {
githubPrService.SetStatus(prNumber, "failure", "digger/plan")
} else {
githubPrService.SetStatus(prNumber, "failure", "digger/apply")
}
}
usage.ReportErrorAndExit(githubActor, fmt.Sprintf("Failed to run commands. %s", err), 8)
}

if diggerConfig.AutoMerge && allAppliesSuccessful && atLeastOneApply && coversAllImpactedProjects {
Expand All @@ -295,10 +295,10 @@ func GitHubCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh

if allAppliesSuccessful {
// aggreate status checks: success
if atLeastOneApply {
githubPrService.SetStatus(prNumber, "success", "digger/apply")
} else {
if scheduler.IsPlanJobs(jobs) {
githubPrService.SetStatus(prNumber, "success", "digger/plan")
} else {
githubPrService.SetStatus(prNumber, "success", "digger/apply")
}
}

Expand Down

0 comments on commit 82646f0

Please sign in to comment.