{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/cicd/cicd-workflow.v1.schema.json",
  "title": "RootBlocks CICD Workflow v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "pipelineId",
    "standard",
    "provider",
    "providerProfile",
    "serviceCriticality",
    "environments",
    "stages",
    "artifacts",
    "gates",
    "rollback",
    "sourceMaps",
    "evidence"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.cicd-workflow/v1"
    },
    "pipelineId": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "standard": {
      "$ref": "#/$defs/standardRef"
    },
    "provider": {
      "enum": ["github-actions", "azure-devops", "gitlab-ci", "jenkins", "custom"]
    },
    "providerProfile": {
      "$ref": "#/$defs/providerProfile"
    },
    "serviceCriticality": {
      "enum": ["standard", "critical"]
    },
    "environments": {
      "type": "array",
      "minItems": 1,
      "items": {
        "enum": ["dev", "qa", "prod"]
      },
      "uniqueItems": true
    },
    "stages": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/stage"
      }
    },
    "artifacts": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/artifact"
      }
    },
    "gates": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/gate"
      }
    },
    "rollback": {
      "$ref": "#/$defs/rollbackPolicy"
    },
    "sourceMaps": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/sourceMap"
      }
    },
    "secretRefs": {
      "type": "array",
      "items": {
        "$ref": "../security/secret-reference.v1.schema.json"
      }
    },
    "evidence": {
      "$ref": "#/$defs/evidencePolicy"
    }
  },
  "$defs": {
    "standardRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "version", "hash"],
      "properties": {
        "bundleId": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "version": {
          "type": "string",
          "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
        },
        "hash": {
          "type": "string",
          "pattern": "^[a-f0-9]{64,128}$"
        }
      }
    },
    "providerProfile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "provider", "workflowPathPattern", "jobNaming"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "provider": {
          "enum": ["github-actions", "azure-devops", "gitlab-ci", "jenkins", "custom"]
        },
        "workflowPathPattern": {
          "type": "string",
          "minLength": 1
        },
        "jobNaming": {
          "enum": ["stage-id", "stable-prefixed", "custom"]
        },
        "customApprovalRef": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9./-]{2,127}$"
        }
      }
    },
    "stage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "environment"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "type": {
          "enum": ["restore", "build", "test", "security-scan", "package", "publish-artifact", "deploy", "health-check", "promote", "rollback"]
        },
        "environment": {
          "enum": ["dev", "qa", "prod"]
        },
        "dependsOn": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
          },
          "uniqueItems": true
        },
        "producesArtifacts": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
          },
          "uniqueItems": true
        },
        "consumesArtifacts": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
          },
          "uniqueItems": true
        },
        "critical": {
          "type": "boolean"
        }
      }
    },
    "artifact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "pathPattern", "producedByStage", "immutable", "checksum"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "kind": {
          "enum": ["build-output", "test-result", "package", "published-package", "release"]
        },
        "pathPattern": {
          "type": "string",
          "minLength": 1
        },
        "producedByStage": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "publishedByStage": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "immutable": {
          "type": "boolean"
        },
        "checksum": {
          "type": "string",
          "pattern": "^[a-f0-9]{64,128}$"
        }
      }
    },
    "gate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "requiredFor"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "type": {
          "enum": ["unit-tests", "regression", "security", "approval", "health", "policy"]
        },
        "requiredFor": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["dev", "qa", "prod", "package", "publish-artifact", "deploy", "promote", "rollback", "release"]
          },
          "uniqueItems": true
        }
      }
    },
    "rollbackPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["enabled", "strategy", "requiredFor", "evidenceRequired"],
      "properties": {
        "enabled": {
          "type": "boolean"
        },
        "strategy": {
          "enum": ["previous-artifact", "provider-native", "manual", "none"]
        },
        "requiredFor": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["qa", "prod", "deploy", "promote", "critical"]
          },
          "uniqueItems": true
        },
        "evidenceRequired": {
          "const": true
        }
      }
    },
    "sourceMap": {
      "type": "object",
      "additionalProperties": false,
      "required": ["stageId", "constructionPath", "providerFile", "providerJob", "evidencePath"],
      "properties": {
        "stageId": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "constructionPath": {
          "type": "string",
          "pattern": "^rootblocks/.+\\.(json|yaml|yml)$"
        },
        "providerFile": {
          "type": "string",
          "minLength": 1
        },
        "providerJob": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "artifactId": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "evidencePath": {
          "type": "string",
          "pattern": "^evidence/.+\\.(json|yaml|yml)$"
        }
      }
    },
    "evidencePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "events"],
      "properties": {
        "required": {
          "type": "boolean"
        },
        "events": {
          "type": "array",
          "items": {
            "enum": [
              "started",
              "stage-completed",
              "gate-passed",
              "gate-failed",
              "package-created",
              "artifact-published",
              "deployed",
              "promoted",
              "rollback-configured",
              "rolled-back",
              "failed"
            ]
          },
          "uniqueItems": true
        }
      }
    }
  }
}
