-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
279 lines (266 loc) · 9.95 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
pipeline {
options {
timeout(time: 3, unit: 'HOURS') // timeout on whole pipeline job
disableConcurrentBuilds()
}
agent { label 'docker-slave' }
environment {
// OPENSTACK SETTINGS
ssh_key_name = "jenkins-opera"
image_name = "centos7"
username = "centos"
network_name = "orchestrator-network"
security_groups = "default,sodalite-remote-access,sodalite-rest,sodalite-uc"
flavor_name = "m1.small"
// DOCKER SETTINGS
docker_network = "sodalite"
dockerhub_user = " "
dockerhub_pass = " "
//docker_registry_ip = credentials('jenkins-docker-registry-ip')
docker_registry_cert_country_name = "SI"
docker_registry_cert_organization_name = "XLAB"
docker_public_registry_url = "registry.hub.docker.com"
docker_registry_cert_email_address = "[email protected]"
cert_path = "/home/xopera/certs"
cert_files_prefix = "image.docker.local"
// OIDC secrets
oidc_endpoint = credentials('oidc-endpoint')
oidc_secret = credentials('oidc-secret')
auth_api_key = credentials('auth-api-key')
// VAULT
vault_url = credentials('vault-url')
// GIT SETTINGS
git_type = "gitlab"
git_server_url = "https://gitlab.com"
git_auth_token = credentials('git-auth-token')
// OPENSTACK DEPLOYMENT FALLBACK SETTINGS
OS_PROJECT_DOMAIN_NAME = "Default"
OS_USER_DOMAIN_NAME = "Default"
OS_PROJECT_NAME = "orchestrator"
OS_TENANT_NAME = "orchestrator"
OS_USERNAME = credentials('os-username')
OS_PASSWORD = credentials('os-password')
OS_AUTH_URL = credentials('os-auth-url')
OS_INTERFACE = "public"
OS_IDENTITY_API_VERSION = "3"
OS_REGION_NAME = "RegionOne"
OS_AUTH_PLUGIN = "password"
// ANSIBLE SETTINGS
ANSIBLE_TIMEOUT = "60"
// ROOT X.509 CERTIFICATES
ca_crt_file = credentials('xopera-ca-crt')
ca_key_file = credentials('xopera-ca-key')
// CI-CD vars
// When triggered from git tag, $BRANCH_NAME is actually GIT's tag_name
TAG_SEM_VER_COMPLIANT = """${sh(
returnStdout: true,
script: './validate_tag.sh SemVar $BRANCH_NAME'
)}"""
TAG_MAJOR_RELEASE = """${sh(
returnStdout: true,
script: './validate_tag.sh MajRel $BRANCH_NAME'
)}"""
TAG_PRODUCTION = """${sh(
returnStdout: true,
script: './validate_tag.sh production $BRANCH_NAME'
)}"""
TAG_STAGING = """${sh(
returnStdout: true,
script: './validate_tag.sh staging $BRANCH_NAME'
)}"""
}
stages {
stage ('Pull repo code from github') {
steps {
checkout scm
}
}
stage('Inspect GIT TAG'){
steps {
sh """ #!/bin/bash
echo 'TAG: $BRANCH_NAME'
echo 'Tag is compliant with SemVar 2.0.0: $TAG_SEM_VER_COMPLIANT'
echo 'Tag is Major release: $TAG_MAJOR_RELEASE'
echo 'Tag is production: $TAG_PRODUCTION'
echo 'Tag is staging: $TAG_STAGING'
"""
}
}
stage('Test MODAK') {
steps {
sh """ #!/bin/bash -l
set -eux
cd MODAK/
python3 -m venv venv-pre-commit
. venv-pre-commit/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install --no-cache-dir pre-commit
pre-commit run -a
"""
sh '''#! /bin/bash -l
set -eux
cd MODAK/
# Remove any container left from a previous run first
docker rm modak-unittest || :
docker build -t modak-unittest .
docker run --name modak-unittest modak-unittest pytest --cov-report xml:coverage.xml --junitxml=results.xml --cov=MODAK
docker cp modak-unittest:/opt/app/results.xml results-docker.xml
docker cp modak-unittest:/opt/app/coverage.xml coverage-docker.xml
docker rm modak-unittest
sed -i -e "s|/opt/app|$PWD|" coverage-docker.xml
'''
junit 'MODAK/results-*.xml'
}
}
stage('SonarQube analysis'){
environment {
scannerHome = tool 'SonarQubeScanner'
}
steps {
withSonarQubeEnv('SonarCloud') {
sh """ #!/bin/bash
${scannerHome}/bin/sonar-scanner
"""
}
}
}
stage('Build modak') {
when {
allOf {
// Triggered on every tag, that is considered for staging or production
expression{tag "*"}
expression{
TAG_STAGING == 'true' || TAG_PRODUCTION == 'true'
}
}
}
steps {
sh """#!/bin/bash
cd MODAK
../make_docker.sh build modak-api
"""
}
}
stage('Push modak to DockerHub for staging') {
when {
allOf {
expression{tag "*"}
expression{
TAG_STAGING == 'true'
}
}
}
steps {
withDockerRegistry(credentialsId: 'jenkins-sodalite.docker_token', url: '') {
sh """#!/bin/bash
set -x
git status
./make_docker.sh push modak-api sodaliteh2020 staging
"""
}
}
}
stage('Push modak to DockerHub') {
// Only on production tags
when {
allOf {
expression{tag "*"}
expression{
TAG_PRODUCTION == 'true'
}
}
}
steps {
withDockerRegistry(credentialsId: 'jenkins-sodalite.docker_token', url: '') {
sh """#!/bin/bash
./make_docker.sh push modak-api sodaliteh2020 production
"""
}
}
}
stage('Install deploy dependencies') {
when {
allOf {
expression{tag "*"}
expression{
TAG_STAGING == 'true' || TAG_PRODUCTION == 'true'
}
}
}
steps {
sh """#!/bin/bash
set -x
git status
rm -rf .venv
python3 -m venv .venv
. .venv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install -r deploy-requirements.txt
ansible-galaxy install geerlingguy.pip
ansible-galaxy install geerlingguy.docker
ansible-galaxy install geerlingguy.repo-epel
cp ${ca_crt_file} deploy-blueprint/modules/docker/artifacts/ca.crt
cp ${ca_crt_file} deploy-blueprint/modules/misc/tls/artifacts/ca.crt
cp ${ca_key_file} deploy-blueprint/modules/docker/artifacts/ca.key
cp ${ca_key_file} deploy-blueprint/modules/misc/tls/artifacts/ca.key
"""
}
}
stage('Deploy to openstack for staging') {
// Only on staging tags
when {
allOf {
expression{tag "*"}
expression{
TAG_STAGING == 'true'
}
}
}
environment {
// add env var for this stage only
vm_name = 'modak-dev'
}
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'xOpera_ssh_key', keyFileVariable: 'xOpera_ssh_key_file', usernameVariable: 'xOpera_ssh_username')]) {
sh """#!/bin/bash
set -x
# create input.yaml file from template
envsubst < deploy-blueprint/input/input.yaml.tmpl > deploy-blueprint/input.yaml
. .venv/bin/activate
cd deploy-blueprint
rm -r -f .opera
ssh-keygen -f "/home/jenkins/.ssh/known_hosts" -R "77.231.202.232"
opera deploy service.yaml -i input.yaml
"""
}
}
}
stage('Deploy to openstack for production') {
// Only on production tags
when {
allOf {
expression{tag "*"}
expression{
TAG_PRODUCTION == 'true'
}
}
}
environment {
vm_name = 'modak-prod'
}
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'xOpera_ssh_key', keyFileVariable: 'xOpera_ssh_key_file', usernameVariable: 'xOpera_ssh_username')]) {
sh """#!/bin/bash
set -x
# create input.yaml file from template
envsubst < deploy-blueprint/input/input.yaml.tmpl > deploy-blueprint/input.yaml
. .venv/bin/activate
cd deploy-blueprint
rm -r -f .opera
opera deploy service.yaml -i input.yaml
"""
}
}
}
}
}