Webhook icon
Schedule icon
Request icon
Script icon
Process icon
If icon
Create icon
SlackIncomingWebhook icon
Fail icon
Log icon

CircleCI Pipeline Failure and Flake Ratchet Gate

CircleCI health gate for Kestra. Ratchet failure and flaky rates against a rolling baseline, then open a GitHub issue and Slack alert when the trend regresses.

Categories
Infrastructure

CircleCI tells you a build went red, but it does not tell you that your pipeline is failing more often than it used to. This blueprint turns recent build history into a ratchet: a webhook or an hourly schedule fetches the last runs for a project, a Python step compares the newest window against an older baseline window, and any deterioration past your threshold escalates as a GitHub issue, a Slack ping, and a red execution while healthy runs stay quiet.

How it works

  1. The on_build_event trigger (io.kestra.plugin.core.trigger.Webhook, guarded by the CIRCLECI_WEBHOOK_KEY secret) starts a run whenever CircleCI or your notifier POSTs to it, and the hourly_rollup io.kestra.plugin.core.trigger.Schedule trigger offers a cron cadence that ships disabled: true.
  2. fetch_recent_builds (io.kestra.plugin.core.http.Request) calls the CircleCI builds endpoint for {{ inputs.project_slug }} with the Circle-Token header resolved from the CIRCLECI_TOKEN secret, retrying transient API errors up to three times.
  3. assess_health (io.kestra.plugin.scripts.python.Script on the io.kestra.plugin.core.runner.Process runner) sorts the runs newest first, drops unfinished and canceled builds, and splits the completed ones into a recent window of 10 and a baseline window of 20.
  4. Inside that step, failure rate and flaky rate are computed for both windows, the deltas are compared against regression_threshold_pct, and the worst failing steps (or failing job names when the payload carries no step detail) are aggregated over the recent window.
  5. The verdict travels through the outputs protocol as regression, stable, or insufficient_data, with fractions, percentages, deltas, window counts, and the worst-step summary all in one report payload.
  6. gate (io.kestra.plugin.core.flow.If) opens the escalation path only when the verdict equals regression: open_regression_issue (io.kestra.plugin.github.issues.Create) files the numbers and worst steps in GitHub, alert_slack posts the same figures to Slack, and fail_gate (io.kestra.plugin.core.execution.Fail) turns the execution red.
  7. Healthy and thin-data runs fall through to log_all_clear (io.kestra.plugin.core.log.Log) and stay silent, while the errors handler pings Slack whenever the fetch or the ratchet itself breaks.

What you get

  • A rolling ratchet instead of a raw count: today's failure rate is judged against your own recent history, not an arbitrary number.
  • Flaky detection alongside failures, so a pipeline that passes only after retries is treated as a problem.
  • Worst-step attribution in both the GitHub issue and the Slack message, pointing at the stage to inspect first.
  • A regression boolean, the verdict string, both rates, and a full health_report JSON for downstream flows and dashboards.
  • A red execution on every trip, so branch protections and flow triggers can treat pipeline health as a hard gate.
  • A quiet, logged all-clear on healthy runs, giving you a dated audit trail of every check.

Who it's for

  • Platform and DevOps teams who own a CircleCI project and want to catch slow quality decay before users do.
  • Engineering leads who need a ratchet that names the failing stage rather than another green or red dot.
  • SRE and reliability engineers who wire CI health into incident workflows and on-call alerting.

Why orchestrate this with Kestra

CircleCI reports each build on its own and keeps historical charts in Insights, but it does not compare the newest window against your rolling baseline, open issues, or fail a downstream gate when the trend worsens. Kestra supplies the event trigger, the schedule fallback, retries around a flaky API, a branch that separates escalation from quiet record keeping, and plugin routing into GitHub and Slack, all in one declarative file. Because the ratchet runs as an execution, every trip carries inputs, outputs, and logs that you can replay and audit.

Prerequisites

  • A CircleCI project and a personal API token that can read its builds, with the project slug in vcs-type/org/repo form.
  • A GitHub token allowed to open issues in the target repository, and a Slack incoming webhook for alerts.
  • Python 3 on the Kestra worker, since the ratchet uses the Process runner; switch the script to a Docker task runner if the host has no Python.

Secrets

  • CIRCLECI_TOKEN: CircleCI personal API token sent as the Circle-Token header.
  • CIRCLECI_WEBHOOK_KEY: key segment guarding the webhook trigger URL, treat it like a password.
  • GITHUB_TOKEN: GitHub token used by jwtToken to open the regression issue.
  • SLACK_WEBHOOK_URL: Slack incoming webhook used for regression and failure alerts.

Quick start

  1. Add the four secrets above under Namespace, Secrets, then set project_slug and github_repository inputs to your project.
  2. Run the flow once by hand and read verdict plus health_report from the Outputs tab to confirm the ratchet against your real build history.
  3. Point CircleCI at the webhook so every finished workflow starts a check:
# webhook target to paste into CircleCI or a notifier
https://your-kestra.example.com/api/v1/main/executions/webhook/company.team/circleci-pipeline-health-gate/$CIRCLECI_WEBHOOK_KEY
  1. When you want a cadence instead of events, enable the schedule by flipping the flag it ships with:
- id: hourly_rollup
  type: io.kestra.plugin.core.trigger.Schedule
  cron: "0 * * * *"
  disabled: false

How to extend

  • Tune RECENT_WINDOW, BASELINE_WINDOW, MIN_RECENT, and MIN_BASELINE in the assess_health env block to fit your build volume.
  • Split the single threshold into warn and page tiers with a second If nested under the current gate.
  • Add the CircleCI build URL from the webhook payload to the issue body for one-click navigation to the failing run.
  • Fan out one gate per project with a ForEach over a project list, or chain a remediation flow from this flow's FAILED state.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.