@kitman-sh/software-engineering 0.14.8 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +118 -0
- package/agents/releaser.md +3 -3
- package/dist/bundle.js +12562 -5360
- package/dist/declarations.json +139340 -4561
- package/kitman.config.ts +163 -2
- package/package.json +4 -3
- package/src/agents.ts +10 -0
- package/src/approved.ts +219 -0
- package/src/backport-schemas.ts +46 -0
- package/src/backport.ts +110 -0
- package/src/build-schemas.ts +135 -0
- package/src/build.ts +202 -739
- package/src/change-request-body.ts +22 -3
- package/src/delivery.ts +26 -6
- package/src/deploy.ts +661 -0
- package/src/errand.ts +124 -76
- package/src/file-tasks.ts +17 -10
- package/src/firewatch.ts +191 -82
- package/src/hillclimb.ts +563 -273
- package/src/hindsight.ts +154 -100
- package/src/implemented.ts +143 -0
- package/src/index.ts +511 -12
- package/src/land-epic.ts +30 -19
- package/src/land.ts +293 -0
- package/src/landed.ts +30 -0
- package/src/opened.ts +79 -0
- package/src/plan-review.ts +7 -8
- package/src/plan.ts +717 -432
- package/src/project-update.ts +50 -11
- package/src/rallied.ts +535 -0
- package/src/release-set.ts +370 -0
- package/src/release.ts +1402 -573
- package/src/reproduced.ts +82 -0
- package/src/research.ts +53 -37
- package/src/revert.ts +331 -0
- package/src/review.ts +466 -250
- package/src/shepherd-output.ts +81 -35
- package/src/shepherd.ts +2109 -1304
- package/src/standing.ts +165 -0
- package/src/suggested-check.ts +8 -0
- package/src/sweep.ts +432 -253
- package/src/tidy.ts +239 -156
package/src/deploy.ts
ADDED
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ask,
|
|
3
|
+
claim,
|
|
4
|
+
command,
|
|
5
|
+
compare,
|
|
6
|
+
ComparedChange,
|
|
7
|
+
defaultDeployTarget,
|
|
8
|
+
defineLoop,
|
|
9
|
+
deployStatusOf,
|
|
10
|
+
deployTarget,
|
|
11
|
+
type DeployTargetResult,
|
|
12
|
+
recordDeploy,
|
|
13
|
+
RecordDeployRequest,
|
|
14
|
+
rerunPipeline,
|
|
15
|
+
runContext,
|
|
16
|
+
step,
|
|
17
|
+
StepFailed,
|
|
18
|
+
waitFor
|
|
19
|
+
} from '@kitman-sh/sdk/loop'
|
|
20
|
+
import * as Effect from 'effect/Effect'
|
|
21
|
+
import * as Schema from 'effect/Schema'
|
|
22
|
+
|
|
23
|
+
/** The Deploy Target a deploy goes to when it names none. */
|
|
24
|
+
export const defaultTarget = defaultDeployTarget
|
|
25
|
+
|
|
26
|
+
const Target = RecordDeployRequest.fields.target
|
|
27
|
+
|
|
28
|
+
const Input = Schema.Struct({
|
|
29
|
+
tag: RecordDeployRequest.fields.tag.annotate({ description: 'The released tag to deploy.' }),
|
|
30
|
+
target: Schema.optional(Target).annotate({
|
|
31
|
+
description: 'The Deploy Target to deploy to; `production` when left out.'
|
|
32
|
+
}),
|
|
33
|
+
promote: Schema.optional(Schema.Boolean).annotate({
|
|
34
|
+
description:
|
|
35
|
+
"Promotes the tag through the repository's Deploy Targets in their order up to `target`, starting after the last that runs it, asking before each but the first of the list; a target the repository does not list is deployed to alone."
|
|
36
|
+
}),
|
|
37
|
+
rollback: Schema.optional(Schema.Boolean).annotate({
|
|
38
|
+
description:
|
|
39
|
+
'Deploys an earlier tag on purpose: the guard does not ask about what it takes away, and the Deploy is recorded as a rollback.'
|
|
40
|
+
}),
|
|
41
|
+
previous: Schema.optional(Schema.String).annotate({
|
|
42
|
+
description:
|
|
43
|
+
'What the target runs, for the deploy script and a rollback, when Kitman has recorded no Deploy to it; what it recorded wins.'
|
|
44
|
+
}),
|
|
45
|
+
deployEvent: Schema.optional(Schema.String).annotate({
|
|
46
|
+
description:
|
|
47
|
+
'The Event type a repository with no deploy script reports its deploy with; `deploy` when left out. Its payload names the `tag` and a `status`.'
|
|
48
|
+
}),
|
|
49
|
+
deployTimeoutMinutes: Schema.optional(Schema.Int).annotate({
|
|
50
|
+
description: 'How long to wait for that Event; 60 when left out.'
|
|
51
|
+
}),
|
|
52
|
+
repoBindingId: Schema.optional(Schema.String).annotate({
|
|
53
|
+
description: "The repository to deploy; the Run's Task's, else its Team's default, when left out."
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
type Input = typeof Input.Type
|
|
58
|
+
|
|
59
|
+
const TakenAway = Schema.Struct({ number: Schema.String, title: Schema.String, url: Schema.String })
|
|
60
|
+
|
|
61
|
+
const DeployStatus = Schema.Literals([
|
|
62
|
+
'deployed',
|
|
63
|
+
/** A release candidate the deploy script published and did not deploy. */
|
|
64
|
+
'published',
|
|
65
|
+
'rolled_back',
|
|
66
|
+
'deploy_failed',
|
|
67
|
+
/** The deploy failed after the target changed, and so did the rollback: the target may be broken. */
|
|
68
|
+
'rollback_failed',
|
|
69
|
+
'timed_out',
|
|
70
|
+
/** The target runs ChangeRequests the tag lacks, and nobody chose to deploy over them. */
|
|
71
|
+
'not_deployed'
|
|
72
|
+
])
|
|
73
|
+
|
|
74
|
+
const Promoted = Schema.Struct({
|
|
75
|
+
target: Schema.String,
|
|
76
|
+
/** `already_running` for a target it skipped. */
|
|
77
|
+
status: Schema.Union([DeployStatus, Schema.Literal('already_running')])
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
export const DeployOutput = Schema.Struct({
|
|
81
|
+
status: DeployStatus.annotate({
|
|
82
|
+
description: 'How the deploy ended, such as deployed, not_deployed or deploy_failed.'
|
|
83
|
+
}),
|
|
84
|
+
reason: Schema.String.annotate({ description: 'What happened, or why it stopped, in a line.' }),
|
|
85
|
+
tag: Schema.String.annotate({ description: 'The tag it deployed, or was asked to.' }),
|
|
86
|
+
target: Schema.String.annotate({ description: 'The Deploy Target.' }),
|
|
87
|
+
previous: Schema.optional(Schema.String).annotate({ description: 'What the target ran before.' }),
|
|
88
|
+
rollback: Schema.optional(Schema.Boolean).annotate({ description: 'It deployed an earlier tag as a rollback.' }),
|
|
89
|
+
takesAway: Schema.optional(Schema.Array(TakenAway)).annotate({
|
|
90
|
+
description: 'The ChangeRequests the target ran that the tag lacks.'
|
|
91
|
+
}),
|
|
92
|
+
leftUndeployed: Schema.optional(Schema.Boolean).annotate({
|
|
93
|
+
description: 'A person chose Leave it in the recover Question.'
|
|
94
|
+
}),
|
|
95
|
+
promoted: Schema.optional(Schema.Array(Promoted)).annotate({
|
|
96
|
+
description: 'With `promote`, each target on the way, in order, and how its deploy ended.'
|
|
97
|
+
}),
|
|
98
|
+
stoppedBefore: Schema.optional(Schema.String).annotate({
|
|
99
|
+
description: 'With `promote`, the target a person chose not to go on to.'
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
export type DeployOutput = typeof DeployOutput.Type
|
|
104
|
+
|
|
105
|
+
export class Cleared extends Schema.TaggedClass<Cleared>()('Cleared', {
|
|
106
|
+
/** What the target runs that the tag lacks, when a person chose to deploy over it. */
|
|
107
|
+
takesAway: Schema.Array(ComparedChange)
|
|
108
|
+
}) {}
|
|
109
|
+
|
|
110
|
+
export class Held extends Schema.TaggedClass<Held>()('Held', {
|
|
111
|
+
takesAway: Schema.Array(ComparedChange),
|
|
112
|
+
truncated: Schema.Boolean
|
|
113
|
+
}) {}
|
|
114
|
+
|
|
115
|
+
const changeLine = (change: ComparedChange) => {
|
|
116
|
+
const tasks = change.tasks.map(task => task.key)
|
|
117
|
+
|
|
118
|
+
return `- !${change.number} ${change.title}${tasks.length === 0 ? '' : ` (${tasks.join(', ')})`} ${change.url}`
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const changeRequests = (count: number) => (count === 1 ? 'a ChangeRequest' : `${count} ChangeRequests`)
|
|
122
|
+
|
|
123
|
+
const takeAwayOptions = ['Deploy anyway', "Don't deploy"] as const
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Compares the tag with what the target runs and, when the target runs ChangeRequests the tag lacks, asks whether to
|
|
127
|
+
* deploy over them. It recommends not to, takes that when nobody answers, and has no full-auto answer.
|
|
128
|
+
*/
|
|
129
|
+
export const guard = step('guard', {
|
|
130
|
+
payload: {
|
|
131
|
+
tag: Schema.String,
|
|
132
|
+
target: Schema.String,
|
|
133
|
+
running: Schema.String,
|
|
134
|
+
runningRevision: Schema.optional(Schema.String)
|
|
135
|
+
},
|
|
136
|
+
success: Schema.Union([Cleared, Held]),
|
|
137
|
+
run: ({ tag, target, running, runningRevision }) =>
|
|
138
|
+
Effect.gen(function* () {
|
|
139
|
+
const compared = yield* compare('compare', { base: runningRevision ?? running, head: tag })
|
|
140
|
+
const takesAway = compared.behind.changeRequests
|
|
141
|
+
|
|
142
|
+
if (takesAway.length === 0) return new Cleared({ takesAway: [] })
|
|
143
|
+
const truncated = compared.truncated === true
|
|
144
|
+
|
|
145
|
+
const answer = yield* ask('take-away', {
|
|
146
|
+
prompt: [
|
|
147
|
+
`${target} runs ${running}, which has ${changeRequests(takesAway.length)} ${tag} lacks. Deploying ${tag} takes ${takesAway.length === 1 ? 'it' : 'them'} away from ${target}, while its database keeps what their migrations changed:`,
|
|
148
|
+
takesAway.map(changeLine).join('\n') + (truncated ? '\n- and more the list stops short of' : ''),
|
|
149
|
+
`Deploy ${tag} to ${target} anyway?`
|
|
150
|
+
].join('\n\n'),
|
|
151
|
+
options: [
|
|
152
|
+
{ value: 'Deploy anyway', description: `Takes that work away from ${target}.` },
|
|
153
|
+
{ value: "Don't deploy", description: `Leaves ${target} running ${running}.` }
|
|
154
|
+
] satisfies { readonly value: (typeof takeAwayOptions)[number]; readonly description: string }[],
|
|
155
|
+
recommended: "Don't deploy",
|
|
156
|
+
recommendationReason: `${tag} looks cut from another branch; release a tag that holds this work too, or deploy it as a rollback.`,
|
|
157
|
+
default: "Don't deploy",
|
|
158
|
+
timeout: '1 day'
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
return answer === 'Deploy anyway' ? new Cleared({ takesAway }) : new Held({ takesAway, truncated })
|
|
162
|
+
})
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Holds the Deploy Target until the Run ends, or with `keep: 'caller'` until the Step deploying it ends, so one deploy
|
|
167
|
+
* reaches it at a time and two targets never wait on each other.
|
|
168
|
+
*/
|
|
169
|
+
export const claimed = step('claim', {
|
|
170
|
+
payload: { repoBindingId: Schema.String, target: Schema.String, keep: Schema.optional(Schema.Literal('caller')) },
|
|
171
|
+
run: ({ repoBindingId, target, keep }) =>
|
|
172
|
+
Effect.asVoid(
|
|
173
|
+
// A claim a dead Deploy kept is taken over: nothing deploys under it any more.
|
|
174
|
+
claim('target', {
|
|
175
|
+
resource: `deploy:${repoBindingId}:${target}`,
|
|
176
|
+
takeOverOwnLoop: true,
|
|
177
|
+
keep: keep ?? 'run'
|
|
178
|
+
})
|
|
179
|
+
)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
export class Ran extends Schema.TaggedClass<Ran>()('Ran', {
|
|
183
|
+
/** By the deploy script's exit code: 0 deployed (or published), 1 failed, 2 rolled back, 3 the rollback failed. */
|
|
184
|
+
status: Schema.Literals(['deployed', 'published', 'rolled_back', 'failed', 'rollback_failed']),
|
|
185
|
+
detail: Schema.String
|
|
186
|
+
}) {}
|
|
187
|
+
|
|
188
|
+
export class Reported extends Schema.TaggedClass<Reported>()('Reported', {
|
|
189
|
+
/** What the `deploy` Event said, read as Kitman records its Deploy, or `timed_out` when none came in time. */
|
|
190
|
+
status: Schema.Literals(['deployed', 'failed', 'rolled_back', 'rollback_failed', 'timed_out'])
|
|
191
|
+
}) {}
|
|
192
|
+
|
|
193
|
+
const scriptStatus = (exitCode: number, lastLine: string) => {
|
|
194
|
+
switch (exitCode) {
|
|
195
|
+
case 0:
|
|
196
|
+
return lastLine.startsWith('kitman-deploy: published ') ? 'published' : 'deployed'
|
|
197
|
+
case 2:
|
|
198
|
+
return 'rolled_back'
|
|
199
|
+
case 3:
|
|
200
|
+
return 'rollback_failed'
|
|
201
|
+
default:
|
|
202
|
+
return 'failed'
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const detailLimit = 1000
|
|
207
|
+
|
|
208
|
+
/** The detail of the script's last `kitman-deploy: <status> <detail>` line, or how it exited and its output's end. */
|
|
209
|
+
const scriptDetail = (exitCode: number, lastLine: string, outputTail: string) => {
|
|
210
|
+
const reported = /^kitman-deploy: \S+ (.+)$/u.exec(lastLine.trim())?.[1]?.trim()
|
|
211
|
+
|
|
212
|
+
if (reported !== undefined && reported !== '') return reported.slice(0, detailLimit)
|
|
213
|
+
|
|
214
|
+
return `The deploy command exited with exit code ${exitCode}: ${outputTail.trim().slice(-detailLimit)}`
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const DeployWait = {
|
|
218
|
+
deployEvent: Schema.String,
|
|
219
|
+
timeoutMinutes: Schema.Int
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Waits for the deploy Event of `tag` to `target`, which an Event naming no target is to `production`; `timed_out` when
|
|
224
|
+
* none comes in time. A status Kitman cannot read counts as failed, so a person decides.
|
|
225
|
+
*/
|
|
226
|
+
const reported = (
|
|
227
|
+
name: string,
|
|
228
|
+
tag: string,
|
|
229
|
+
target: string,
|
|
230
|
+
wait: { readonly deployEvent: string; readonly timeoutMinutes: number }
|
|
231
|
+
) =>
|
|
232
|
+
waitFor(name, {
|
|
233
|
+
event: wait.deployEvent,
|
|
234
|
+
filter: { tag, target: target === defaultDeployTarget ? [target, null] : target },
|
|
235
|
+
timeout: `${wait.timeoutMinutes} minutes`
|
|
236
|
+
}).pipe(
|
|
237
|
+
Effect.map(event => new Reported({ status: deployStatusOf(event.payload.status) ?? 'failed' })),
|
|
238
|
+
Effect.catchTag('WaitTimedOut', () => Effect.succeed(new Reported({ status: 'timed_out' })))
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Runs the repository's approved command `deploy`, `.kitman/deploy` unless the repository approves another, as
|
|
243
|
+
* `deploy <tag> [previous]` with `KITMAN_DEPLOY_TARGET` naming the target, with no agent; its exit code is the outcome.
|
|
244
|
+
* A repository with no such script reports its deploy with an Event instead, which it waits for.
|
|
245
|
+
*/
|
|
246
|
+
export const ran = step('run', {
|
|
247
|
+
payload: { tag: Schema.String, previous: Schema.optional(Schema.String), target: Schema.String, ...DeployWait },
|
|
248
|
+
success: Schema.Union([Ran, Reported]),
|
|
249
|
+
run: ({ tag, previous, target, ...wait }) =>
|
|
250
|
+
Effect.gen(function* () {
|
|
251
|
+
const result = yield* command('command', {
|
|
252
|
+
run: 'deploy',
|
|
253
|
+
args: previous === undefined ? [tag] : [tag, previous],
|
|
254
|
+
env: { KITMAN_DEPLOY_TARGET: target },
|
|
255
|
+
timeout: '3 hours'
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
if (result.status === 'missing') return yield* reported('event', tag, target, wait)
|
|
259
|
+
|
|
260
|
+
if (result.status === 'interrupted')
|
|
261
|
+
return new Ran({
|
|
262
|
+
status: 'failed',
|
|
263
|
+
detail: 'Its Runner stopped while the deploy command ran; it did not run again'
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
return new Ran({
|
|
267
|
+
status: scriptStatus(result.exitCode, result.lastLine),
|
|
268
|
+
detail: scriptDetail(result.exitCode, result.lastLine, result.outputTail)
|
|
269
|
+
})
|
|
270
|
+
})
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
/** Records a deploy against its Deploy Target. */
|
|
274
|
+
export const recorded = step('record', {
|
|
275
|
+
payload: {
|
|
276
|
+
target: Schema.String,
|
|
277
|
+
tag: Schema.String,
|
|
278
|
+
status: RecordDeployRequest.fields.status,
|
|
279
|
+
detail: Schema.optional(Schema.String)
|
|
280
|
+
},
|
|
281
|
+
run: request => Effect.asVoid(recordDeploy('deploy', request))
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
export class Recovered extends Schema.TaggedClass<Recovered>()('Recovered', {
|
|
285
|
+
status: Schema.Literals(['deployed', 'rolled_back', 'deploy_failed', 'rollback_failed', 'timed_out']),
|
|
286
|
+
reason: Schema.String,
|
|
287
|
+
/** What the retry's deploy Event said, which Kitman recorded as its Deploy. */
|
|
288
|
+
retried: Schema.optional(Schema.Literals(['deployed', 'rolled_back'])),
|
|
289
|
+
/** The tag it rolled back to. */
|
|
290
|
+
to: Schema.optional(Schema.String),
|
|
291
|
+
left: Schema.Boolean
|
|
292
|
+
}) {}
|
|
293
|
+
|
|
294
|
+
const recovery = (previous: string | undefined) =>
|
|
295
|
+
previous === undefined
|
|
296
|
+
? (['Retry the deploy', 'Leave it'] as const)
|
|
297
|
+
: (['Retry the deploy', `Roll back to ${previous}`, 'Leave it'] as const)
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Asks, with no default, what to do about a deploy that failed or did not report in time: retry it (it runs the tag's
|
|
301
|
+
* failed pipeline jobs again and waits), roll back to what the target ran (it runs that tag's pipeline again in full and
|
|
302
|
+
* waits for its deploy), or leave it.
|
|
303
|
+
*/
|
|
304
|
+
export const recovered = step('recover', {
|
|
305
|
+
payload: {
|
|
306
|
+
tag: Schema.String,
|
|
307
|
+
target: Schema.String,
|
|
308
|
+
previous: Schema.optional(Schema.String),
|
|
309
|
+
status: Schema.Literals(['failed', 'timed_out']),
|
|
310
|
+
...DeployWait
|
|
311
|
+
},
|
|
312
|
+
success: Recovered,
|
|
313
|
+
run: ({ tag, target, previous, status: first, ...wait }) =>
|
|
314
|
+
Effect.gen(function* () {
|
|
315
|
+
let status: 'failed' | 'timed_out' = first
|
|
316
|
+
|
|
317
|
+
for (let round = 1; ; round++) {
|
|
318
|
+
const answer = yield* ask(`ask:${round}`, {
|
|
319
|
+
prompt: `${tag} ${status === 'timed_out' ? 'did not report a deploy in time' : 'failed to deploy'}. What now?`,
|
|
320
|
+
options: recovery(previous),
|
|
321
|
+
recommended: 'Retry the deploy',
|
|
322
|
+
recommendationReason:
|
|
323
|
+
'A failed or late deploy is often passing, and a retry runs only the failed jobs of the same tag.',
|
|
324
|
+
// Waits for a person, so Leave it is always their choice.
|
|
325
|
+
default: null
|
|
326
|
+
})
|
|
327
|
+
|
|
328
|
+
const settled = status === 'failed' ? 'deploy_failed' : 'timed_out'
|
|
329
|
+
|
|
330
|
+
if (answer === 'Leave it')
|
|
331
|
+
return new Recovered({ status: settled, reason: `${tag} was left undeployed`, left: true })
|
|
332
|
+
|
|
333
|
+
if (answer === 'Retry the deploy') {
|
|
334
|
+
yield* rerunPipeline(`rerun:${round}`, { ref: tag })
|
|
335
|
+
const again = yield* reported(`retry:${round}`, tag, target, wait)
|
|
336
|
+
|
|
337
|
+
// A `rolled_back` Event of `tag` leaves the target running `tag`, as on the first wait.
|
|
338
|
+
if (again.status === 'deployed' || again.status === 'rolled_back')
|
|
339
|
+
return new Recovered({
|
|
340
|
+
status: 'deployed',
|
|
341
|
+
reason: `${tag} deployed${again.status === 'rolled_back' ? ', as a rollback' : ''}`,
|
|
342
|
+
retried: again.status,
|
|
343
|
+
left: false
|
|
344
|
+
})
|
|
345
|
+
|
|
346
|
+
if (again.status === 'rollback_failed')
|
|
347
|
+
return new Recovered({
|
|
348
|
+
status: 'rollback_failed',
|
|
349
|
+
reason: `${tag} failed to deploy again, and so did its rollback`,
|
|
350
|
+
left: false
|
|
351
|
+
})
|
|
352
|
+
status = again.status === 'timed_out' ? 'timed_out' : 'failed'
|
|
353
|
+
continue
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const to = previous ?? tag
|
|
357
|
+
|
|
358
|
+
yield* rerunPipeline(`redeploy:${round}`, { ref: to, all: true })
|
|
359
|
+
const back = yield* reported(`rollback:${round}`, to, target, wait)
|
|
360
|
+
|
|
361
|
+
if (back.status === 'deployed' || back.status === 'rolled_back')
|
|
362
|
+
return new Recovered({ status: 'rolled_back', reason: `Rolled back to ${to}`, to, left: false })
|
|
363
|
+
|
|
364
|
+
if (back.status === 'rollback_failed')
|
|
365
|
+
return new Recovered({
|
|
366
|
+
status: 'rollback_failed',
|
|
367
|
+
reason: `The rollback to ${to} failed, and so did its own rollback`,
|
|
368
|
+
left: false
|
|
369
|
+
})
|
|
370
|
+
status = back.status === 'timed_out' ? 'timed_out' : 'failed'
|
|
371
|
+
}
|
|
372
|
+
})
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
const runOutcome = {
|
|
376
|
+
deployed: 'deployed',
|
|
377
|
+
published: 'published',
|
|
378
|
+
rolled_back: 'rolled_back',
|
|
379
|
+
failed: 'deploy_failed',
|
|
380
|
+
rollback_failed: 'rollback_failed'
|
|
381
|
+
} as const
|
|
382
|
+
|
|
383
|
+
/** A rollback that deployed ends `rolled_back`; one the script undid left the target where it was, so it failed. */
|
|
384
|
+
const rollbackOutcome = {
|
|
385
|
+
deployed: 'rolled_back',
|
|
386
|
+
published: 'published',
|
|
387
|
+
rolled_back: 'deploy_failed',
|
|
388
|
+
deploy_failed: 'deploy_failed',
|
|
389
|
+
rollback_failed: 'rollback_failed'
|
|
390
|
+
} as const
|
|
391
|
+
|
|
392
|
+
const takenAway = (changes: readonly ComparedChange[]) =>
|
|
393
|
+
changes.map(change => ({ number: change.number, title: change.title, url: change.url }))
|
|
394
|
+
|
|
395
|
+
const OneDeploy = Schema.Struct({
|
|
396
|
+
tag: Schema.String,
|
|
397
|
+
target: Schema.String,
|
|
398
|
+
rollback: Schema.Boolean,
|
|
399
|
+
repository: Schema.String,
|
|
400
|
+
/** What the target runs when Kitman has recorded no Deploy to it. */
|
|
401
|
+
previous: Schema.optional(Schema.String),
|
|
402
|
+
...DeployWait
|
|
403
|
+
})
|
|
404
|
+
|
|
405
|
+
type OneDeploy = typeof OneDeploy.Type
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Deploys `tag` to one Deploy Target: guarded, under its claim, then recorded. A promotion's claim is let go when the
|
|
409
|
+
* Step deploying its target ends, so the next promotion Question keeps no deploy to that target waiting.
|
|
410
|
+
*/
|
|
411
|
+
const deployTo = ({ tag, target, rollback, repository, ...one }: OneDeploy, promoting = false) =>
|
|
412
|
+
Effect.gen(function* () {
|
|
413
|
+
const held = (running: DeployTargetResult['running']) =>
|
|
414
|
+
// Taking work away is what a rollback is for.
|
|
415
|
+
rollback || running === undefined || running.tag === tag
|
|
416
|
+
? Effect.succeed(new Cleared({ takesAway: [] }))
|
|
417
|
+
: guard({
|
|
418
|
+
tag,
|
|
419
|
+
target,
|
|
420
|
+
running: running.tag,
|
|
421
|
+
...(running.revision === undefined ? undefined : { runningRevision: running.revision })
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
const notDeployed = (kept: Held, running: string) =>
|
|
425
|
+
({
|
|
426
|
+
status: 'not_deployed',
|
|
427
|
+
reason: `${target} runs ${changeRequests(kept.takesAway.length)} ${tag} lacks${kept.truncated ? ', and more' : ''} (${kept.takesAway.map(change => `!${change.number}`).join(', ')}), and nobody chose to deploy over them`,
|
|
428
|
+
tag,
|
|
429
|
+
target,
|
|
430
|
+
previous: running,
|
|
431
|
+
takesAway: takenAway(kept.takesAway)
|
|
432
|
+
}) satisfies DeployOutput
|
|
433
|
+
|
|
434
|
+
const before = (yield* deployTarget('target', { target })).running
|
|
435
|
+
const cleared = yield* held(before)
|
|
436
|
+
|
|
437
|
+
if (cleared._tag === 'Held') return notDeployed(cleared, before?.tag ?? tag)
|
|
438
|
+
|
|
439
|
+
yield* claimed({ repoBindingId: repository, target, ...(promoting ? { keep: 'caller' as const } : undefined) })
|
|
440
|
+
|
|
441
|
+
// A deploy that finished while this one waited for the target is guarded against too.
|
|
442
|
+
const { running } = yield* deployTarget('target:claimed', { target })
|
|
443
|
+
const guarded = running?.tag === before?.tag ? cleared : yield* held(running)
|
|
444
|
+
|
|
445
|
+
if (guarded._tag === 'Held') return notDeployed(guarded, running?.tag ?? tag)
|
|
446
|
+
|
|
447
|
+
const { takesAway } = guarded
|
|
448
|
+
const previous = running?.tag ?? one.previous
|
|
449
|
+
const ended = { tag, target, ...(previous === undefined ? undefined : { previous }) }
|
|
450
|
+
const recordedAs = rollback ? 'rolled_back' : 'deployed'
|
|
451
|
+
|
|
452
|
+
const kept = takesAway.length === 0 ? undefined : { takesAway: takenAway(takesAway) }
|
|
453
|
+
const wait = { deployEvent: one.deployEvent, timeoutMinutes: one.timeoutMinutes }
|
|
454
|
+
const deployed = yield* ran({ tag, ...(previous === undefined ? undefined : { previous }), target, ...wait })
|
|
455
|
+
|
|
456
|
+
if (deployed._tag === 'Ran') {
|
|
457
|
+
const status = runOutcome[deployed.status]
|
|
458
|
+
|
|
459
|
+
if (deployed.status === 'deployed') yield* recorded({ target, tag, status: recordedAs, detail: deployed.detail })
|
|
460
|
+
// With no tag known before it, nothing says what the target runs after the script undid it.
|
|
461
|
+
else if (deployed.status === 'rolled_back') {
|
|
462
|
+
if (previous !== undefined)
|
|
463
|
+
yield* recorded({ target, tag: previous, status: 'rolled_back', detail: deployed.detail })
|
|
464
|
+
} else if (deployed.status !== 'published')
|
|
465
|
+
yield* recorded({ target, tag, status: deployed.status, detail: deployed.detail })
|
|
466
|
+
|
|
467
|
+
return {
|
|
468
|
+
status: rollback ? rollbackOutcome[status] : status,
|
|
469
|
+
reason:
|
|
470
|
+
rollback && status === 'rolled_back'
|
|
471
|
+
? `${tag} failed to deploy, and ${previous === undefined ? `${target} was left as it was` : `${target} runs ${previous} again`}: ${deployed.detail}`
|
|
472
|
+
: deployed.detail,
|
|
473
|
+
...ended,
|
|
474
|
+
...(rollback ? { rollback } : undefined),
|
|
475
|
+
...kept
|
|
476
|
+
} satisfies DeployOutput
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// A deploy Event records its own Deploy; a `rolled_back` one of `tag` leaves the target running `tag`.
|
|
480
|
+
if (deployed.status === 'deployed' || deployed.status === 'rolled_back') {
|
|
481
|
+
if (rollback && deployed.status === 'deployed') yield* recorded({ target, tag, status: 'rolled_back' })
|
|
482
|
+
|
|
483
|
+
return {
|
|
484
|
+
status: rollback ? 'rolled_back' : 'deployed',
|
|
485
|
+
reason: `${tag} deployed to ${target}${deployed.status === 'rolled_back' ? ', as a rollback' : ''}`,
|
|
486
|
+
...ended,
|
|
487
|
+
...(rollback ? { rollback } : undefined),
|
|
488
|
+
...kept
|
|
489
|
+
} satisfies DeployOutput
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (deployed.status === 'rollback_failed')
|
|
493
|
+
return {
|
|
494
|
+
status: deployed.status,
|
|
495
|
+
reason: `${tag} failed to deploy to ${target}, and so did its rollback`,
|
|
496
|
+
...ended,
|
|
497
|
+
...(rollback ? { rollback } : undefined),
|
|
498
|
+
...kept
|
|
499
|
+
} satisfies DeployOutput
|
|
500
|
+
|
|
501
|
+
const recovery = yield* recovered({
|
|
502
|
+
tag,
|
|
503
|
+
target,
|
|
504
|
+
...(previous === undefined ? undefined : { previous }),
|
|
505
|
+
status: deployed.status,
|
|
506
|
+
...wait
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
// A retry of a rollback is recorded as one, as its first Event would be.
|
|
510
|
+
const rolledBackTo =
|
|
511
|
+
recovery.status === 'rolled_back' ? recovery.to : rollback && recovery.retried === 'deployed' ? tag : undefined
|
|
512
|
+
|
|
513
|
+
if (rolledBackTo !== undefined) yield* recorded({ target, tag: rolledBackTo, status: 'rolled_back' })
|
|
514
|
+
|
|
515
|
+
return {
|
|
516
|
+
status: (
|
|
517
|
+
{
|
|
518
|
+
deployed: rollback ? 'rolled_back' : 'deployed',
|
|
519
|
+
rolled_back: 'rolled_back',
|
|
520
|
+
deploy_failed: 'deploy_failed',
|
|
521
|
+
rollback_failed: 'rollback_failed',
|
|
522
|
+
timed_out: 'timed_out'
|
|
523
|
+
} as const
|
|
524
|
+
)[recovery.status],
|
|
525
|
+
reason: recovery.reason,
|
|
526
|
+
...ended,
|
|
527
|
+
...(rollback ? { rollback } : undefined),
|
|
528
|
+
...(recovery.left ? { leftUndeployed: true } : undefined),
|
|
529
|
+
...kept
|
|
530
|
+
} satisfies DeployOutput
|
|
531
|
+
})
|
|
532
|
+
|
|
533
|
+
const promoteOptions = (to: string) => [`Deploy to ${to}`, 'Stop here'] as const
|
|
534
|
+
|
|
535
|
+
/** Asks whether to go on to the next target; a full-auto Team goes on. */
|
|
536
|
+
const goOn = (tag: string, from: string, to: string, ranBefore: boolean) => {
|
|
537
|
+
const [deploy, stop] = promoteOptions(to)
|
|
538
|
+
|
|
539
|
+
return ask(`promote:${to}`, {
|
|
540
|
+
prompt: `${tag} ${ranBefore ? 'runs on' : 'deployed to'} ${from}. Deploy it to ${to} next?`,
|
|
541
|
+
options: [
|
|
542
|
+
{ value: deploy, description: `Deploys ${tag} to ${to}.` },
|
|
543
|
+
{ value: stop, description: `Leaves ${to} as it is, and ${from} running ${tag}.` }
|
|
544
|
+
],
|
|
545
|
+
recommended: deploy,
|
|
546
|
+
recommendationReason: `${tag} ${ranBefore ? 'runs on' : 'deployed to'} ${from} as it should.`,
|
|
547
|
+
default: null,
|
|
548
|
+
auto: deploy
|
|
549
|
+
}).pipe(Effect.map(answer => answer === deploy))
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** Deploys one target of a promotion, its Steps named under it, such as `promote[staging]/run`. */
|
|
553
|
+
export const promotedTo = step('promote', {
|
|
554
|
+
payload: OneDeploy,
|
|
555
|
+
success: DeployOutput,
|
|
556
|
+
key: ({ target }) => target,
|
|
557
|
+
run: one => deployTo(one, true)
|
|
558
|
+
})
|
|
559
|
+
|
|
560
|
+
type Promoted = typeof Promoted.Type
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Deploys `tag` to each Deploy Target the repository lists up to `through`, in order, starting after the last that runs
|
|
564
|
+
* it already, and asking before each but the first of the list. A deploy that does not leave its target running the tag
|
|
565
|
+
* stops it there, leaving the targets before as they are.
|
|
566
|
+
*/
|
|
567
|
+
const promote = (one: Omit<OneDeploy, 'target'>, through: string) =>
|
|
568
|
+
Effect.gen(function* () {
|
|
569
|
+
const { tag } = one
|
|
570
|
+
const last = yield* deployTarget('order', { target: through })
|
|
571
|
+
const order = last.order ?? []
|
|
572
|
+
const end = order.indexOf(through)
|
|
573
|
+
const path = end === -1 ? [through] : order.slice(0, end + 1)
|
|
574
|
+
|
|
575
|
+
const states = yield* Effect.forEach(path, target =>
|
|
576
|
+
target === through ? Effect.succeed(last) : deployTarget(`running:${target}`, { target })
|
|
577
|
+
)
|
|
578
|
+
|
|
579
|
+
const runsTag = states.findLastIndex(state => state.running?.tag === tag) + 1
|
|
580
|
+
const promoted: Promoted[] = path.slice(0, runsTag).map(target => ({ target, status: 'already_running' }))
|
|
581
|
+
const pending = path.slice(runsTag)
|
|
582
|
+
const done = one.rollback ? 'rolled_back' : 'deployed'
|
|
583
|
+
|
|
584
|
+
const ranOn = path[runsTag - 1]
|
|
585
|
+
let ended: DeployOutput | undefined
|
|
586
|
+
|
|
587
|
+
for (const target of pending) {
|
|
588
|
+
const from = ended?.target ?? ranOn
|
|
589
|
+
|
|
590
|
+
if (from !== undefined && !(yield* goOn(tag, from, target, ended === undefined)))
|
|
591
|
+
return {
|
|
592
|
+
...(ended ?? { status: done, tag, target: from }),
|
|
593
|
+
reason:
|
|
594
|
+
ended === undefined
|
|
595
|
+
? `${from} runs ${tag} already, and it stopped before ${target}`
|
|
596
|
+
: `${tag} deployed to ${ended.target}, and stopped before ${target}: ${ended.reason}`,
|
|
597
|
+
promoted,
|
|
598
|
+
stoppedBefore: target
|
|
599
|
+
} satisfies DeployOutput
|
|
600
|
+
|
|
601
|
+
ended = yield* promotedTo({ ...one, target })
|
|
602
|
+
promoted.push({ target, status: ended.status })
|
|
603
|
+
|
|
604
|
+
if (ended.status !== done) break
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (ended === undefined)
|
|
608
|
+
return {
|
|
609
|
+
status: done,
|
|
610
|
+
reason: `${through} runs ${tag} already`,
|
|
611
|
+
tag,
|
|
612
|
+
target: through,
|
|
613
|
+
promoted
|
|
614
|
+
} satisfies DeployOutput
|
|
615
|
+
|
|
616
|
+
return { ...ended, promoted } satisfies DeployOutput
|
|
617
|
+
})
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Deploys a released tag to a Deploy Target, `production` by default, with no new tag. Its guard compares the tag
|
|
621
|
+
* with what the target runs, and when the target runs ChangeRequests the tag lacks, as a tag cut from another branch
|
|
622
|
+
* does, asks whether to deploy over them; nobody answering, or a full-auto Team, deploys nothing. A `rollback` skips
|
|
623
|
+
* the guard. It holds `deploy:<repository>:<target>` while it deploys, runs the repository's approved command `deploy`
|
|
624
|
+
* (`.kitman/deploy <tag> <previous>`, with `KITMAN_DEPLOY_TARGET`) with no agent, and records the outcome. A repository
|
|
625
|
+
* with no deploy script reports the deploy with an Event instead, and a failed or late one asks whether to retry, roll
|
|
626
|
+
* back or leave it. With `promote`, it promotes the tag along the repository's Deploy Targets up to `target` that way, one
|
|
627
|
+
* target at a time, asking before each but the first; a full-auto Team goes on.
|
|
628
|
+
*/
|
|
629
|
+
export const deployLoop = defineLoop({
|
|
630
|
+
id: 'deploy',
|
|
631
|
+
version: '1.1.0',
|
|
632
|
+
title: 'Deploy',
|
|
633
|
+
description: 'Deploys a released tag to a Deploy Target, asking before it takes live work away.',
|
|
634
|
+
input: Input,
|
|
635
|
+
output: DeployOutput,
|
|
636
|
+
agents: {},
|
|
637
|
+
checkout: 'read',
|
|
638
|
+
capabilities: ['ask', 'verify', 'claim', 'run_command', 'publish_release', 'merge_change_request'],
|
|
639
|
+
run: (input: Input) =>
|
|
640
|
+
Effect.gen(function* () {
|
|
641
|
+
const repository = (yield* runContext).repository?.repoBinding.id
|
|
642
|
+
|
|
643
|
+
if (repository === undefined)
|
|
644
|
+
return yield* new StepFailed({ step: 'claim', reason: 'The Run has no repository to deploy' })
|
|
645
|
+
|
|
646
|
+
const one = {
|
|
647
|
+
tag: input.tag,
|
|
648
|
+
rollback: input.rollback === true,
|
|
649
|
+
repository,
|
|
650
|
+
...(input.previous === undefined ? undefined : { previous: input.previous }),
|
|
651
|
+
deployEvent: input.deployEvent ?? 'deploy',
|
|
652
|
+
timeoutMinutes: input.deployTimeoutMinutes ?? 60
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
const target = input.target ?? defaultTarget
|
|
656
|
+
|
|
657
|
+
return yield* input.promote === true ? promote(one, target) : deployTo({ ...one, target })
|
|
658
|
+
})
|
|
659
|
+
})
|
|
660
|
+
|
|
661
|
+
export default deployLoop
|