summaryrefslogtreecommitdiff
path: root/lib/spack/spack/schema/ci.py
blob: 9cc9baf385b929d3a4e70b533c4aafccc3619c49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Copyright 2013-2024 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

"""Schema for gitlab-ci.yaml configuration file.

.. literalinclude:: ../spack/schema/ci.py
   :lines: 13-
"""

from llnl.util.lang import union_dicts

import spack.schema.gitlab_ci

# Schema for script fields
# List of lists and/or strings
# This is similar to what is allowed in
# the gitlab schema
script_schema = {
    "type": "array",
    "items": {"anyOf": [{"type": "string"}, {"type": "array", "items": {"type": "string"}}]},
}

# Schema for CI image
image_schema = {
    "oneOf": [
        {"type": "string"},
        {
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "entrypoint": {"type": "array", "items": {"type": "string"}},
            },
        },
    ]
}

# Additional attributes are allow
# and will be forwarded directly to the
# CI target YAML for each job.
attributes_schema = {
    "type": "object",
    "additionalProperties": True,
    "properties": {
        "image": image_schema,
        "tags": {"type": "array", "items": {"type": "string"}},
        "variables": {
            "type": "object",
            "patternProperties": {r"[\w\d\-_\.]+": {"type": "string"}},
        },
        "before_script": script_schema,
        "script": script_schema,
        "after_script": script_schema,
    },
}

submapping_schema = {
    "type": "object",
    "additionalProperties": False,
    "required": ["submapping"],
    "properties": {
        "match_behavior": {"type": "string", "enum": ["first", "merge"], "default": "first"},
        "submapping": {
            "type": "array",
            "items": {
                "type": "object",
                "additionalProperties": False,
                "required": ["match"],
                "properties": {
                    "match": {"type": "array", "items": {"type": "string"}},
                    "build-job": attributes_schema,
                    "build-job-remove": attributes_schema,
                },
            },
        },
    },
}

named_attributes_schema = {
    "oneOf": [
        {
            "type": "object",
            "additionalProperties": False,
            "properties": {"noop-job": attributes_schema, "noop-job-remove": attributes_schema},
        },
        {
            "type": "object",
            "additionalProperties": False,
            "properties": {"build-job": attributes_schema, "build-job-remove": attributes_schema},
        },
        {
            "type": "object",
            "additionalProperties": False,
            "properties": {"copy-job": attributes_schema, "copy-job-remove": attributes_schema},
        },
        {
            "type": "object",
            "additionalProperties": False,
            "properties": {
                "reindex-job": attributes_schema,
                "reindex-job-remove": attributes_schema,
            },
        },
        {
            "type": "object",
            "additionalProperties": False,
            "properties": {
                "signing-job": attributes_schema,
                "signing-job-remove": attributes_schema,
            },
        },
        {
            "type": "object",
            "additionalProperties": False,
            "properties": {
                "cleanup-job": attributes_schema,
                "cleanup-job-remove": attributes_schema,
            },
        },
        {
            "type": "object",
            "additionalProperties": False,
            "properties": {"any-job": attributes_schema, "any-job-remove": attributes_schema},
        },
    ]
}

pipeline_gen_schema = {
    "type": "array",
    "items": {"oneOf": [submapping_schema, named_attributes_schema]},
}

core_shared_properties = union_dicts(
    {
        "pipeline-gen": pipeline_gen_schema,
        "rebuild-index": {"type": "boolean"},
        "broken-specs-url": {"type": "string"},
        "broken-tests-packages": {"type": "array", "items": {"type": "string"}},
        "target": {"type": "string", "enum": ["gitlab"], "default": "gitlab"},
    }
)

# TODO: Remove in Spack 0.23
ci_properties = {
    "anyOf": [
        {
            "type": "object",
            "additionalProperties": False,
            # "required": ["mappings"],
            "properties": union_dicts(
                core_shared_properties, {"enable-artifacts-buildcache": {"type": "boolean"}}
            ),
        },
        {
            "type": "object",
            "additionalProperties": False,
            # "required": ["mappings"],
            "properties": union_dicts(
                core_shared_properties, {"temporary-storage-url-prefix": {"type": "string"}}
            ),
        },
    ]
}

#: Properties for inclusion in other schemas
properties = {
    "ci": {
        "oneOf": [
            # TODO: Replace with core-shared-properties in Spack 0.23
            ci_properties,
            # Allow legacy format under `ci` for `config update ci`
            spack.schema.gitlab_ci.gitlab_ci_properties,
        ]
    }
}

#: Full schema with metadata
schema = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Spack CI configuration file schema",
    "type": "object",
    "additionalProperties": False,
    "properties": properties,
}


def update(data):
    import llnl.util.tty as tty

    import spack.ci
    import spack.environment as ev

    # Warn if deprecated section is still in the environment
    ci_env = ev.active_environment()
    if ci_env:
        env_config = ci_env.manifest[ev.TOP_LEVEL_KEY]
        if "gitlab-ci" in env_config:
            tty.die("Error: `gitlab-ci` section detected with `ci`, these are not compatible")

    # Detect if the ci section is using the new pipeline-gen
    # If it is, assume it has already been converted
    return spack.ci.translate_deprecated_config(data)