-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdemo-v2-mpt.jsonnet
187 lines (158 loc) · 8.11 KB
/
demo-v2-mpt.jsonnet
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
local pipelines = import '../pipeline.libsonnet';
local mpt = import '../v2PipelineTemplate.libsonnet';
local deployment = import 'deployment.json';
local kubeutils = import 'kubeutils.libsonnet';
local canaryDeployment = kubeutils.canary(deployment);
local account = 'staging-demo';
local app = 'myapp';
local moniker = pipelines.moniker(app, 'some-cluster')
.withStack('someStack')
.withDetail('someDetail');
local myJenkinsMaster = 'staging-jenkins';
local myJenkinsJob = 'smoketest';
local notificationAddress = 'development';
local notificationType = 'slack';
local notificationConditions = ['pipeline.starting', 'pipeline.failed', 'pipeline.complete'];
local myManifestArtifactName = 'app/manifest.yaml';
local myManifestArtifactVersion = 'master';
local myManifestArtifactReference = 'https://gitlab.com/api/v4/projects/your-org%2Fyour-project/repository/files/app%2Fmanifest%2Eyaml/raw';
local myManifestArtifactLocation = 'someLocation';
// Must be specified in pipeline, but not in artifact creation
local myManifestArtifactAccount = 'gitlab-account';
local myDockerArtifactName = 'docker-name';
local myDockerAccount = 'docker-account';
local myDockerOrganization = 'your-docker-org';
local myDockerRegistry = 'index.docker.io';
local myDockerRepository = 'yourorg/app';
local myDockerTag = '^git-.*$';
local myBranch = 'master';
local myProject = 'your-org';
local mySlug = 'your-project';
local mySource = 'gitlab';
local dockerArtifact = pipelines.artifacts
.dockerImage()
.withName(myDockerArtifactName)
.withReference(myDockerRegistry + '/' + myDockerRepository);
local expectedDocker = pipelines.expectedArtifact(myDockerArtifactName)
.withDefaultArtifact(dockerArtifact)
.withMatchArtifact(dockerArtifact)
.withUseDefaultArtifact(true)
.withUsePriorArtifact(false);
local gitlabArtifact = pipelines.artifacts
.gitlabFile()
.withLocation(myManifestArtifactLocation)
.withName(myManifestArtifactName)
.withReference(myManifestArtifactReference)
.withVersion(myManifestArtifactVersion);
local expectedManifest = pipelines.expectedArtifact(myManifestArtifactName)
.withDefaultArtifact(gitlabArtifact)
.withMatchArtifact(gitlabArtifact)
.withUsePriorArtifact(false)
.withUseDefaultArtifact(true);
local dockerTrigger = pipelines.triggers
.docker('myDockerTrigger')
// If ExpectedArtifact Required, add below.
// .withExpectedArtifacts([expectedDocker])
.withAccount(myDockerAccount)
.withOrganization(myDockerOrganization)
.withRegistry(myDockerRegistry)
.withRepository(myDockerRepository)
.withTag(myDockerTag);
local gitTrigger = pipelines.triggers
.git('myGitTrigger')
// If ExpectedArtifact Required, add below.
// .withExpectedArtifacts([expectedManifest])
.withBranch(myBranch)
.withProject(myProject)
.withSlug(mySlug)
.withSource(mySource);
local emailPipelineNotification = pipelines.notification
.withAddress('[email protected]')
.withCC('[email protected]')
.withLevel('pipeline')
.withType('email')
.withWhen('pipeline.starting');
local slackStageNotification = pipelines.notification
.withAddress(notificationAddress)
.withLevel('stage')
.withType(notificationType)
.withWhen('stage.starting')
.withWhen('stage.failed', 'testf: one two three, $variable, %value, "quoted": https://example.com')
.withWhen('stage.complete', 'test');
local manualJudgment = pipelines.stages
.manualJudgment('Manual Judgment')
.withInstructions('Do you want to go ahead?')
.withJudgmentInputs(['yes', 'no']);
local checkPreconditions = pipelines.stages
.checkPreconditions('Confirm Judgment')
.withExpression("${ #judgment('Manual Judgment') == 'yes' }", true)
.withRequisiteStages(manualJudgment);
local wait = pipelines.stages
.wait('Wait')
.withSkipWaitText('Custom wait message')
.withWaitTime('${ templateVariables.waitTime }')
.withRequisiteStages(checkPreconditions);
local whitelist = {
endHour: 7,
endMin: 0,
startHour: 5,
startMin: 0,
};
local deployManifestArtifact = pipelines.stages
.deployManifest('Deploy a manifest with artifact')
.withAccount(account)
.withManifestArtifact(expectedManifest)
.withManifestArtifactAccount(myManifestArtifactAccount)
.withMoniker(moniker)
.withOverrideTimeout('300000')
.withRestrictedExecutionWindow(['1', '2', '3'], whitelist)
.withRequisiteStages(wait);
local deployManifestTextBaseline = pipelines.stages
.deployManifest('Deploy a manifest')
.withAccount(account)
.withManifests(deployment)
.withMoniker(moniker)
.withRequisiteStages(wait);
local deployManifestTextCanary = pipelines.stages
.deployManifest('Deploy a canary manifest')
.withManifests(canaryDeployment)
.withAccount(account)
.withMoniker(moniker)
.withRequisiteStages(wait);
local findArtifactsFromResource = pipelines.stages
.findArtifactsFromResource('Find nginx-deployment')
.withAccount(account)
.withLocation('default')
.withManifestName('Deployment','nginx-deployment')
.withRequisiteStages([deployManifestTextBaseline, deployManifestTextCanary, deployManifestArtifact]);
local jenkinsJob = pipelines.stages
.jenkins('Run Jenkins Job')
.withJob(myJenkinsJob)
.withMarkUnstableAsSuccessful('false')
.withMaster(myJenkinsMaster)
.withNotifications(slackStageNotification)
.withOverrideTimeout('300000')
.withRequisiteStages(findArtifactsFromResource)
.withWaitForCompletion('true');
local pipeline = pipelines.pipeline()
.withApplication(app)
.withExpectedArtifacts([expectedDocker, expectedManifest])
.withName('Demo pipeline')
.withNotifications([emailPipelineNotification])
.withTriggers([dockerTrigger, gitTrigger])
.withStages([manualJudgment, checkPreconditions, wait, deployManifestTextBaseline, deployManifestTextCanary, deployManifestArtifact, findArtifactsFromResource, jenkinsJob]);
local metadata = mpt.metadata()
.withName('My New MPT')
.withDescription('Totally Rad k8s Pipeline')
.withOwner('[email protected]')
.withScopes(['global']);
local waitTime = mpt.variable()
.withType('int')
.withDefaultValue(42)
.withDescription('The length of the segment of time this pipeline shall wait')
.withName('waitTime');
mpt.pipelineTemplate()
.withId('my-new-mpt')
.withMetadata(metadata)
.withVariables([waitTime])
.withPipeline(pipeline)