Deploy to server after merge to master branch
One could use conditional builds https://docs.travis-ci.com/user/conditional-builds-stages-jobs/

If code is merged into master one could decide to deploy code to production, but I personally prefer a human intervention by a Product Owner.


jobs:
  include:
      if: branch = master



or


stages:
  - name: deploy
    # require the branch name to be master
    if: branch = master



At other projects I preferred to deploy only tags to enforce the git flow (tags should be created otherwise no deployment to production).


stages:
  - name: deploy
    # require the tag name to match a regular expression
    if: tag =~ ^v1



One could also try (I did not try it yet) whether the if works in combination with script sections:

https://github.com/030/ansible-firefox/blob/master/.travis.yml

Last remark: I would avoid creating such scripts (the ones you included in the question) as most CI tools contain condition checks.