-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.libsonnet
380 lines (342 loc) · 12.2 KB
/
main.libsonnet
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
local crdsonnet = import 'github.com/crdsonnet/crdsonnet/crdsonnet/main.libsonnet';
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
local render = import './render.libsonnet';
local schema = import './schema.libsonnet';
local parsed =
crdsonnet.fromSchema(
'drone',
schema,
render='dynamic',
).drone
;
local package(name, parents=[]) = {
'#': d.package.new(
name,
'github.com/crdsonnet/drone-libsonnet',
'',
'main.libsonnet',
)
+ d.package.withUsageTemplate(|||
local drone = import "%s";
drone.%s%s.<attribute>
||| % [
'github.com/crdsonnet/drone-libsonnet',
std.join('.', parents) + (if std.length(parents) != 0 then '.' else ''),
name,
]),
};
local getName(n) =
std.splitLimit(n, '_', 1)[1];
local lib =
{
'#': d.package.new(
'drone',
'github.com/crdsonnet/drone-libsonnet',
'Jsonnet library for generating Drone CI configuration file.',
'main.libsonnet',
)
+ d.package.withUsageTemplate(|||
%s
```
Render the YAML file:
```console
drone jsonnet --stream \
--format \
--source <(jsonnet -J vendor/ drone.jsonnet) \
--target .drone.yaml
```
> Originally the intention was to render YAML with `std.manifestYamlStream()`,
> however at Grafana Labs we noticed that this function suffers from
> performance issues (taking 16 seconds to render a 23K LoC YAML). Its much
> faster to render the drone pipeline into json with
> `drone.render.getDroneObjects()` and use the `drone` cli tooling to do the
> YAML conversion. Alternatively `jsonnet -y` can be used, which delivers
> a valid YAML stream (json as valid YAML) but it might not look as nice.
```
||| % (importstr 'example/drone.jsonnet')),
}
+ {
// Strip `kind_` from name
[getName(k)]:
parsed[k]
+ package(getName(k))
for k in std.objectFields(parsed)
if std.startsWith(k, 'kind_')
// `kind_pipeline` is covered by `pipeline.<type>`
&& k != 'kind_pipeline'
}
+ {
// Strip `pipeline_` from name and nest in `pipeline` object
pipeline:
{
[getName(k)]:
parsed[k]
+ package(getName(k), ['pipeline'])
for k in std.objectFields(parsed)
if std.startsWith(k, 'pipeline_')
}
+ package('pipeline'),
}
+ {
secret+: {
'#new':: d.fn(
'`new` is a shorthand for creating a new secret object',
args=[
d.arg('name', d.T.string),
d.arg('path', d.T.string),
d.arg('key', d.T.string),
]
),
new(name, path, key):
self.withKind()
+ self.withName(name)
+ self.get.withPath(path)
+ self.get.withName(key),
},
local pipeline = super.pipeline,
pipeline: {
[k]:
pipeline[k]
{
'#new':: d.fn(
'`new` is a shorthand for creating a new pipeline object',
args=[d.arg('name', d.T.string)]
),
new(name):
self.withKind()
+ self.withType()
+ self.withName(name),
'#withParallelStepsMixin':: d.fn(
|||
Pipeline steps are executed sequentially by default. You can optionally
describe your build steps as a directed acyclic graph with `depends_on`.
'`withParallelStepsMixin` will configure each step with `<dependsOn>` to
ensure these steps are executed in parallel. By default it will set
`depends_on` to 'clone'.
|||,
args=[
d.arg('steps', d.T.array),
d.arg('dependsOn', d.T.array, ['clone']),
]
),
withParallelStepsMixin(steps, dependsOn=['clone']): {
steps+: [
step + pipeline[k].steps.withDependsOn(dependsOn)
for step in steps
],
},
steps:: {},
step: // Use singular instead of plural
super.steps
+ package('step', ['pipeline'])
+ {
'#dependsOnCloneStep':: d.fn(
|||
`dependsOnCloneStep` is a shorthand for `withDependsOn(['clone'])
|||,
),
dependsOnCloneStep():
self.withDependsOn('clone'),
// Extend when with useful shortcuts
when+: {
'#onPushToBranch':: d.fn(
|||
`onPushToBranch` will conditionally limit this step to a `push` event
on `<branch_name>`
|||,
args=[d.arg('branch_name', d.T.string)]
),
onPushToBranch(branch_name):
self.event.withIncludeMixin(['push'])
+ self.branch.withIncludeMixin([branch_name]),
'#onPushToMasterBranch':: d.fn(
|||
`onPushToMasterBranch` will conditionally limit this step to a `push`
event on `master` branch
|||,
),
onPushToMasterBranch(): self.onPushToBranch('master'),
'#onPushToMainBranch':: d.fn(
|||
`onPushToMainBranch` will conditionally limit this step to a `push`
event on `main` branch
|||,
),
onPushToMainBranch(): self.onPushToBranch('main'),
'#onPullRequest':: d.fn(
|||
`onPullRequest` will conditionally limit this step to
a `pull_request` event
|||,
),
onPullRequest(): self.event.withIncludeMixin(['pull_request']),
'#onTag':: d.fn(
|||
`onTag` will conditionally limit this step to a `tag` event
|||,
),
onTag(): self.event.withIncludeMixin(['tag']),
'#onTagPattern':: d.fn(
|||
`onTagPattern` will conditionally limit this step to the
creation of a `tag` matching the pattern `tag_pattern`.
|||,
),
onTagPattern(tag_pattern): self.ref.withIncludeMixin(['refs/tags/' + tag_pattern]),
'#onSuccess':: d.fn(
|||
`onSuccess` will conditionally limit this step to a successful
pipeline
|||,
),
onSuccess(): self.withStatusMixin(['success']),
'#onFailure':: d.fn(
|||
`onFailure` will conditionally limit this step to a pipeline failure.
|||,
),
onFailure(): self.withStatusMixin(['failure']),
},
}
+ (if k == 'docker'
then {
'#new':: d.fn(
'`new` is a shorthand for creating a new step object',
args=[
d.arg('name', d.T.string),
d.arg('image', d.T.string),
]
),
new(name, image):
self.withName(name)
+ self.withImage(image),
withPrivileged(): super.withPrivileged(true),
}
else {
'#new':: d.fn(
'`new` is a shorthand for creating a new step object',
args=[d.arg('name', d.T.string)]
),
new(name):
self.withName(name),
}),
clone+: {
'#withDisable':: d.fn(
|||
`withDisable` is a shorthand for disabling cloning, it will also unset
`clone.depth` and `clone.retries` to avoid confusion
|||
),
withDisable(): {
clone: {
disable: true,
// hide other attributes on disable
depth:: 0,
retries:: 0,
},
},
},
// Extend trigger with useful shortcuts
trigger+: {
'#onPushToBranches':: d.fn(
|||
`onPushToBranches` will conditionally limit pipeline execution to
a `push` event on `<branches>`
|||
),
onPushToBranches(branches):
self.event.withIncludeMixin('push')
+ self.branch.withIncludeMixin(branches),
'#onPullRequestAndPushToBranches':: d.fn(
|||
`onPullRequestAndPushToBranches` will conditionally limit pipeline
execution to `push` and `pull_request` events on `<branches>`
|||
),
onPullRequestAndPushToBranches(branches):
self.event.withIncludeMixin(['pull_request', 'push'])
+ self.branch.withIncludeMixin(branches),
'#onPushToMasterBranch':: d.fn(
|||
`onPushToMasterBranch` will conditionally limit pipeline
execution to a `push` event on `master` branch
|||
),
onPushToMasterBranch():
self.onPushToBranches(['master']),
'#onPushToMainBranch':: d.fn(
|||
`onPushToMainBranch` will conditionally limit pipeline
execution to a `push` event on `main` branch
|||
),
onPushToMainBranch():
self.onPushToBranches(['main']),
'#onPullRequest':: d.fn(
|||
`onPullRequest` will conditionally limit pipeline
execution to a `pull_request` event
|||
),
onPullRequest():
self.event.withIncludeMixin('pull_request'),
'#onPromotion':: d.fn(
|||
`onPromotion` will conditionally limit pipeline
execution to a `promotion` event
|||
),
onPromotion(targets):
self.event.withIncludeMixin('promote')
+ self.target.withIncludeMixin(targets),
'#onCronSchedule':: d.fn(
|||
`onCronSchedule` will conditionally limit pipeline
execution to a `cron` event with `<schedule>`
|||
),
onCronSchedule(schedule):
self.event.withIncludeMixin('cron')
+ self.cron.withIncludeMixin(schedule),
'#hourly':: d.fn(
|||
`hourly` will conditionally limit pipeline
execution to a `hourly` `cron` event.
|||
),
hourly(): self.onCronSchedule('hourly'),
'#nightly':: d.fn(
|||
`hourly` will conditionally limit pipeline
execution to a `nightly` `cron` event.
|||
),
nightly(): self.onCronSchedule('nightly'),
'#onModifiedPaths':: d.fn(
|||
`onModifiedPaths` will conditionally limit pipeline execution on changes
to these paths (requires plugin)
|||
),
onModifiedPaths(paths):
self.paths.withIncludeMixin(paths),
'#onModifiedPath':: d.fn(
'`onModifiedPath` shorthand for `onModifiedPaths([path])'
),
onModifiedPath(path):
self.onModifiedPaths([path]),
},
}
+ (if 'services' in pipeline[k]
then {
services:: {},
service: // Use singular instead of plural
super.services
+ package('service', ['pipeline']),
}
else {})
for k in std.objectFields(pipeline)
},
render: render,
};
lib