# Skip build but for specific branch?

**URL:** <https://discuss.circleci.com/t/skip-build-but-for-specific-branch/37160>\
**Category:** Build Environment\
**Created:** [August 20, 2020, 11:08pm UTC](https://discuss.circleci.com/t/skip-build-but-for-specific-branch/37160 "2020-08-20T23:08:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rankinit](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rankinit/32/25520_2.png) [@rankinit](https://discuss.circleci.com/u/rankinit)\
**Post date:** [August 20, 2020, 11:08pm UTC](https://discuss.circleci.com/t/skip-build-but-for-specific-branch/37160/1 "2020-08-20T23:08:32Z")

</div>

[https://circleci.com/docs/2.0/skip-build/#skipping-a-build](https://circleci.com/docs/2.0/skip-build/#skipping-a-build)

In this documentation it explains that by using `[ci skip]` or `[skip ci]` in the commit message that circleci will skip that build.

Can we specify a branch to ignore for example ‘[ci skip staging]’

If that is not possible another user posted some time ago another solution where by checking the commit message for a custom designation to skip the build or stop the job here:

> [@Skipping certain builds only for certain stages (or how to make circleci step halt work across jobs)](https://discuss.circleci.com/t/skipping-certain-builds-only-for-certain-stages-or-how-to-make-circleci-step-halt-work-across-jobs/27300):
>
> The way we deploy from staging to production is by doing a --ff-only merge. Because we have CircleCI update our git-dumped database schema after each PR is merged, the last commit in staging should always be ignored by CI. That can be easily done by adding “[ci skip]” to the commit name, but the problem is then that the --ff-only merge from staging to production won’t trigger a CI deployment. How do I ensure that some commits are skipped by certain stages? Our attempt Instead, we tried rolling…

Is this currently possible with the latest version of circleci?

I would like to add a step in my job where by circleci would commit and push to the branch the workflow is running off of at the end of my job. However I don’t want to have circleci build for that branch again for that commit as this would cause a recursion.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![annapamela](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@annapamela](https://discuss.circleci.com/u/annapamela)\
**Post date:** [August 25, 2020, 8:25pm UTC](https://discuss.circleci.com/t/skip-build-but-for-specific-branch/37160/2 "2020-08-25T20:25:38Z")

</div>

Hi and welcome @rankinit !

Thank you for writing in! Unfortunately, there’s no way to specify “ignored branches” in your commit message.

If your workflow kicks off with a single job, you can filter by branch and ignore branches with that job: [https://circleci.com/docs/2.0/configuration-reference/#branches](https://circleci.com/docs/2.0/configuration-reference/#branches).

If your workflow kicks off with multiple jobs, you can try adding a check job that filters by branch as described in this article: [https://support.circleci.com/hc/en-us/articles/115015953868-Filter-workflows-by-branch-](https://support.circleci.com/hc/en-us/articles/115015953868-Filter-workflows-by-branch-)

So for example, you can use a workflow like the following:

```yaml
version: 2.1

jobs:
  checking-branch-job:
    docker:
      - image: circleci/node:10.8
    steps:
      - run: echo "hello world"
  next-job-1:
    docker:
      - image: circleci/node:10.8
    steps:
      - run: echo "1"
  next-job-2:
    docker:
      - image: circleci/node:10.8
    steps:
      - run: echo "2"

workflows:
  build:
    jobs:
      - checking-branch-job:
          filters:
            branches:
              ignore:
                - branch-to-ignore
      - next-job-1:
          requires:
            - checking-branch-job
      - next-job-2:
          requires:
            - checking-branch-job

```

If you trigger a pipeline on `branch-to-ignore`, the pipeline will say `Created workflow` but will not run.

I hope that helps! Please let me know if you have any questions!

---

<div class="post-metadata">

**Author:** ![rankinit](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rankinit/32/25520_2.png) [@rankinit](https://discuss.circleci.com/u/rankinit)\
**Post date:** [August 27, 2020, 5:00pm UTC](https://discuss.circleci.com/t/skip-build-but-for-specific-branch/37160/3 "2020-08-27T17:00:51Z")

</div>

Thank you for your response!

I don’t think branch-to-ignore would work as the commit and push would be on a branch that I have set up a filter to specifically trigger a workflow on that branch. In essence it would cause a recursive build process to fire off (merge changes into stage branch -\> workflow kickoffs a stage build to testflight and auto update version number -\> commit and push version back into stage branch -\> workflow kickoffs a stage build…)

Is there a way to stop a workflow (without an error alert) when a commit message contains certain text?

---

<div class="post-metadata">

**Author:** ![annapamela](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@annapamela](https://discuss.circleci.com/u/annapamela)\
**Post date:** [August 27, 2020, 5:59pm UTC](https://discuss.circleci.com/t/skip-build-but-for-specific-branch/37160/4 "2020-08-27T17:59:11Z")

</div>

Hi @rankinit! Thanks for clarifying – and yes, I see the conundrum here!

You can certainly parse the commit message, or use the built-in environment variable for “CIRCLE\_BRANCH” to stop the step. You can stop the workflow with an exit 1, or since you are trying to avoid that, you can [use the API to cancel the currently running workflow](https://circleci.com/docs/api/v2/#cancel-a-workflow) .

So for example:

```auto
version: 2.1

jobs:
  my-test-job:
    machine: true
    steps:
      - checkout
      - run: | # this job exits if the branch matches a string
          if ["$CIRCLE_BRANCH" == 'branch-to-ignore']; then
            exit 1
          fi
      - run: | # this job cancels the workflow with the API if there is a particular string in the commit message. In this case it looks for [ignore-branch:true]
          COMMIT_SUBJECT=`git log -1 --pretty=%s.`
          IGNORE_BRANCH=$(echo "${COMMIT_SUBJECT}" | sed -En 's/.*\[ignore-branch:(true)\].*/\1/p')
          echo "Commit subject: ${COMMIT_SUBJECT}"
          echo "ignore-branch: ${IGNORE_BRANCH}"
          if ["$IGNORE_BRANCH" == 'true']; then
            curl -X POST "https://circleci.com/api/v2/workflow/${CIRCLE_WORKFLOW_ID}/cancel" \
            -H "Accept: application/json" \
            -H "Circle-Token: ${CIRCLE_TOKEN}"
          fi
  another-job:
    machine: true
    steps:
      - run: echo "I should not run"

workflows:
  tests:
    jobs:
      - my-test-job:
          context: context
      - another-job:
          requires:
            - my-test-job

```

Note that in the second step, I passed in my CIRCLE\_TOKEN from a context.

There is also, however, an open feature request to cancel a workflow from within a job without using the API: [https://ideas.circleci.com/ideas/CCI-I-856](https://ideas.circleci.com/ideas/CCI-I-856). Please do vote for it if this is something that will be useful to you!

I hope that helps! Please ask away if you have additional questions!
