Skip build but for specific branch?

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:

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!

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.

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-

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

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!

1 Like

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?

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 .

So for example:

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