forked from aquasecurity/deployments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
231 lines (220 loc) · 9.05 KB
/
Jenkinsfile
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
@Library('aqua-pipeline-lib@master') _
import com.aquasec.deployments.orchestrators.*
class Global {
static Object CHANGED_FILES = []
static Object CHANGED_CF_FILES = []
static Object CHANGED_MANIFESTS_FILES = []
static Object SORTED_CHANGED_FILES = []
static String BUILD_USER_EMAIL
}
def orchestrator = new OrcFactory(this).GetOrc()
def debug = true
pipeline {
agent {
label 'deployment_slave'
}
options {
ansiColor('xterm')
timestamps()
skipStagesAfterUnstable()
skipDefaultCheckout()
buildDiscarder(logRotator(daysToKeepStr: '7'))
lock('k3s')
}
environment {
AWS_ACCESS_KEY_ID = credentials('svc_team_1_aws_access_key_id')
AWS_SECRET_ACCESS_KEY = credentials('svc_team_1_aws_secret_access_key')
AWS_REGION = "us-west-2"
AQUADEV_AZURE_ACR_PASSWORD = credentials('aquadevAzureACRpassword')
AUTH0_CREDS = credentials('auth0Credential')
VAULT_TERRAFORM_SID = credentials('VAULT_TERRAFORM_SID')
VAULT_TERRAFORM_SID_USERNAME = "$VAULT_TERRAFORM_SID_USR"
VAULT_TERRAFORM_SID_PASSWORD = "$VAULT_TERRAFORM_SID_PSW"
VAULT_TERRAFORM_RID = credentials('VAULT_TERRAFORM_RID')
VAULT_TERRAFORM_RID_USERNAME = "$VAULT_TERRAFORM_RID_USR"
VAULT_TERRAFORM_RID_PASSWORD = "$VAULT_TERRAFORM_RID_PSW"
}
stages {
stage("Checkout") {
steps {
script {
log.info "CHANGE_TARGET: ${CHANGE_TARGET}"
log.info "CHANGE_BRANCH: ${CHANGE_BRANCH}"
deployment.clone branch: "master"
checkout([
$class : 'GitSCM',
branches : scm.branches,
doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'deployments']],
userRemoteConfigs : scm.userRemoteConfigs
])
}
}
}
stage("Analyze Changes") {
steps {
script {
dir("deployments") {
Global.CHANGED_FILES = sh(script: "git --no-pager diff origin/${CHANGE_TARGET} --name-only", returnStdout: true).trim().split("\\r?\\n")
def gitCommits = sh(script: "git log --pretty=format:'%h' -n 1", returnStdout: true).trim().split("\\r?\\n")
for (commit in gitCommits) {
log.info "commit: ${commit}"
}
for (file in Global.CHANGED_FILES) {
log.info "file: ${file}"
}
}
def sortChangedFiles = deployments.sortChangedFiles(Global.CHANGED_FILES)
Global.CHANGED_CF_FILES = sortChangedFiles["CHANGED_CF_FILES"]
Global.CHANGED_MANIFESTS_FILES = sortChangedFiles["CHANGED_MANIFESTS_FILES"]
Global.SORTED_CHANGED_FILES = sortChangedFiles["SORTED_CHANGED_FILES"]
}
}
}
stage('Other Files') {
when {
allOf {
not { expression { return Global.SORTED_CHANGED_FILES.isEmpty() } }
expression { return deployments.runCloudFormation(CHANGE_TARGET) }
}
}
steps {
script {
log.info "Starting to test SORTED_CHANGED_FILES"
for (file in Global.SORTED_CHANGED_FILES) {
log.info "file: ${file} was changed"
}
}
}
}
stage('Cloudformation') {
when {
allOf {
not { expression { return Global.CHANGED_CF_FILES.isEmpty() } }
expression { return deployments.runCloudFormation(CHANGE_TARGET) }
}
}
steps {
script {
log.info "Starting to test Cloudformation yamls"
def deploymentImage = docker.build("deployment-cloudformation-image", "-f Dockerfile-cloudformation .")
deploymentImage.inside("-u root") {
log.info "Installing aqaua-deployment python package"
sh """
aws codeartifact login --tool pip --repository deployment --domain aqua-deployment --domain-owner 172746256356
pip install aqua-deployment
"""
log.info "Finished to install aqaua-deployment python package"
def parallelStagesMap = Global.CHANGED_CF_FILES.collectEntries {
["${it.split("/")[-1]}": deployments.generateStage(it, "cloudformation")]
}
parallel parallelStagesMap
}
}
}
}
stage("Manifest") {
when {
allOf {
not { expression { return Global.CHANGED_MANIFESTS_FILES.isEmpty() } }
expression { return deployments.runCloudFormation(CHANGE_TARGET) }
}
}
steps {
script {
log.info "Starting to test Manifest yamls"
def deploymentImage = docker.build("deployment-manifest-image", "-f Dockerfile-manifest .")
deploymentImage.inside("-u root") {
def parallelStagesMap = Global.CHANGED_MANIFESTS_FILES.collectEntries {
["${it.split("/")[-1]}": deployments.generateStage(it, "manifest")]
}
parallel parallelStagesMap
}
}
}
}
stage("K3S Cluster Install and Prepare") {
when {
allOf {
not { expression { return Global.CHANGED_MANIFESTS_FILES.isEmpty() } }
expression { return deployments.runCloudFormation(CHANGE_TARGET) }
}
}
steps {
script {
orchestrator.install()
helm.settingKubeConfig()
kubectl.createNamespace create: "yes"
kubectl.createDockerRegistrySecret create: "yes"
}
}
}
stage("Deploy Manifests"){
when {
allOf {
not { expression { return Global.CHANGED_MANIFESTS_FILES.isEmpty() } }
expression { return deployments.runCloudFormation(CHANGE_TARGET) }
}
}
steps{
script{
def deploymentImage = docker.build("deployment-k3s-image", "-f Dockerfile-k3s .")
deploymentImage.inside("-u root --network host") {
log.info "Pulling manifests with Aquactl and modifying other manifests"
sh """
aws codeartifact login --tool pip --repository deployment --domain aqua-deployment --domain-owner 172746256356
pip install aqua-deployment
/bin/bash k3s/prepare.sh
"""
}
}
}
}
stage("Updating Consul") {
when {
allOf {
not { expression { return Global.CHANGED_MANIFESTS_FILES.isEmpty() } }
expression { return deployments.runCloudFormation(CHANGE_TARGET) }
}
}
steps {
script {
helm.updateConsul("create")
log.info "Updated Consul successfully"
}
}
}
stage("Running Mstp Tests") {
when {
allOf {
not { expression { return Global.CHANGED_MANIFESTS_FILES.isEmpty() } }
expression { return deployments.runCloudFormation(CHANGE_TARGET) }
}
}
steps {
script {
log.info "Running Mstp tests"
helm.runMstpTestsManifests debug: debug
}
}
}
}
post {
always {
script {
try{
helm.updateConsul("delete")
orchestrator.uninstall()
echo "k3s uninstalled"
helm.removeDockerLocalImages()
cleanWs()
}
catch(err){
cleanWs()
helm.removeDockerLocalImages()
}
// notifyFullJobDetailes subject: "${env.JOB_NAME} Pipeline | ${currentBuild.result}", emails: userEmail
}
}
}
}