-
Notifications
You must be signed in to change notification settings - Fork 57
/
RolloutOpenShiftDeploymentStage.groovy
145 lines (133 loc) · 5.49 KB
/
RolloutOpenShiftDeploymentStage.groovy
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
package org.ods.component
import groovy.transform.TypeChecked
import groovy.transform.TypeCheckingMode
import org.ods.services.OpenShiftService
import org.ods.services.JenkinsService
import org.ods.util.ILogger
@SuppressWarnings('ParameterCount')
@TypeChecked
class RolloutOpenShiftDeploymentStage extends Stage {
public final String STAGE_NAME = 'Deploy to OpenShift'
private final OpenShiftService openShift
private final JenkinsService jenkins
private final RolloutOpenShiftDeploymentOptions options
private IDeploymentStrategy deploymentStrategy
private Map<String, Object> config
@SuppressWarnings(['AbcMetric', 'CyclomaticComplexity'])
@TypeChecked(TypeCheckingMode.SKIP)
RolloutOpenShiftDeploymentStage(
def script,
IContext context,
Map<String, Object> config,
OpenShiftService openShift,
JenkinsService jenkins,
ILogger logger) {
super(script, context, logger)
if (!config.selector) {
config.selector = context.selector
}
if (!config.imageTag) {
config.imageTag = context.shortGitCommit
}
if (!config.deployTimeoutMinutes) {
config.deployTimeoutMinutes = context.openshiftRolloutTimeout ?: 15
}
if (!config.deployTimeoutRetries) {
config.deployTimeoutRetries = context.openshiftRolloutTimeoutRetries ?: 5
}
// Helm options
if (!config.chartDir) {
config.chartDir = 'chart'
}
if (!config.containsKey('helmReleaseName')) {
config.helmReleaseName = context.componentId
}
if (!config.containsKey('helmValues')) {
config.helmValues = [:]
}
if (!config.containsKey('helmValuesFiles')) {
config.helmValuesFiles = [ 'values.yaml' ]
}
if (!config.containsKey('helmEnvBasedValuesFiles')) {
config.helmEnvBasedValuesFiles = []
}
if (!config.containsKey('helmDefaultFlags')) {
config.helmDefaultFlags = ['--install', '--atomic']
}
if (!config.containsKey('helmAdditionalFlags')) {
config.helmAdditionalFlags = []
}
if (!config.containsKey('helmDiff')) {
config.helmDiff = true
}
if (!config.helmPrivateKeyCredentialsId) {
config.helmPrivateKeyCredentialsId = "${context.cdProject}-helm-private-key"
}
// Tailor options
if (!config.openshiftDir) {
config.openshiftDir = 'openshift'
}
if (!config.tailorPrivateKeyCredentialsId) {
config.tailorPrivateKeyCredentialsId = "${context.cdProject}-tailor-private-key"
}
if (!config.tailorSelector) {
config.tailorSelector = config.selector
}
if (!config.containsKey('tailorVerify')) {
config.tailorVerify = true
}
if (!config.containsKey('tailorExclude')) {
config.tailorExclude = 'bc,is'
}
if (!config.containsKey('tailorParamFile')) {
config.tailorParamFile = '' // none apart the automatic param file
}
if (!config.containsKey('tailorPreserve')) {
config.tailorPreserve = [] // do not preserve any paths in live config
}
if (!config.containsKey('tailorParams')) {
config.tailorParams = []
}
this.config = config
this.options = new RolloutOpenShiftDeploymentOptions(config)
this.openShift = openShift
this.jenkins = jenkins
}
// This is called from Stage#execute if the branch being built is eligible.
protected run() {
if (!context.environment) {
logger.warn 'Skipping because of empty (target) environment ...'
return
}
// We have to move everything here,
// otherwise Jenkins will complain
// about: "hudson.remoting.ProxyException: CpsCallableInvocation{methodName=fileExists, ..."
def isHelmDeployment = steps.fileExists(options.chartDir + '/Chart.yaml')
logger.info("isHelmDeployment: ${isHelmDeployment}")
def isTailorDeployment = steps.fileExists(options.openshiftDir)
if (isTailorDeployment && isHelmDeployment) {
steps.error("Must be either a Tailor based deployment or a Helm based deployment")
throw new IllegalStateException("Must be either a Tailor based deployment or a Helm based deployment")
}
// Use tailorDeployment in the following cases:
// (1) We have an openshiftDir
// (2) We do not have an openshiftDir but neither do we have an indication that it is Helm
if (isTailorDeployment || (!isHelmDeployment && !isTailorDeployment)) {
deploymentStrategy = new TailorDeploymentStrategy(script, context, config, openShift, jenkins, logger)
String resourcePath = 'org/ods/component/RolloutOpenShiftDeploymentStage.deprecate-tailor.GString.txt'
def msg =steps.libraryResource(resourcePath)
logger.warn(msg)
}
if (isHelmDeployment) {
deploymentStrategy = new HelmDeploymentStrategy(script, context, config, openShift, jenkins, logger)
}
logger.info("deploymentStrategy: ${deploymentStrategy} -- ${deploymentStrategy.class.name}")
return deploymentStrategy.deploy()
}
protected String stageLabel() {
if (options.selector != context.selector) {
return "${STAGE_NAME} (${options.selector})"
}
STAGE_NAME
}
}