Tag matching, using condition - an example

A lot of posts here are about getting tags to correctly cause a workflow to start. This is mainly due to the fact that circleci’s processor will only process tags after a check against the branch name has been processed (let’s call it a hidden feature), but also in part due to the boiler-plate filter code that is required to cause the right actions to take place.

Over the years a lot of answers have included code snippets, but there are few clear examples and when conditional flows were added, the examples provided focused on the structure of the condition, without many examples in context.

Below is an example that may help someone in the future (see notes after)

version: 2.1

executors:
  exec_node:
    machine: true
    working_directory: /tmp/CircleCI_working_directories/task
    resource_class: company/basic-linux

##common values (think variables, structs and the like)

  tag_filters: &tag_filters
    filters:
      tags:
        only: 
          - /^[vV].[0-9]+.*/ 
          - /^[0-9a-zA-Z]+.*/
      branches:
        ignore: /.*/

   
commands:
      
  do_step_1:
    steps:
      - run: 
          name: first step
          command: echo 1

  do_step_2:
    steps:
      - run: 
          name: first step
          command: echo 1

  clean_up_workspace:
    steps:
      - run:
          name: remove all the working files
          command: |
                   rm -rf *  || true
                   rm -rf .* || true

jobs:

  build-runner-latest-from-branch-v2:
    executor: exec_node
    parameters:
      token:
       type: string
    steps:
      - do_step_1
      - do_step_2
       - clean_up_workspace:
      

workflows:
   Frontend-dev1:
     when: 
       or:
         - matches: { pattern: "^[0-9a-zA-Z]+.ci-dev" , value:  << pipeline.git.tag >> }
         - matches: { pattern: "^[0-9a-zA-Z]+.ci-dev1" , value:  << pipeline.git.tag >> }
     jobs:
         - build-runner-latest-from-branch-v2:
              token: "BwqVYRS5287"
              <<: *tag_filters

   Frontend-dev2:
     when: 
       or:
         - matches: { pattern: "^[0-9a-zA-Z]+.ci-dev" , value:  << pipeline.git.tag >> }
         - matches: { pattern: "^[0-9a-zA-Z]+.ci-dev2" , value:  << pipeline.git.tag >> }
     jobs:
         - build-runner-latest-from-branch-v2:
             token: "H8cia5EPF18z"
             <<: *tag_filters

Notes

  • The filter code has been set up using yaml’s anchor and reference feature (think macro). So the filter is created with the tag of tag_filters and can then be reused as and when needed.

  • I code using the structure of “workflows” that call “jobs”, that in turn execute “commands”.

  • exact pattern matches for the tag name take place within the “when” section of each workflow.

  • the called job has a parameter defined and passed as a way to show context regarding order and spacing.

More general info

  • This was put together using code that runs on a self-hosted runner (hence the executor).

  • An example tag that will cause a workflow to operate is 0f3d5ef.ci-dev1, to get both workflows to operate a tag like 0f3d5ef.ci-dev can be used.