Need help to prevent duplicate builds for pull_request and push

Hello!

I am facing a problem where every push triggers a build and every pull request update also triggers a build. With this, whenever a new push is made to a pull request, two build processes start: one for updating the pull request, and one for making a push. Both executing duplicate stages: build and test.

I am trying to get around that by using the commit_message conditional, but still can’t get it to work as I want.

What I want:

  • if the commit message contains the word #ci-test, run stages: build and test.
  • if a pull request is updated or created, run stages: build, test and deploy to dev.
  • if a push is made to the master branch, run stages: build, test and deploy to prod.

My (simplified) .travis.yml file is:

group: vm
services:
  - docker

git:
  depth: false

env:
  global:
    - MYVAR="VARVALUE"

jobs:
  include:
    - stage: "build"
      if: type = pull_request OR commit_message =~ /#ci-test/
      script: build_docker_image.sh

    - stage: "tests"
      if: type = pull_request OR commit_message =~ /#ci-test/
      script: run_tests.sh

    - stage: "deploy to dev"
      if: type = pull_request
      script: do_the_deploy_to_dev_magic.sh

    - stage: "deploy to production"
      if: type = push AND branch = master
      script: do_the_deploy_to_prod_magic.sh

Before I added the commit_message condition, it was running the jobs as expected, but only for pull request updates and for push to master branch. Pushes to other branches didn’t trigger anything, as expected. After adding the commit_message condition, it started triggering the jobs for every push, in any branch, again.

Another doubt I have is: Does commit_message condition check run only for the latest commit message, or does it check every commit message of a branch/push ?

Can anyone help me to figure out what am I doing wrong?

Thanks in advance.