{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://docs.hod.sh/schema/pipeline.json",
  "title": "Hod pipeline",
  "description": "A Hod pipeline, .hod/pipeline.yaml (format version 0). The schema checks shapes; hod validate also type-checks expressions and checks the rules between keys.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "version",
    "on",
    "jobs"
  ],
  "properties": {
    "version": {
      "description": "The format's version: 0.",
      "enum": [
        0,
        "0"
      ]
    },
    "on": {
      "description": "Which forge events start runs: a list, or a map from events to filters. Manual and scheduled runs start whatever on says.",
      "anyOf": [
        {
          "type": "null"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/definitions/trigger"
          }
        },
        {
          "$ref": "#/definitions/on"
        }
      ]
    },
    "vars": {
      "description": "Named CEL expressions, evaluated once per run; read as vars.<name>.",
      "type": [
        "object",
        "null"
      ],
      "propertyNames": {
        "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
      },
      "additionalProperties": {
        "$ref": "#/definitions/expression"
      }
    },
    "concurrency": {
      "description": "Each run of this pipeline is a member of this group. Pull requests get one by default.",
      "$ref": "#/definitions/concurrency"
    },
    "jobs": {
      "description": "The jobs, by name.",
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "pattern": "^[A-Za-z_][A-Za-z0-9_-]*$"
      },
      "additionalProperties": {
        "if": {
          "type": "object",
          "required": [
            "uses"
          ]
        },
        "then": {
          "$ref": "#/definitions/template_job"
        },
        "else": {
          "if": {
            "type": "object",
            "required": [
              "from"
            ]
          },
          "then": {
            "$ref": "#/definitions/group_job"
          },
          "else": {
            "$ref": "#/definitions/job"
          }
        }
      }
    }
  },
  "definitions": {
    "scalar": {
      "description": "A value YAML reads as text: Hod keeps numbers and booleans as written.",
      "type": [
        "string",
        "number",
        "boolean"
      ]
    },
    "template": {
      "description": "Text, where each ${{ … }} part is a CEL expression whose value is put in.",
      "type": [
        "string",
        "number",
        "boolean"
      ]
    },
    "expression": {
      "description": "A CEL expression, type-checked when the pipeline compiles.",
      "type": [
        "string",
        "number",
        "boolean"
      ]
    },
    "string_list": {
      "description": "One value or a list of values.",
      "anyOf": [
        {
          "type": "null"
        },
        {
          "$ref": "#/definitions/scalar"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/definitions/scalar"
          }
        }
      ]
    },
    "ident": {
      "type": "string",
      "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
    },
    "duration": {
      "description": "A duration such as 90s, 30m, 1h30m or 2d: units largest first.",
      "type": "string",
      "pattern": "^\\s*(\\d+d)?(\\d+h)?(\\d+m)?(\\d+s)?\\s*$",
      "minLength": 2
    },
    "exit_code": {
      "type": "integer",
      "minimum": 1,
      "maximum": 255
    },
    "exit_codes": {
      "description": "An exit code, or a list of them (1 to 255).",
      "anyOf": [
        {
          "$ref": "#/definitions/exit_code"
        },
        {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/exit_code"
          }
        }
      ]
    },
    "env": {
      "description": "Environment variables. Values are templates: ${{ … }} puts an expression's value in.",
      "type": [
        "object",
        "null"
      ],
      "propertyNames": {
        "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
      },
      "additionalProperties": {
        "$ref": "#/definitions/template"
      }
    },
    "trigger": {
      "enum": [
        "push",
        "pull_request"
      ]
    },
    "on": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "push": {
          "description": "Pushes of branches and tags. Without filters, every push.",
          "anyOf": [
            {
              "type": "null"
            },
            {
              "$ref": "#/definitions/push"
            }
          ]
        },
        "pull_request": {
          "description": "Pull requests, filtered by their base branch. Without filters, every pull request.",
          "anyOf": [
            {
              "type": "null"
            },
            {
              "$ref": "#/definitions/pull_request"
            }
          ]
        }
      }
    },
    "push": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "branches": {
          "description": "Globs on branch names; * doesn't cross /.",
          "$ref": "#/definitions/string_list"
        },
        "tags": {
          "description": "Globs on tag names; * doesn't cross /.",
          "$ref": "#/definitions/string_list"
        }
      }
    },
    "pull_request": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "branches": {
          "description": "Globs on the pull request's base branch.",
          "$ref": "#/definitions/string_list"
        }
      }
    },
    "concurrency_policy": {
      "description": "What a new member does when the group is busy.",
      "enum": [
        "buffer_all",
        "buffer_one",
        "skip",
        "cancel_other",
        "allow"
      ]
    },
    "concurrency": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "group": {
          "description": "The group's name, a template over event, vars and a job's matrix.",
          "$ref": "#/definitions/template"
        },
        "policy": {
          "description": "buffer_all (default) waits behind earlier members; buffer_one supersedes the waiting ones; skip doesn't run if busy; cancel_other supersedes every other member; allow (top level only) runs in parallel.",
          "$ref": "#/definitions/concurrency_policy"
        }
      },
      "if": {
        "properties": {
          "policy": {
            "const": "allow"
          }
        },
        "required": [
          "policy"
        ]
      },
      "then": {
        "not": {
          "required": [
            "group"
          ]
        }
      },
      "else": {
        "required": [
          "group"
        ]
      }
    },
    "cache_preset": {
      "description": "A built-in cache preset.",
      "enum": [
        "go",
        "npm",
        "pip",
        "rust"
      ]
    },
    "cache_entry": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "paths": {
          "description": "Directories to keep, expanded in the job's environment ($HOME, ~/).",
          "$ref": "#/definitions/nonempty_list"
        },
        "key_files": {
          "description": "Globs of the files whose contents make the cache key, such as lock files.",
          "$ref": "#/definitions/nonempty_list"
        }
      },
      "required": [
        "paths",
        "key_files"
      ]
    },
    "nonempty_list": {
      "anyOf": [
        {
          "$ref": "#/definitions/scalar"
        },
        {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/scalar"
          }
        }
      ]
    },
    "step_cache": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "inputs": {
          "description": "Files and globs the step depends on: when none changed, the step is skipped and its files and outputs restored.",
          "$ref": "#/definitions/nonempty_list"
        }
      },
      "required": [
        "inputs"
      ]
    },
    "matrix_entry": {
      "description": "Keys and their values: the job runs once per combination.",
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
      },
      "additionalProperties": {
        "anyOf": [
          {
            "$ref": "#/definitions/scalar"
          },
          {
            "type": "array",
            "minItems": 1,
            "items": {
              "$ref": "#/definitions/scalar"
            }
          }
        ]
      }
    },
    "matrix": {
      "description": "Run the job once per combination of values: a map, or a list of maps whose instances are added up.",
      "anyOf": [
        {
          "$ref": "#/definitions/matrix_entry"
        },
        {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/matrix_entry"
          }
        }
      ]
    },
    "retry_when": {
      "enum": [
        "failure",
        "timeout",
        "error"
      ]
    },
    "retry_rule": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "when": {
          "description": "failure (a step failed), timeout, error (setup, storage, checkout, a lost runner). All three by default.",
          "anyOf": [
            {
              "$ref": "#/definitions/retry_when"
            },
            {
              "type": "array",
              "items": {
                "$ref": "#/definitions/retry_when"
              }
            }
          ]
        },
        "exit_codes": {
          "description": "Only failures of a step that exited with one of these.",
          "$ref": "#/definitions/exit_codes"
        },
        "max": {
          "description": "More attempts this rule allows: 2 by default, at most 10.",
          "type": "integer",
          "minimum": 1,
          "maximum": 10
        }
      }
    },
    "allow_failure_codes": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "exit_codes": {
          "description": "Allow failures of a step that exited with one of these.",
          "$ref": "#/definitions/exit_codes"
        }
      },
      "required": [
        "exit_codes"
      ]
    },
    "allow_failure": {
      "description": "true allows every failure, timeout and error; {exit_codes: [...]} only those codes.",
      "anyOf": [
        {
          "type": "boolean"
        },
        {
          "$ref": "#/definitions/allow_failure_codes"
        }
      ]
    },
    "port": {
      "anyOf": [
        {
          "type": "integer",
          "minimum": 1,
          "maximum": 65535
        },
        {
          "type": "string",
          "pattern": "^[0-9]+$"
        }
      ]
    },
    "ready": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "run": {
          "description": "A script, run in the service with sh -c, that exits 0 once it is ready.",
          "$ref": "#/definitions/scalar"
        },
        "timeout": {
          "description": "How long to wait: 60s by default.",
          "$ref": "#/definitions/duration"
        }
      },
      "minProperties": 1
    },
    "service": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "image": {
          "description": "The service's image.",
          "$ref": "#/definitions/template"
        },
        "env": {
          "$ref": "#/definitions/env"
        },
        "command": {
          "description": "Arguments; replaces the image's CMD.",
          "type": "array",
          "items": {
            "$ref": "#/definitions/template"
          }
        },
        "entrypoint": {
          "description": "Replaces the image's ENTRYPOINT, and drops its CMD.",
          "type": "array",
          "items": {
            "$ref": "#/definitions/template"
          }
        },
        "ports": {
          "description": "Ports to wait for before the first step.",
          "anyOf": [
            {
              "$ref": "#/definitions/port"
            },
            {
              "type": "array",
              "items": {
                "$ref": "#/definitions/port"
              }
            }
          ]
        },
        "ready": {
          "description": "A readiness script, or {run, timeout}.",
          "anyOf": [
            {
              "$ref": "#/definitions/scalar"
            },
            {
              "$ref": "#/definitions/ready"
            }
          ]
        }
      },
      "required": [
        "image"
      ]
    },
    "services": {
      "description": "Containers beside the job, sharing its network: steps reach them at localhost and at their name.",
      "type": [
        "object",
        "null"
      ],
      "propertyNames": {
        "pattern": "^(?!localhost$)[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$"
      },
      "additionalProperties": {
        "anyOf": [
          {
            "$ref": "#/definitions/template"
          },
          {
            "$ref": "#/definitions/service"
          }
        ]
      }
    },
    "secret_grant": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "name": {
          "description": "The secret's name.",
          "$ref": "#/definitions/ident"
        },
        "env": {
          "description": "Also put the value in this variable.",
          "$ref": "#/definitions/ident"
        }
      },
      "required": [
        "name"
      ]
    },
    "secrets": {
      "description": "Secrets granted by name: each is a file in $HOD_SECRETS, and with env also a variable.",
      "type": "array",
      "minItems": 1,
      "items": {
        "anyOf": [
          {
            "$ref": "#/definitions/ident"
          },
          {
            "$ref": "#/definitions/secret_grant"
          }
        ]
      }
    },
    "identity_grant": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "name": {
          "description": "The token's name: a file in /run/hod/identity/<name>. token by default.",
          "$ref": "#/definitions/ident"
        },
        "audience": {
          "description": "The token's audience, such as sts.amazonaws.com.",
          "$ref": "#/definitions/scalar"
        }
      },
      "required": [
        "audience"
      ]
    },
    "identity": {
      "description": "Short-lived OIDC tokens signed by the Hod server: {audience}, or a list of {name, audience}.",
      "anyOf": [
        {
          "$ref": "#/definitions/identity_grant"
        },
        {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/identity_grant"
          }
        }
      ]
    },
    "guard": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "uses": {
          "description": "The guard component, such as hod/egress-guard@v1.",
          "type": "string"
        },
        "with": {
          "description": "The guard's inputs.",
          "type": "object"
        }
      },
      "required": [
        "uses"
      ]
    },
    "output_type": {
      "enum": [
        "string",
        "bool",
        "int",
        "list(string)"
      ]
    },
    "step": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "id": {
          "description": "Lets later steps read steps.<id>.outputs and steps.<id>.result.",
          "$ref": "#/definitions/ident"
        },
        "uses": {
          "description": "A step component: ./path in the repository, or a published one such as hod/setup-go@v1.",
          "type": "string",
          "pattern": "^\\./|@"
        },
        "with": {
          "description": "The component's inputs.",
          "type": [
            "object",
            "null"
          ],
          "additionalProperties": {
            "anyOf": [
              {
                "$ref": "#/definitions/template"
              },
              {
                "type": "array",
                "items": {
                  "$ref": "#/definitions/scalar"
                }
              }
            ]
          }
        },
        "name": {
          "description": "Shown in logs, the UI and the API. The script's first line by default.",
          "$ref": "#/definitions/scalar"
        },
        "run": {
          "description": "The script: bash -eo pipefail if the image has bash, else sh -e. No ${{ }} here: pass values through env.",
          "$ref": "#/definitions/scalar"
        },
        "dir": {
          "description": "Working directory, relative to the workspace.",
          "$ref": "#/definitions/scalar"
        },
        "env": {
          "$ref": "#/definitions/env"
        },
        "if": {
          "description": "Whether the step runs: a CEL expression.",
          "$ref": "#/definitions/expression"
        },
        "cache": {
          "description": "The step cache.",
          "$ref": "#/definitions/step_cache"
        },
        "files": {
          "description": "Files or directories the step produces, copied into the jobs that need this one.",
          "$ref": "#/definitions/string_list"
        },
        "outputs": {
          "description": "Typed values the step writes, one file each in $HOD_OUTPUTS.",
          "type": [
            "object",
            "null"
          ],
          "propertyNames": {
            "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
          },
          "additionalProperties": {
            "anyOf": [
              {
                "$ref": "#/definitions/output_type"
              },
              {
                "description": "An output type written with spaces, such as list( string ).",
                "type": "string",
                "pattern": "^\\s*(s\\s*t\\s*r\\s*i\\s*n\\s*g|b\\s*o\\s*o\\s*l|i\\s*n\\s*t|l\\s*i\\s*s\\s*t\\s*\\(\\s*s\\s*t\\s*r\\s*i\\s*n\\s*g\\s*\\))\\s*$"
              }
            ]
          }
        },
        "push": {
          "description": "Refs the step may push to: globs on full ref names, starting with refs/.",
          "anyOf": [
            {
              "type": "string",
              "pattern": "^refs/"
            },
            {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "string",
                "pattern": "^refs/"
              }
            }
          ]
        },
        "timeout": {
          "description": "How long the step may run.",
          "$ref": "#/definitions/duration"
        },
        "allow_failure": {
          "$ref": "#/definitions/allow_failure"
        },
        "secrets": {
          "$ref": "#/definitions/secrets"
        },
        "identity": {
          "$ref": "#/definitions/identity"
        }
      },
      "oneOf": [
        {
          "required": [
            "run"
          ]
        },
        {
          "required": [
            "uses"
          ]
        }
      ]
    },
    "job": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "needs": {
          "description": "Jobs that must end first; this job gets their files and reads their outputs.",
          "$ref": "#/definitions/string_list"
        },
        "runs-on": {
          "description": "Labels the runner must have. Can read ${{ matrix.* }} only.",
          "$ref": "#/definitions/string_list"
        },
        "image": {
          "description": "The container image the job runs in.",
          "$ref": "#/definitions/template"
        },
        "if": {
          "description": "Whether the job runs: a CEL expression.",
          "$ref": "#/definitions/expression"
        },
        "env": {
          "$ref": "#/definitions/env"
        },
        "docker": {
          "description": "Give the job a Docker daemon of its own.",
          "type": "boolean"
        },
        "fresh": {
          "description": "Build from scratch: restore no dependency or step cache and save none. For jobs whose output is released.",
          "type": "boolean"
        },
        "cache": {
          "description": "Dependency caches: preset names (go, npm, pip, rust) or {paths, key_files}.",
          "type": "array",
          "items": {
            "anyOf": [
              {
                "$ref": "#/definitions/cache_preset"
              },
              {
                "$ref": "#/definitions/cache_entry"
              }
            ]
          }
        },
        "steps": {
          "description": "What the job runs, in order, in one container.",
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/step"
          }
        },
        "matrix": {
          "$ref": "#/definitions/matrix"
        },
        "fail_fast": {
          "description": "With matrix: a failed instance cancels the others.",
          "type": "boolean"
        },
        "max_parallel": {
          "description": "With matrix: at most this many instances at once.",
          "type": "integer",
          "minimum": 1
        },
        "concurrency": {
          "description": "The job is a member of this group, across runs.",
          "$ref": "#/definitions/concurrency"
        },
        "timeout": {
          "description": "How long the job may run: 6h by default.",
          "$ref": "#/definitions/duration"
        },
        "retry": {
          "description": "Run a failed job again: a number of attempts, or a list of rules.",
          "anyOf": [
            {
              "type": "integer",
              "minimum": 1,
              "maximum": 10
            },
            {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#/definitions/retry_rule"
              }
            }
          ]
        },
        "allow_failure": {
          "$ref": "#/definitions/allow_failure"
        },
        "services": {
          "$ref": "#/definitions/services"
        },
        "secrets": {
          "$ref": "#/definitions/secrets"
        },
        "identity": {
          "$ref": "#/definitions/identity"
        },
        "guards": {
          "description": "Components that run for the whole job, such as an egress guard.",
          "type": "array",
          "items": {
            "$ref": "#/definitions/guard"
          }
        }
      },
      "required": [
        "steps"
      ]
    },
    "template_job": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "uses": {
          "description": "A template: a component.yaml with jobs, expanded when the pipeline compiles.",
          "type": "string"
        },
        "with": {
          "description": "The template's inputs.",
          "type": [
            "object",
            "null"
          ]
        },
        "needs": {
          "description": "Jobs that must end first.",
          "$ref": "#/definitions/string_list"
        },
        "if": {
          "description": "Whether the template's jobs run: a CEL expression.",
          "$ref": "#/definitions/expression"
        },
        "matrix": {
          "$ref": "#/definitions/matrix"
        },
        "secrets": {
          "description": "The template's secret names, mapped to yours.",
          "type": "object",
          "additionalProperties": {
            "$ref": "#/definitions/ident"
          }
        }
      },
      "required": [
        "uses"
      ]
    },
    "group_file": {
      "description": "A job group's file: a path from the repository root to a .yaml or .yml file under .hod/jobs/.",
      "type": "string",
      "pattern": "^\\./\\.hod/+jobs/.+\\.ya?ml$"
    },
    "group_job": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "from": {
          "description": "A job group: its jobs come from these files under .hod/jobs/, named <group>/<job>. Each file has one key, jobs:.",
          "anyOf": [
            {
              "$ref": "#/definitions/group_file"
            },
            {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#/definitions/group_file"
              }
            }
          ]
        },
        "needs": {
          "description": "Jobs that every job of the group needs.",
          "$ref": "#/definitions/string_list"
        },
        "if": {
          "description": "Whether the group's jobs run: a CEL expression, ANDed into each job's own if:.",
          "$ref": "#/definitions/expression"
        },
        "matrix": {
          "$ref": "#/definitions/matrix"
        }
      },
      "required": [
        "from"
      ]
    }
  }
}
