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

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.

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?

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. :yum:

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 :smiling_face_with_tear:

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:

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

check the spaces and see an example I provided below

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.

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!