Skip commands if branches are created by Dependabot using regex

How can I set a logical statement to skip the commands if the branch matches the regex?

I tried to add the following to filter out if branch are created by Dependabot but seems like the CircleCI build is not accessing the logical statements and continue to run even if my branch name is dependabot.workflow

when:
    condition:
      not:
       equal: [ /^dependabot.*/ , << pipeline.git.branch >>]

    steps:
     - build-artifacts:
         build_cmd: $BUILD_CMD

     - build-push-docker-image:
         prod: false
         image: $DOCKER_IMAGE
         repo: $DOCKER_REPO

Hi @guohaolee,

Currently, conditional logic does not support regular expressions. You can however use branch level job execution with regular expressions.

1 Like

Hi ,

Thanks for that. But that doesn’t really help. Instead, I just added the following if else on the command to skip it if the branch name starts with dependabot.

commands:
 build-push-docker-image:
   description: "Build docker image command"
   steps:
   run:
     name: Run docker build
     command: |
       if [[ ! << pipeline.git.branch >> =~ ^dependabot.* ]]; then
          docker build .
       else
         echo "skip this run step"
       fi
1 Like

Nice solution @guohaolee!

Unfortunately, this solution won’t stop the resource allocation. It would better to have at least two condition operators like starts_with and ends_with @Henna, otherwise, thinking on the workflow level, if you have 3 jobs which should be filtered by branch, you have to repeat the filter/branches/only over and over again.

A solution like:

workflows:
 ...
   run_on_dependantbot_branches:
      when:
         condition:
             starts_with: [  "dependabot" , << pipeline.git.branch >>]
   jobs:
     ...

Hi @vpereira ,

That is a great point! At the time when this post was created, we did not support using regular expressions for branch filtering on a workflow level. We do now!

We do not currently have a starts_with key but you could use a regular expression in a logic statement to do what you described above.

You can also create an feature request on CircleCI’s ideas page for starts_with & ends_with keys.

1 Like

Hey @Henna thank you for your reply!

1 Like