# Conditional builds with 1 "Travis CI - Pull-Request" in pull-request and push from any branches?

**URL:** <https://travis-ci.community/t/conditional-builds-with-1-travis-ci-pull-request-in-pull-request-and-push-from-any-branches/11673>\
**Category:** Travis CI Discussions & Feedback\
**Created:** [June 3, 2021, 2:03pm UTC](https://travis-ci.community/t/conditional-builds-with-1-travis-ci-pull-request-in-pull-request-and-push-from-any-branches/11673 "2021-06-03T14:03:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![junaruga](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/junaruga/32/8943_2.png) [@junaruga](https://travis-ci.community/u/junaruga)\
**Post date:** [June 3, 2021, 2:03pm UTC](https://travis-ci.community/t/conditional-builds-with-1-travis-ci-pull-request-in-pull-request-and-push-from-any-branches/11673/1 "2021-06-03T14:03:52Z")

</div>

I am looking for how to run Travis conditionally by following conditions.

1. When at the pull-request, as a default behavior without `branches` syntax and without `if` syntax, the both “Travis CI - Branch” and "Travis - Pull Request ‘’ are executed. The following screen shot shows the status. But I want to enable only “Travis CI - Pull Request” excluding “Travis CI - Branch” build at the pull-request, because I want to see the result from `https://travis-ci.com/github/<name>/<repo>/pull_requests`.

2. I want to enable Travis when a commit is pushed or when it is executed on cron mode to specific branches such as `master`.

 ![screenshot-github.com-2021.06.03-15_55_15](https://us1.discourse-cdn.com/flex015/uploads/travis_ci/original/2X/7/715a8e32d9d0b5535c1553644327908ca93a64f2.png)

1. I want to enable Travis when a commit is pushed from any branches on the “forked” repository.

Do you have any idea how to implement this condition in `.travis.yml`?

Maybe we need to use `if` syntax on the top level.

> **[Travis CI Documentation](https://docs.travis-ci.com/user/conditions-v1)**
>
> Conditions

Here is a related past discussion about this topic. However, the mentioned `branches - only` syntax does not cover my case, because it does not enable Travis at the push timing.

> <https://github.com/travis-ci/travis-ci/issues/1147#issuecomment-160820262>
>
> I understand that when testing PR travis actually checks how it would be when me…rged to master and when testing a branch it just pulls that branch. This is good.
> 
> However, in some cases the branch is already rebased against master, so no merge needed and travis ends up building the same commit twice.
> 
> We have that happening a lot in our travis pro account. Would be nice if travis could be smarter about this and only build the same commit once.

Thanks.

---

<div class="post-metadata">

**Author:** ![junaruga](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/junaruga/32/8943_2.png) [@junaruga](https://travis-ci.community/u/junaruga)\
**Post date:** [June 3, 2021, 3:32pm UTC](https://travis-ci.community/t/conditional-builds-with-1-travis-ci-pull-request-in-pull-request-and-push-from-any-branches/11673/2 "2021-06-03T15:32:25Z")

</div>

Here is one example possibly to avoid one of the duplicated builds on the pull-request timing. But in my case I want to enable Travis at the push to master branch. So, maybe I need a different condition.

> <https://github.com/simd-everywhere/simde/blob/c29f9fbe9f0dc17f635c2e605376897c47167aa7/.travis.yml#L8>

```auto
if: branch != master OR type == pull_request

```

---

<div class="post-metadata">

**Author:** ![junaruga](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/junaruga/32/8943_2.png) [@junaruga](https://travis-ci.community/u/junaruga)\
**Post date:** [June 3, 2021, 4:06pm UTC](https://travis-ci.community/t/conditional-builds-with-1-travis-ci-pull-request-in-pull-request-and-push-from-any-branches/11673/3 "2021-06-03T16:06:31Z")

</div>

The core challenge is how to distinguish the “Travis CI - Branch” build to master branch at the pull-request from another build by pushing a commit to master branch at push by a condition.

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [June 4, 2021, 7:42am UTC](https://travis-ci.community/t/conditional-builds-with-1-travis-ci-pull-request-in-pull-request-and-push-from-any-branches/11673/4 "2021-06-04T07:42:44Z")

</div>

> [@junaruga](#):
>
> But I want to enable only “Travis CI - Pull Request” excluding “Travis CI - Branch” build at the pull-request

In Github project settings → Branches → Branch protection rules, disable the “Branch” status check.

> [@junaruga](#):
>
> I want to enable Travis when a commit is pushed or when it is executed on cron mode to specific branches such as `master` .

Didn’t quite get that. In combination with the previous point, looks like you just want to run all the kinds of builds, but only for the branch `master`:

```auto
if: branch = master

```

> [@junaruga](#):
>
> I want to enable Travis when a commit is pushed from any branches on the “forked” repository.

```auto
if: repo != project/repo

```

* * *

So combined, that’ll be:

```auto
if: branch = master OR repo != project/repo

```

---

<div class="post-metadata">

**Author:** ![junaruga](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/junaruga/32/8943_2.png) [@junaruga](https://travis-ci.community/u/junaruga)\
**Post date:** [June 7, 2021, 4:20pm UTC](https://travis-ci.community/t/conditional-builds-with-1-travis-ci-pull-request-in-pull-request-and-push-from-any-branches/11673/5 "2021-06-07T16:20:14Z")

</div>

@native-api Thanks for your help!

> In Github project settings → Branches → Branch protection rules, disable the “Branch” status check.

The following testing repo to show the screen shot picture did not have the protected branches. So, I am not sure if it is related to excluding “Travis CI - Branch”.

> **[GitHub - junaruga/ci-multi-arch-native-test](https://github.com/junaruga/ci-multi-arch-native-test)**
>
> Contribute to junaruga/ci-multi-arch-native-test development by creating an account on GitHub.

> Didn’t quite get that. In combination with the previous point, looks like you just want to run all the kinds of builds, but only for the branch `master`:

Yes, right for the all kind of builds. The actual case I am working on is the ruby/ruby repository. It’s not only `master` branch but also the `master` and `ruby_N_N` branches (version stable branches).

> ```auto
> if: repo != project/repo
> 
> ```
> 
> So combined, that’ll be:
> 
> ```auto
> if: branch = master OR repo != project/repo
> 
> ```

Here is what I did referring your idea. The `ruby/ruby` is `project/repo`. Here is The PR [Enable Travis on the specific branches or forked repositories. by junaruga · Pull Request #4556 · ruby/ruby · GitHub](https://github.com/ruby/ruby/pull/4556) as a reference.

```auto
if: (repo = ruby/ruby AND (branch = master OR branch =~ /^ruby_\d_\d$/)) OR repo != ruby/ruby

```

Note on the following condition without `repo = ruby/ruby AND`, when sending a PR from forked repo’s `master` or `ruby_N_N` on a forked repository, the PR has 2 Travis builds (Travis CI - Pull-request, and Travis CI - Branch). So, I need to add the condition `repo = ruby/ruby AND` .

```auto
if: branch = master OR branch =~ /^ruby_\d_\d$/ OR repo != ruby/ruby

```

Thanks! It seems I could solve this issue with your help.
