For the past few years I’ve come to rely on Glenn Gillen’s plugin to get the exit code from processes executed by heroku run. Well colour me behine the times, but Heroku resolved this issue in May 2015.

The Heroku run command:

$ heroku run echo "Hello Foo!"
Running echo "Hello Foo!" on ⬢ my-app... up, run.1847
Hello Foo!
$ echo $?
0

All good, everything ran as expected. Now let’s try to run an unknown command:

$ heroku run echo_foo "Hello Foo!"
Running echo_foo "Hello Foo!" on ⬢ my-app... up, run.5157
bash: echo_foo: command not found
$ echo $?
0

As you can see, $? is 0 but that’s not the exit code of the process that ran on the dyno, instead that’s the exit code of the heroku CLI. However, it is now possible to get the exit code of the process from the dyno using --exit-code:

$ heroku run echo_foo "Hello Foo!"
Running echo_foo "Hello Foo!" on ⬢ my-app... up, run.7529
bash: echo_foo: command not found
 ▸    Process exited with code 127
$ echo $?
127

I know I’m late to this particular party, but still, yay! 🎉