Terminate makefile command when subcommand throws?

makefile, shell

Solution

The exit status of a shell command list is the exit status of the last command in the list. Simply turn your command list into separate single simple commands. By default, `make` stops when a command returns nonzero. So you get what you want with

deploy:
    @make test
    make deploy-git
    make deploy-servers

Should you ever want to ignore the exit status of a simple command, you can prefix it with a dash:

 target:
     cmd1
     -cmd2 # It is okay if this fails
     cmd3

Your `make` manual has all the details.

Problem

I've got the following Makefile: ``` #runs the working directory unit tests test: @NODE_ENV=test; \ mocha --ignore-leaks $(shell find ./test -name \*test.js); #deploys working directory deploy: @make test; \ make deploy-git; \ make deploy-servers; #deploys working to git deployment branch deploy-git: @status=$$(git status --porcelain); \ if test "x$${status}" = x; then \ git branch -f deployment; \ git push origin deployment; \ echo "Done deploying to git deployment branch."; \ else \ git status; \ echo "Error: cannot deploy. Working directory is dirty."; \ fi deploy-servers: # for each server # @DEPLOY_SERVER_IP = "127.0.0.1"; \ # make deploy-server #deploy-server: # connect to this server with ssh # check if app is already running # stop the app on the server if already running # set working directory to app folder # update deployment git branch # use git to move head to deployment branch # start app again ``` Note that `deploy-servers` and `deploy-server` are just dummies for now. This is what the `deploy` command should do: - run the tests (`make test`), exit on failure - push current head to deployment branch (`make deploy-git`), exit on failure - pull deployment branch on servers (`make deploy-servers`) You can see this in the Makefile as: ``` deploy: @make test; \ make deploy-git; \ make deploy-servers; ``` The issue is that I am not sure how to prevent `make deploy-git` from executing when `make test` fails, and how to prevent `make deploy-servers` from executing when the tests fail or when `make deploy-git` fails. Is there a clear way to do this, or should I resort to using shell files or write these tools in a normal programming language?

Original source