Prevent more than 1 CircleCI tag workflow from running concurrently

If you are using CircleCI for tag-based deployments, you may want to prevent more than 1 deployment happening at once. This code snippet allows you to check within a pipeline whether another pipeline triggered by a Git tag using the same workflow is currently running.

Because the list of pipelines does not show you which workflow they are using, we are assuming that there is only a single tag-based workflow (if you have multiple you will need to tweak accordingly)

Then it checks all recent pipelines to see whether they are in running or on-hold status (you may need to add additional statuses) and exits non-zero if there are any others excluding the current pipeline (the 1 running the check)

jobs:
  check-if-safe-to-release:
    docker:
      - image: cimg/base:stable
    parameters:
      repository:
        type: string
        description: What repository is the application stored in?
    environment:
      PROJECT: github/magickatt/<< parameters.repository >>
    steps:
      - run:
          name: Check if pipeline is already running for this workflow
          command: |
            ...

You can then include this in .circleci/config.yml as a Job, or even as a Command. If your organisation is using Private Orbs you can move the Job there so that it is more re-useable.

Add comment