{
  "name": "CI/CD Deployment Pipeline",
  "version": "1.0.0",
  "description": "Automated build, test, deploy workflow with health checks and rollback - demonstrates conditional control flow, trycatch error handling, and multi-stage orchestration",
  "input_schema": {
    "type": "object",
    "properties": {
      "service": {
        "type": "string",
        "description": "Name of the service to deploy"
      },
      "commit_sha": {
        "type": "string",
        "description": "Git commit SHA to deploy"
      },
      "environment": {
        "type": "string",
        "enum": ["staging", "production"],
        "default": "staging"
      },
      "org": {
        "type": "string",
        "description": "GitHub organization"
      },
      "repo": {
        "type": "string",
        "description": "GitHub repository name"
      }
    },
    "required": ["service", "commit_sha", "org", "repo"]
  },
  "output_schema": {
    "type": "object",
    "properties": {
      "deployment": { "type": "object" },
      "health_check": { "type": "object" },
      "tests": { "type": "object" }
    }
  },
  "stages": [
    {
      "id": "log-start",
      "name": "Log Deployment Start",
      "component_type": "logger",
      "config": {
        "message": "Starting deployment of {{input.service}} ({{input.commit_sha}}) to {{input.environment}}",
        "level": "info",
        "tags": {
          "component": "cicd",
          "service": "{{input.service}}"
        }
      },
      "depends_on": [],
      "position": { "x": 100, "y": 100 }
    },
    {
      "id": "trigger-build",
      "name": "Trigger CI Build",
      "component_type": "http_request",
      "config": {
        "url": "https://api.github.com/repos/{{input.org}}/{{input.repo}}/actions/workflows/build.yml/dispatches",
        "method": "POST",
        "headers": {
          "Authorization": "Bearer {{secrets.GITHUB_TOKEN}}",
          "Accept": "application/vnd.github.v3+json"
        },
        "body": {
          "ref": "main",
          "inputs": {
            "commit_sha": "{{input.commit_sha}}",
            "service": "{{input.service}}"
          }
        },
        "timeout": 30
      },
      "depends_on": ["log-start"],
      "position": { "x": 100, "y": 200 }
    },
    {
      "id": "poll-build-status",
      "name": "Poll Build Status",
      "component_type": "http_request",
      "config": {
        "url": "https://api.github.com/repos/{{input.org}}/{{input.repo}}/actions/runs",
        "method": "GET",
        "headers": {
          "Authorization": "Bearer {{secrets.GITHUB_TOKEN}}"
        },
        "params": {
          "head_sha": "{{input.commit_sha}}",
          "per_page": "1"
        },
        "timeout": 30
      },
      "depends_on": ["trigger-build"],
      "position": { "x": 100, "y": 300 }
    },
    {
      "id": "validate-build",
      "name": "Validate Build Success",
      "component_type": "conditional",
      "config": {
        "condition": "{{upstream.poll-build-status.body.workflow_runs[0].conclusion == 'success'}}",
        "if_true": ["deploy-staging"],
        "if_false": ["notify-build-failure"]
      },
      "depends_on": ["poll-build-status"],
      "position": { "x": 100, "y": 400 }
    },
    {
      "id": "deploy-staging",
      "name": "Deploy to Staging",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.K8S_API_URL}}/apis/apps/v1/namespaces/staging/deployments/{{input.service}}",
        "method": "PATCH",
        "headers": {
          "Authorization": "Bearer {{secrets.K8S_TOKEN}}",
          "Content-Type": "application/strategic-merge-patch+json"
        },
        "body": {
          "spec": {
            "template": {
              "spec": {
                "containers": [{
                  "name": "{{input.service}}",
                  "image": "{{secrets.REGISTRY}}/{{input.service}}:{{input.commit_sha}}"
                }]
              }
            }
          }
        },
        "timeout": 60
      },
      "depends_on": ["validate-build"],
      "position": { "x": 350, "y": 400 }
    },
    {
      "id": "staging-health-check",
      "name": "Staging Health Check",
      "component_type": "http_request",
      "config": {
        "url": "https://staging-{{input.service}}.example.com/health",
        "method": "GET",
        "timeout": 10
      },
      "depends_on": ["deploy-staging"],
      "position": { "x": 350, "y": 500 }
    },
    {
      "id": "run-tests",
      "name": "Run Integration Tests",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.TEST_RUNNER_URL}}/run",
        "method": "POST",
        "body": {
          "suite": "integration",
          "environment": "staging",
          "service": "{{input.service}}"
        },
        "timeout": 300
      },
      "depends_on": ["staging-health-check"],
      "position": { "x": 350, "y": 600 }
    },
    {
      "id": "check-tests",
      "name": "Check Test Results",
      "component_type": "conditional",
      "config": {
        "condition": "{{upstream.run-tests.body.passed == true}}",
        "if_true": ["deploy-production"],
        "if_false": ["analyze-failure"]
      },
      "depends_on": ["run-tests"],
      "position": { "x": 350, "y": 700 }
    },
    {
      "id": "deploy-production",
      "name": "Deploy to Production",
      "component_type": "trycatch",
      "config": {
        "try_stages": ["prod-deploy", "prod-health-check"],
        "catch_stages": ["rollback-production"],
        "finally_stages": ["log-result"]
      },
      "depends_on": ["check-tests"],
      "position": { "x": 600, "y": 700 }
    },
    {
      "id": "prod-deploy",
      "name": "Production Deploy",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.K8S_API_URL}}/apis/apps/v1/namespaces/production/deployments/{{input.service}}",
        "method": "PATCH",
        "headers": {
          "Authorization": "Bearer {{secrets.K8S_TOKEN}}",
          "Content-Type": "application/strategic-merge-patch+json"
        },
        "body": {
          "spec": {
            "template": {
              "spec": {
                "containers": [{
                  "name": "{{input.service}}",
                  "image": "{{secrets.REGISTRY}}/{{input.service}}:{{input.commit_sha}}"
                }]
              }
            }
          }
        },
        "timeout": 60
      },
      "depends_on": ["deploy-production"],
      "position": { "x": 600, "y": 800 }
    },
    {
      "id": "prod-health-check",
      "name": "Production Health Check",
      "component_type": "http_request",
      "config": {
        "url": "https://{{input.service}}.example.com/health",
        "method": "GET",
        "timeout": 10
      },
      "depends_on": ["prod-deploy"],
      "position": { "x": 600, "y": 900 }
    },
    {
      "id": "notify-success",
      "name": "Notify Success",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.SLACK_WEBHOOK}}",
        "method": "POST",
        "body": {
          "text": "Deployment successful: {{input.service}} ({{input.commit_sha}}) deployed to {{input.environment}}"
        }
      },
      "depends_on": ["prod-health-check"],
      "position": { "x": 850, "y": 900 }
    },
    {
      "id": "analyze-failure",
      "name": "AI Analyze Failure",
      "component_type": "generator",
      "config": {
        "model": "gpt-4",
        "temperature": 0.3,
        "system_prompt": "You are a DevOps engineer analyzing deployment failures. Provide: 1) Root cause, 2) Severity, 3) Recommended fix.",
        "prompt": "Analyze this deployment failure:\n\nService: {{input.service}}\nCommit: {{input.commit_sha}}\nTest results: {{upstream.run-tests.body}}"
      },
      "depends_on": ["check-tests"],
      "position": { "x": 100, "y": 800 }
    },
    {
      "id": "rollback-production",
      "name": "Rollback Production",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.K8S_API_URL}}/apis/apps/v1/namespaces/production/deployments/{{input.service}}/rollback",
        "method": "POST",
        "headers": {
          "Authorization": "Bearer {{secrets.K8S_TOKEN}}"
        },
        "timeout": 60
      },
      "depends_on": ["deploy-production"],
      "position": { "x": 850, "y": 800 }
    },
    {
      "id": "notify-build-failure",
      "name": "Notify Build Failure",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.SLACK_WEBHOOK}}",
        "method": "POST",
        "body": {
          "text": "Build failed for {{input.service}} ({{input.commit_sha}})"
        }
      },
      "depends_on": ["validate-build"],
      "position": { "x": 100, "y": 500 }
    },
    {
      "id": "log-result",
      "name": "Log Deployment Result",
      "component_type": "logger",
      "config": {
        "message": "Deployment completed",
        "level": "info",
        "data": {
          "service": "{{input.service}}",
          "commit": "{{input.commit_sha}}",
          "environment": "{{input.environment}}"
        }
      },
      "depends_on": ["deploy-production"],
      "position": { "x": 850, "y": 700 }
    }
  ],
  "output_stage_id": "notify-success"
}
