-
Notifications
You must be signed in to change notification settings - Fork 55
/
mark.ts
110 lines (92 loc) · 3.08 KB
/
mark.ts
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
import {Command, Option} from 'clipanion'
import {TagCommand} from '../tag/tag'
import {
CUSTOM_TAGS_TAG,
ENV_TAG,
IS_DEPLOYMENT_TAG,
IS_ROLLBACK_TAG,
REVISION_TAG,
SERVICE_TAG,
CONTAINS_DEPLOYMENT_TAG,
} from './constants'
/**
* This command is a wrapper around the datadog-ci tag command, allowing customers to mark CI jobs
* as deployments and setting specific properties, like the environment or the revision in a simple way.
*/
export class DeploymentMarkCommand extends Command {
public static paths = [['deployment', 'mark']]
public static usage = Command.Usage({
category: 'CI Visibility',
description: 'Mark a CI job as a deployment.',
details: `
This command will mark a CI job as a deployment.\n
See README for details.
`,
examples: [
['Mark a CI job as a deployment', 'datadog-ci deployment mark'],
['Mark a CI job as a deployment to the staging environment', 'datadog-ci deployment mark --env:staging'],
['Mark a CI job as a rollback deployment', 'datadog-ci deployment mark --is-rollback'],
['Mark a CI job as a deployment of the v123-456 version', 'datadog-ci deployment mark --revision:v123-456'],
[
'Mark a CI job as a deployment for service payment-service',
'datadog-ci deployment mark --service:payment-service',
],
],
})
private noFail = Option.Boolean('--no-fail', false)
private isRollback = Option.Boolean('--is-rollback', false)
private env = Option.String('--env', {
description: 'Example: prod',
})
private revision = Option.String('--revision', {
description: 'Example: 1.0.0',
})
private service = Option.String('--service', {
description: 'Example: payment-service',
})
private tags = Option.Array('--tags')
public async execute() {
const tagJobCommand = new TagCommand()
tagJobCommand.setLevel('job')
tagJobCommand.setTags(this.createJobDeploymentTags())
tagJobCommand.context = this.context
tagJobCommand.setSilent(false)
const tagPipelineCommand = new TagCommand()
tagPipelineCommand.setLevel('pipeline')
tagPipelineCommand.setTags(this.createPipelineDeploymentTags())
tagPipelineCommand.context = this.context
tagPipelineCommand.setSilent(true)
if (this.noFail) {
tagJobCommand.setNoFail(true)
tagPipelineCommand.setNoFail(true)
}
const tagJobCommandExitCode = await tagJobCommand.execute()
if (tagJobCommandExitCode === 0) {
return tagPipelineCommand.execute()
} else {
return tagJobCommandExitCode
}
}
public createJobDeploymentTags(): string[] {
const tags = [IS_DEPLOYMENT_TAG]
if (this.env) {
tags.push(ENV_TAG + this.env)
}
if (this.revision) {
tags.push(REVISION_TAG + this.revision)
}
if (this.service) {
tags.push(SERVICE_TAG + this.service)
}
if (this.isRollback) {
tags.push(IS_ROLLBACK_TAG)
}
if (this.tags) {
tags.push(CUSTOM_TAGS_TAG + this.tags.join(','))
}
return tags
}
public createPipelineDeploymentTags(): string[] {
return [CONTAINS_DEPLOYMENT_TAG]
}
}