id: gitlab-environment-deployment-failure-sentinel
namespace: company.devops
inputs:
- id: gitlab_project_path
type: STRING
displayName: GitLab Project Full Path
defaults: "gitlab-org/gitlab"
description: Full path of the GitLab project (e.g. `group/subgroup/project` or
`org/repo`).
- id: target_environments
type: ARRAY
itemType: STRING
displayName: Target Environments
defaults: [ "production", "staging", "uat" ]
description: List of environment names to monitor for deployment failures. Leave
empty to audit all active environments.
- id: max_tolerable_failures
type: INT
displayName: Maximum Tolerable Deployment Failures
defaults: 0
description: Allowable count of failed environments before dispatching a
critical alert to Slack.
triggers:
- id: deployment_health_schedule
type: io.kestra.plugin.core.trigger.Schedule
description: Audits environment rollout statuses every 15 minutes. Shipped
disabled by default.
cron: "*/15 * * * *"
disabled: true
tasks:
- id: fetch_environment_deployments
type: io.kestra.plugin.graphql.Request
description: Query GitLab GraphQL API for project environments and their latest
deployment statuses.
uri: "https://gitlab.com/api/graphql"
headers:
Authorization: "Bearer {{ secret('GITLAB_PRIVATE_TOKEN') }}"
Content-Type: "application/json"
User-Agent: "Kestra-GitLab-Deployment-Sentinel/1.0"
failOnGraphQLErrors: false
query: |
query ($projectPath: ID!) {
project(fullPath: $projectPath) {
environments(first: 25) {
nodes {
id
name
state
externalUrl
lastDeployment {
id
iid
status
createdAt
finishedAt
commit {
shortId
title
authorName
}
user {
username
name
}
}
}
}
}
}
variables:
projectPath: "{{ inputs.gitlab_project_path }}"
- id: audit_deployment_statuses
type: io.kestra.plugin.scripts.python.Script
description: Filter environments and flag recent deployments that ended in
FAILED, CANCELED, or BLOCKED states.
containerImage: python:3.11-slim
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
env:
RESPONSE_BODY: "{{ outputs.fetch_environment_deployments.body }}"
PROJECT_PATH: "{{ inputs.gitlab_project_path }}"
TARGET_ENVS: "{{ inputs.target_environments | toJson }}"
script: |
import os
import json
from kestra import Kestra
raw_data = os.environ.get("RESPONSE_BODY", "{}")
project_path = os.environ.get("PROJECT_PATH", "")
target_envs_raw = os.environ.get("TARGET_ENVS", "[]")
try:
target_envs = [env.lower() for env in json.loads(target_envs_raw)]
except Exception:
target_envs = []
try:
data = json.loads(raw_data)
except Exception:
data = {}
environments = data.get("data", {}).get("project", {}).get("environments", {}).get("nodes", [])
failed_environments = []
for env in environments:
env_name = env.get("name", "unknown")
if target_envs and env_name.lower() not in target_envs:
continue
last_dep = env.get("lastDeployment")
if not last_dep:
continue
status = (last_dep.get("status") or "").upper()
if status in ["FAILED", "CANCELED", "BLOCKED"]:
commit = last_dep.get("commit") or {}
user = last_dep.get("user") or {}
failed_environments.append({
"environment": env_name,
"status": status,
"deployment_iid": last_dep.get("iid"),
"commit_sha": commit.get("shortId", "N/A"),
"commit_title": commit.get("title", "No commit title"),
"author": commit.get("authorName") or user.get("name", "Unknown"),
"finished_at": last_dep.get("finishedAt", "N/A")
})
summary = f"GitLab project {project_path} has {len(failed_environments)} environment(s) with failed or blocked deployments."
formatted_lines = [
f"• *{item['environment']}*: Status `{item['status']}` (Deployment #{item['deployment_iid']}, Commit `{item['commit_sha']}`: \"{item['commit_title']}\" by {item['author']})"
for item in failed_environments[:5]
]
Kestra.outputs({
"failed_environments_count": len(failed_environments),
"failed_environments": failed_environments,
"summary": summary,
"details_formatted": "\n".join(formatted_lines)
})
- id: check_deployment_failures
type: io.kestra.plugin.core.flow.If
description: Evaluate if count of failed environments breaches allowable limit.
condition: "{{ outputs.audit_deployment_statuses.vars.failed_environments_count
> inputs.max_tolerable_failures }}"
then:
- id: notify_slack_deployment_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Post high-severity rollout failure notification to DevOps and
Release channels in Slack.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": "#devops-deployments",
"username": "Kestra GitLab Rollout Sentinel",
"icon_emoji": ":warning:",
"attachments": [
{
"color": "#dc3545",
"title": "GitLab Environment Deployment Failure Detected",
"title_link": "https://gitlab.com/{{ inputs.gitlab_project_path }}/-/environments",
"text": "{{ outputs.audit_deployment_statuses.vars.summary }}",
"fields": [
{
"title": "Project",
"value": "`{{ inputs.gitlab_project_path }}`",
"short": true
},
{
"title": "Failed Environments Count",
"value": "{{ outputs.audit_deployment_statuses.vars.failed_environments_count }}",
"short": true
},
{
"title": "Impacted Environments & Deployments",
"value": "{{ outputs.audit_deployment_statuses.vars.details_formatted }}",
"short": false
}
],
"footer": "Kestra Rollout Sentinel",
"ts": "{{ now() | date('X') }}"
}
]
}
errors:
- id: send_failure_alert
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Dispatch critical alert if GitLab GraphQL query or rollout audit
encounters unexpected failures.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": "#devops-alerts",
"username": "Kestra Sentinel Watchdog",
"icon_emoji": ":rotating_light:",
"attachments": [
{
"color": "#dc3545",
"title": "GitLab Deployment Sentinel Execution Failed",
"text": "Execution *{{ execution.id }}* failed during environment deployment inspection for project *{{ inputs.gitlab_project_path }}*.",
"fields": [
{
"title": "Flow ID",
"value": "{{ flow.id }}",
"short": true
},
{
"title": "Namespace",
"value": "{{ flow.namespace }}",
"short": true
}
],
"footer": "Kestra Error Watchdog",
"ts": "{{ now() | date('X') }}"
}
]
}