# Trigger build when we push on only particular branch

**URL:** <https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169>\
**Category:** Build Environment\
**Tags:** xcode, ios\
**Created:** [August 29, 2019, 11:07am UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169 "2019-08-29T11:07:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![prat3ik](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/prat3ik/32/23197_2.png) [@prat3ik](https://discuss.circleci.com/u/prat3ik)\
**Post date:** [August 29, 2019, 11:07am UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/1 "2019-08-29T11:07:46Z")

</div>

I have below **config.yml** code which is working fine, but the problem is by default build is getting triggered on every `git push` event so if I am pushing the code on any branch the build would be triggered, and the matter of fact is I want the build trigger when I push code to only particular `develop` branch.

Giving more details about my query  
My project has 4 branches:

1. `master`
2. `develop`
3. `develop-test`
4. `feature`
5. `release`

Out of which only `develop` branch is circleci configured(contains .circleci/config.yml), other branches do not have circleci configuration, so right now according to my config.yml when I am pushing the change on `release`/`feature`/ `develop-test`/`master` the build is getting triggered and failing in couple of seconds as they don’t have config.yml file. So I want build triggered only if push event occurs to `develop` branch.

**config.yml:**

```auto
# .circleci/config.yml
version: 2
jobs:
  build:
    branches:
      only:
        - develop
        
    macos:
      xcode: "10.2.1"
    working_directory: /Users/distiller/project
    environment:
      FL_OUTPUT_DIR: output
      FASTLANE_LANE: build_dist
      BUNDLER_VERSION: 2.0.2

    shell: /bin/bash --login -o pipefail
    steps:
      # - add_ssh_keys:
      # fingerprints:
      # - "cb:ef:91:c4:e7:63:1a:9c:4d:a6:d2:84:9e:fa:68:63"
      - checkout
      - restore_cache:
          key: 1-gems-{{ checksum "Gemfile.lock" }}
      - run: bundle check || bundle install --path vendor/bundle
      - save_cache:
          key: 1-gems-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle
      - run:
          name: fastlane
          command: bundle exec fastlane $FASTLANE_LANE
      - store_artifacts:
          path: output
      - store_test_results:
          path: output/scan

```

Does anyone has suggestions what I am missing 🙂

Thank you

---

<div class="post-metadata">

**Author:** ![stella](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@stella](https://discuss.circleci.com/u/stella)\
**Post date:** [August 30, 2019, 1:51am UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/2 "2019-08-30T01:51:04Z")

</div>

Hi @prat3ik,

> [@prat3ik](#):
>
> branches: only: - develop

I see that the filter configuration is incorrect; filters require a `filter` key and should be defined  
for a job under the `workflows` key. See: [https://circleci.com/docs/2.0/configuration-reference/#filters-1](https://circleci.com/docs/2.0/configuration-reference/#filters-1)

Once you’ve got the configuration fixed, I would recommend putting it (or a minimal version, with the filters) in your other branches to avoid pushes to those branches triggering a build.

---

<div class="post-metadata">

**Author:** ![prat3ik](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/prat3ik/32/23197_2.png) [@prat3ik](https://discuss.circleci.com/u/prat3ik)\
**Post date:** [August 30, 2019, 7:14am UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/3 "2019-08-30T07:14:20Z")

</div>

Thanks for suggestion @stella

I have already tried this solution, however again I tried below `config.yml` and pushed with my ‘develop’ branch, so that build has triggered automatically for ‘develop’ branch

```auto
# .circleci/config.yml

version: 2
jobs:
  build:
   filters:
      branches:
        only:
          - develop
        
    macos:
      xcode: "10.2.1"
    working_directory: /Users/distiller/project
    environment:
      FL_OUTPUT_DIR: output
      FASTLANE_LANE: build_dist
      BUNDLER_VERSION: 2.0.2

    shell: /bin/bash --login -o pipefail
    steps:
      - checkout
      - restore_cache:
          key: 1-gems-{{ checksum "Gemfile.lock" }}
      - run: bundle check || bundle install --path vendor/bundle
      - save_cache:
          key: 1-gems-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle
      - run:
          name: fastlane
          command: bundle exec fastlane $FASTLANE_LANE
      - store_artifacts:
          path: output
      - store_test_results:
          path: output/scan

```

But when I pushed branch `release` , it triggered on circleci(which I am not expecting to trigger).

Can you suggest me what exact change do I need to apply?

Thanks

---

<div class="post-metadata">

**Author:** ![gmemstr](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/gmemstr/32/22922_2.png) [@gmemstr](https://discuss.circleci.com/u/gmemstr)\
**Post date:** [August 30, 2019, 11:04am UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/4 "2019-08-30T11:04:04Z")

</div>

Hi @prat3ik, could you confirm that the new config.yml file has been committed to your primary branch? By default this is `master`, however, this can be changed in the repository settings on GitHub, under “Branches”.

---

<div class="post-metadata">

**Author:** ![prat3ik](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/prat3ik/32/23197_2.png) [@prat3ik](https://discuss.circleci.com/u/prat3ik)\
**Post date:** [September 2, 2019, 2:30pm UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/5 "2019-09-02T14:30:22Z")

</div>

Hi @gmemstr  
Thanks for suggestion!

No, however I have tested with the sample project that is not happening either.

**So for testing purpose I have created one simple project (having one test.txt file only).**  
**That project have 3 branches, and all of them(Except ‘test’ branch) contains same configuration shown below**

Branches:

1. master (contains .circleci/config.yml)
2. develop (contains .circleci/config.yml)
3. test

`.circleci/config.yml`:

```auto
version: 2
jobs:
  build:
    filters:
      branches:
        only:
          - develop
    docker:
      - image: circleci/node:4.8.2 # the primary container, where your job's commands are run
    steps:
      - checkout # check out the code in the project directory
      - run: echo "hello world" # run the `echo` command

```

However when I push the code in `test`, it triggers the pipeline on circleci for `test` branch,  
I just want to trigger pipeline if I do only push branch `develop`, for all other branches I do not want to trigger pipeline if I do push into them

---

<div class="post-metadata">

**Author:** ![efthimia145](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/efthimia145/32/28891_2.png) [@efthimia145](https://discuss.circleci.com/u/efthimia145)\
**Post date:** [December 12, 2021, 4:41pm UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/6 "2021-12-12T16:41:06Z")

</div>

Hello @prat3ik!

I know it’s been a long time since you has this problem, but did you manage to figure out the solution?

I’m having the same issue.

---

<div class="post-metadata">

**Author:** ![AndersJesperPersson](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/andersjesperpersson/32/28930_2.png) [@AndersJesperPersson](https://discuss.circleci.com/u/AndersJesperPersson)\
**Post date:** [December 15, 2021, 9:04am UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/7 "2021-12-15T09:04:44Z")

</div>

Hi all,  
I´m currently working in a group project and we are struggeling with the same issue ^^. Any luck yet eftimia with finding a solution?

---

<div class="post-metadata">

**Author:** ![efthimia145](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/efthimia145/32/28891_2.png) [@efthimia145](https://discuss.circleci.com/u/efthimia145)\
**Post date:** [January 5, 2022, 2:45pm UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/8 "2022-01-05T14:45:20Z")

</div>

Hey. Oops, just saw your reply.

No not really. We ended up keeping and adapting the tests in every branch.

Although the pipeline is always triggered, the configuration file is **unique** for each branch. So you can probably create some dummy successful tests for the branches you do not mind testing, and ignore them. 😋

---

<div class="post-metadata">

**Author:** ![theadmin](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@theadmin](https://discuss.circleci.com/u/theadmin)\
**Post date:** [January 28, 2022, 4:58am UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/9 "2022-01-28T04:58:34Z")

</div>

I had a similar issue that the filter to ignore “main” didn’t work. After consulting with a CircleCi engineer, she pointed out the spacing wasn’t correct 🥲

so, check the spaces and I hope they have a tool or at least a manual method to check the spaces. Here’s an example with the correct spaces:

```auto
version: 2.1
workflows:
  my-App: # workflow label
    jobs:
      - build-myApp:  
          filters:
            branches:
               ignore:                
                  - main

```

---

<div class="post-metadata">

**Author:** ![theadmin](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@theadmin](https://discuss.circleci.com/u/theadmin)\
**Post date:** [January 28, 2022, 5:08am UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/10 "2022-01-28T05:08:11Z")

</div>

check the spaces and see an example I provided below

---

<div class="post-metadata">

**Author:** ![theadmin](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@theadmin](https://discuss.circleci.com/u/theadmin)\
**Post date:** [January 28, 2022, 5:11am UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/11 "2022-01-28T05:11:05Z")

</div>

check the spaces and see an example I provided below. You can specify in the filter whether to use (ignore) to ignore branches you specify below the filter from running or (only) to run the branches you specify below the filter. In my example above, I wanted to run all branches created in the repo except the main branch. So, you will need to add that in every branch including the main branch so that it doesn’t run when you push code to it.

---

<div class="post-metadata">

**Author:** ![efthimia145](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/efthimia145/32/28891_2.png) [@efthimia145](https://discuss.circleci.com/u/efthimia145)\
**Post date:** [January 28, 2022, 8:03am UTC](https://discuss.circleci.com/t/trigger-build-when-we-push-on-only-particular-branch/32169/12 "2022-01-28T08:03:26Z")

</div>

I have indeed tried this, except with `only` and listing the branches i wanted the tests to run on, but it was not successful. I might have done something wrong with the spaces as you said, however I do not need it right now.

Thanks for your answer!
