{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://bitbucket.org/pipelines.json",
  "definitions": {
    "image_name": {
      "type": "string",
      "description": "Name of the Docker image which may or may not include registry URL, tag, and digest value",
      "default": "atlassian/default-image:latest",
      "minLength": 1
    },
    "run_as_user": {
      "type": "number",
      "description": "Overrides image's default user, specified user UID must be an existing user in the image with a valid home directory"
    },
    "complex_image": {
      "type": "object",
      "oneOf": [
        {
          "properties": {
            "name": {
              "$ref": "#/definitions/image_name"
            },
            "run-as-user": {
              "$ref": "#/definitions/run_as_user"
            }
          },
          "required": [
            "name"
          ],
          "additionalProperties": false
        },
        {
          "properties": {
            "name": {
              "$ref": "#/definitions/image_name"
            },
            "run-as-user": {
              "$ref": "#/definitions/run_as_user"
            },
            "username": {
              "type": "string",
              "description": "Username to use to fetch the Docker image",
              "minLength": 1
            },
            "password": {
              "type": "string",
              "description": "Password to use to fetch the Docker image",
              "minLength": 1
            },
            "email": {
              "type": "string",
              "description": "Email to use to fetch the Docker image",
              "minLength": 1
            }
          },
          "required": [
            "name",
            "username",
            "password"
          ],
          "additionalProperties": false
        },
        {
          "properties": {
            "name": {
              "$ref": "#/definitions/image_name"
            },
            "run-as-user": {
              "$ref": "#/definitions/run_as_user"
            },
            "aws": {
              "type": "object",
              "oneOf": [
                {
                  "properties": {
                    "access-key": {
                      "type": "string",
                      "description": "AWS access key for Amazon Elastic Container Registry (AWS ECR)",
                      "minLength": 1
                    },
                    "secret-key": {
                      "type": "string",
                      "description": "AWS secret key for Amazon Elastic Container Registry (AWS ECR)",
                      "minLength": 1
                    }
                  },
                  "required": [
                    "access-key",
                    "secret-key"
                  ],
                  "additionalProperties": false
                },
                {
                  "properties": {
                    "oidc-role": {
                      "type": "string",
                      "description": "Provides Pipeline with access to private Docker images hosted in an Amazon Elastic Container Registry (AWS ECR)",
                      "minLength": 1
                    }
                  },
                  "required": [
                    "oidc-role"
                  ],
                  "additionalProperties": false
                }
              ]
            }
          },
          "required": [
            "aws",
            "name"
          ],
          "additionalProperties": false
        }
      ]
    },
    "image": {
      "oneOf": [
        {
          "$ref": "#/definitions/image_name"
        },
        {
          "$ref": "#/definitions/complex_image"
        }
      ]
    },
    "max_time": {
      "type": "number",
      "description": "Maximum amount of minutes a step can execute",
      "exclusiveMinimum": 0,
      "maximum": 120,
      "default": 120
    },
    "size": {
      "type": "string",
      "description": "Multiplier of the resources allocated to a pipeline step",
      "enum": [
        "1x",
        "2x"
      ],
      "default": "1x"
    },
    "clone_properties": {
      "type": "object",
      "description": "Contains settings to clone the repository into a container",
      "properties": {
        "enabled": {
          "type": "boolean",
          "description": "Enables cloning of the repository before a step in a pipeline",
          "default": true
        },
        "lfs": {
          "type": "boolean",
          "description": "Enables the download of LFS files in the clone (supported only for Git repositories)",
          "default": false
        },
        "depth": {
          "description": "Depth of Git clone (supported only for Git repositories)",
          "oneOf": [
            {
              "type": "number",
              "exclusiveMinimum": 0
            },
            {
              "const": "full"
            }
          ],
          "default": 50
        },
        "skip-ssl-verify": {
          "type": "boolean",
          "description": "Disables SSL verification during git clone, allowing the use of self-signed certificates",
          "default": false
        }
      }
    },
    "artifact_list": {
      "type": "array",
      "description": "Files produced by a step to share with a following step",
      "items": {
        "type": "string",
        "description": "Glob pattern for the path to the artifacts"
      },
      "minItems": 1
    },
    "complex_artifacts": {
      "type": "object",
      "properties": {
        "download": {
          "type": "boolean",
          "description": "Enables downloading of all available artifacts at the start of a step",
          "default": true
        },
        "paths": {
          "$ref": "#/definitions/artifact_list"
        }
      }
    },
    "fail_fast": {
      "type": "boolean"
    },
    "runner_label": {
      "type": "string",
      "description": "Any label assigned to a self-hosted repository or workspace Pipeline runner",
      "minLength": 1,
      "maxLength": 50
    },
    "trigger": {
      "type": "string",
      "enum": [
        "automatic",
        "manual"
      ],
      "default": "automatic"
    },
    "condition": {
      "type": "object",
      "properties": {
        "changesets": {
          "type": "object",
          "description": "Condition on the changesets involved in a pipeline",
          "properties": {
            "includePaths": {
              "type": "array",
              "description": "Condition which holds only if any of the modified files match any of the defined patterns",
              "items": {
                "type": "string",
                "description": "Glob pattern to match the file path",
                "minLength": 1
              },
              "minItems": 1
            }
          },
          "required": [
            "includePaths"
          ],
          "additionalProperties": false
        }
      },
      "required": [
        "changesets"
      ],
      "additionalProperties": false
    },
    "step": {
      "type": "object",
      "properties": {
        "step": {
          "type": "object",
          "description": "Build execution unit",
          "properties": {
            "after-script": {
              "$ref": "#/definitions/script",
              "description": "Commands to execute after the step succeeds or fails"
            },
            "artifacts": {
              "anyOf": [
                {
                  "$ref": "#/definitions/artifact_list"
                },
                {
                  "$ref": "#/definitions/complex_artifacts"
                }
              ]
            },
            "caches": {
              "type": "array",
              "description": "Caches enabled for the step",
              "items": {
                "type": "string",
                "description": "Name of the cache"
              },
              "minItems": 1
            },
            "clone": {
              "$ref": "#/definitions/clone_properties"
            },
            "condition": {
              "$ref": "#/definitions/condition",
              "description": "Condition to execute the step"
            },
            "deployment": {
              "type": "string",
              "description": "Type of environment for the deployment step, used in the Deployments dashboard",
              "minLength": 1
            },
            "fail-fast": {
              "$ref": "#/definitions/fail_fast",
              "description": "A flag reflecting whether other steps in a parallel group should be force stopped if this step fails"
            },
            "image": {
              "$ref": "#/definitions/image"
            },
            "max-time": {
              "$ref": "#/definitions/max_time"
            },
            "name": {
              "type": "string",
              "description": "Name of the step",
              "minLength": 1
            },
            "oidc": {
              "type": "boolean",
              "description": "Enable the use of OpenID Connect to connect a pipeline step to a resource server"
            },
            "runs-on": {
              "oneOf": [
                {
                  "$ref": "#/definitions/runner_label"
                },
                {
                  "type": "array",
                  "description": "List of labels assigned to Pipeline runner",
                  "items": {
                    "$ref": "#/definitions/runner_label"
                  },
                  "minItems": 1
                }
              ]
            },
            "script": {
              "$ref": "#/definitions/script",
              "description": "Commands to execute in the step"
            },
            "services": {
              "type": "array",
              "description": "Services enabled for the step",
              "items": {
                "type": "string",
                "description": "Name of the service"
              },
              "minItems": 1,
              "maxItems": 5
            },
            "size": {
              "$ref": "#/definitions/size"
            },
            "trigger": {
              "$ref": "#/definitions/trigger",
              "description": "Specifies if a step runs automatically or only when manually triggered by a user"
            }
          },
          "required": [
            "script"
          ]
        }
      },
      "additionalProperties": false
    },
    "stage": {
      "type": "object",
      "properties": {
        "stage": {
          "type": "object",
          "properties": {
            "condition": {
              "$ref": "#/definitions/condition",
              "description": "Condition to execute the stage"
            },
            "deployment": {
              "type": "string",
              "description": "Type of environment for the deployment stage, used in the Deployments dashboard",
              "minLength": 1
            },
            "name": {
              "type": "string",
              "description": "Name of the stage",
              "minLength": 1
            },
            "steps": {
              "type": "array",
              "description": "List of steps in a stage",
              "items": {
                "$ref": "#/definitions/step"
              },
              "minItems": 1
            },
            "trigger": {
              "$ref": "#/definitions/trigger",
              "description": "Specifies if the stage runs automatically or only when manually triggered by a user"
            }
          },
          "required": [
            "steps"
          ],
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "parallel_steps": {
      "type": "array",
      "description": "Set of steps to run concurrently",
      "items": {
        "$ref": "#/definitions/step"
      },
      "minItems": 2
    },
    "step_or_stage_or_parallel_item": {
      "oneOf": [
        {
          "$ref": "#/definitions/step"
        },
        {
          "$ref": "#/definitions/stage"
        },
        {
          "type": "object",
          "properties": {
            "parallel": {
              "oneOf": [
                {
                  "$ref": "#/definitions/parallel_steps"
                },
                {
                  "type": "object",
                  "properties": {
                    "fail-fast": {
                      "$ref": "#/definitions/fail_fast",
                      "description": "A flag reflecting whether all steps in a parallel group should be force stopped if any of its steps fails."
                    },
                    "steps": {
                      "$ref": "#/definitions/parallel_steps"
                    }
                  },
                  "required": [
                    "steps"
                  ],
                  "additionalProperties": false
                }
              ]
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "actions_list": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/step_or_stage_or_parallel_item"
      },
      "minItems": 1
    },
    "named_pipelines": {
      "type": "object",
      "patternProperties": {
        "^.*$": {
          "$ref": "#/definitions/actions_list"
        }
      }
    },
    "script": {
      "type": "array",
      "items": {
        "oneOf": [
          {
            "type": "string",
            "description": "Command to execute"
          },
          {
            "$ref": "#/definitions/pipe"
          }
        ]
      },
      "minItems": 1
    },
    "pipe": {
      "type": "object",
      "description": "Pipe to execute",
      "properties": {
        "pipe": {
          "type": "string",
          "description": "Pipe identifier"
        },
        "variables": {
          "type": "object",
          "description": "Environment variables passed to the pipe",
          "patternProperties": {
            "^.*$": {
              "oneOf": [
                {
                  "type": "string",
                  "description": "Variable value"
                },
                {
                  "type": "array",
                  "items": {
                    "type": "string",
                    "description": "Array variable item value"
                  },
                  "minItems": 1
                }
              ]
            }
          },
          "minProperties": 1
        }
      },
      "required": [
        "pipe"
      ]
    },
    "cache_path": {
      "type": "string",
      "description": "Path to the directory to be cached, can be absolute or relative to the clone directory"
    },
    "cache_with_key": {
      "type": "object",
      "description": "Cache definition declaring a set of files as a key and path to the directory to be cached",
      "properties": {
        "key": {
          "type": "object",
          "description": "Cache key definition",
          "properties": {
            "files": {
              "type": "array",
              "items": {
                "type": "string",
                "description": "Path to a file or glob pattern of files in the repository which form the cache key"
              },
              "minItems": 1
            }
          },
          "required": [
            "files"
          ],
          "additionalProperties": false
        },
        "path": {
          "$ref": "#/definitions/cache_path"
        }
      },
      "required": [
        "key",
        "path"
      ],
      "additionalProperties": false
    }
  },
  "type": "object",
  "properties": {
    "clone": {
      "$ref": "#/definitions/clone_properties"
    },
    "definitions": {
      "type": "object",
      "description": "Defines resources used elsewhere in the pipeline configuration",
      "properties": {
        "caches": {
          "type": "object",
          "description": "Defines custom caches to be used by pipelines",
          "patternProperties": {
            "^[a-z0-9]([-a-z0-9]{0,48}[a-z0-9])?$": {
              "oneOf": [
                {
                  "$ref": "#/definitions/cache_path"
                },
                {
                  "$ref": "#/definitions/cache_with_key"
                }
              ]
            }
          },
          "not": {
            "required": [
              "docker"
            ]
          },
          "additionalProperties": false
        },
        "services": {
          "type": "object",
          "description": "Defines services that run in separate Docker containers",
          "patternProperties": {
            "^.*$": {
              "type": "object",
              "properties": {
                "image": {
                  "$ref": "#/definitions/image"
                },
                "memory": {
                  "type": "number",
                  "description": "Memory limit for the service container, in megabytes",
                  "minimum": 128,
                  "default": 1024
                },
                "variables": {
                  "type": "object",
                  "description": "Environment variables passed to the service container",
                  "patternProperties": {
                    "^.*$": {
                      "type": "string"
                    }
                  },
                  "minProperties": 1
                }
              }
            }
          },
          "additionalProperties": false
        }
      }
    },
    "image": {
      "$ref": "#/definitions/image"
    },
    "options": {
      "type": "object",
      "properties": {
        "docker": {
          "type": "boolean",
          "description": "A flag to add Docker to all build steps in all pipelines"
        },
        "max-time": {
          "$ref": "#/definitions/max_time"
        },
        "size": {
          "$ref": "#/definitions/size"
        }
      }
    },
    "pipelines": {
      "type": "object",
      "properties": {
        "default": {
          "$ref": "#/definitions/actions_list"
        },
        "branches": {
          "$ref": "#/definitions/named_pipelines",
          "description": "Branch-specific build pipelines"
        },
        "custom": {
          "type": "object",
          "description": "Pipelines that can only be triggered manually or scheduled from the Bitbucket Cloud interface",
          "patternProperties": {
            "^.*$": {
              "type": "array",
              "items": {
                "oneOf": [
                  {
                    "type": "object",
                    "properties": {
                      "variables": {
                        "type": "array",
                        "description": "List of variables for the custom pipeline",
                        "items": {
                          "type": "object",
                          "properties": {
                            "name": {
                              "type": "string",
                              "description": "Name of a variable for the custom pipeline",
                              "minLength": 1
                            },
                            "default": {
                              "type": "string",
                              "description": "Default value for the variable, used unless the value is manually set when the pipeline is run"
                            },
                            "allowed-values": {
                              "type": "array",
                              "description": "A list of values that are allowed for the variable",
                              "items": {
                                "type": "string",
                                "description": "Allowed value for the variable"
                              },
                              "minItems": 1
                            },
                            "description": {
                              "type": "string",
                              "description": "Description of the variable",
                              "minLength": 1
                            }
                          },
                          "required": [
                            "name"
                          ],
                          "additionalProperties": false
                        },
                        "minItems": 1
                      }
                    },
                    "additionalProperties": false
                  },
                  {
                    "$ref": "#/definitions/step_or_stage_or_parallel_item"
                  }
                ]
              },
              "minItems": 1
            }
          }
        },
        "pull-requests": {
          "$ref": "#/definitions/named_pipelines",
          "description": "Pull-request-specific build pipelines"
        },
        "tags": {
          "$ref": "#/definitions/named_pipelines",
          "description": "Tag-specific build pipelines"
        }
      },
      "additionalProperties": false
    }
  },
  "required": [
    "pipelines"
  ]
}