<p>Turns out this was not a real issue. My apologies. At the end of that shell script, it did an echo which is what was returning the non-zero status code. After I took that out, it worked as expected.</p>

CC BY-SA 3.0
2017-11-23T14:04:05.663Z
shouldn't jenkins fail is any command in 'execute shell' returns non-zero?
xddsg
CC BY-SA 3.0
2017-04-12T20:49:43.497Z
2017-04-12T20:49:43.497Z
Dan Mandle
<p>I'm using Terraform as part of a Jenkins Pipeline. When <code>terraform apply</code> encounters an error, it does not return a non-zero exit code. When running into a problem on Jenkins, it does not stop the pipeline or mark the build as FAILURE.</p>

<p>If terraform encounters an error, I need the job to stop and not progress. </p>

<p>Looking at the <a href="https://www.terraform.io/docs/commands/apply.html" rel="nofollow noreferrer">docs for <code>apply</code></a>, it doesn't look like I can provide a flag to return helpful exit codes. The <a href="https://www.terraform.io/docs/commands/plan.html" rel="nofollow noreferrer">docs for <code>plan</code></a> has a <code>-detailed-exitcode</code> flag, but doesn't look like that'll help me.</p>

CC BY-SA 3.0
2017-04-12T19:47:44.813Z
Would sidestepping the issue by running `terraform apply | tee /dev/tty | ( ! grep "Error applying plan" )` work? That will return 1 if the "Error applying plan" text is in the output. Let me know if that's a suitable answer for you, or if you're looking for a *more graceful* solution.
Aurora0001
CC BY-SA 3.0
2017-04-12T19:54:44.957Z
Thanks for your response. That would do the job, but it's a little more of a hack than what I was hoping for.
Dan Mandle
<jenkins><terraform>
Terraform apply exit code on error
Dan Mandle
# Terraform apply exit code on error

(Asked by: username_0 on 2017-04-12)

I'm using Terraform as part of a Jenkins Pipeline. When `terraform apply` encounters an error, it does not return a non-zero exit code. When running into a problem on Jenkins, it does not stop the pipeline or mark the build as FAILURE.

If terraform encounters an error, I need the job to stop and not progress.

Looking at the [docs for `apply`](https://www.terraform.io/docs/commands/apply.html), it doesn't look like I can provide a flag to return helpful exit codes. The [docs for `plan`](https://www.terraform.io/docs/commands/plan.html) has a `-detailed-exitcode` flag, but doesn't look like that'll help me.

**Tags:** jenkins, terraform

> **Comments:**
> **username_1**: Would sidestepping the issue by running `terraform apply | tee /dev/tty | ( ! grep "Error applying plan" )` work? That will return 1 if the "Error applying plan" text is in the output. Let me know if that's a suitable answer for you, or if you're looking for a *more graceful* solution.  
> **username_0**: Thanks for your response. That would do the job, but it's a little more of a hack than what I was hoping for.  

---

## 1 answers

### Answer by username_0 on 2017-04-12. Score: 3

Turns out this was not a real issue. My apologies. At the end of that shell script, it did an echo which is what was returning the non-zero status code. After I took that out, it worked as expected.

> **Comments:**
> **username_2**: shouldn't jenkins fail is any command in 'execute shell' returns non-zero?  

---

