Trigger build when we push on only particular branch

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:

# .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 :slight_smile:

Thank you

Hi @prat3ik,

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

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.

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

# .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

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”.

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:

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