-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
290 lines (282 loc) · 11.2 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
280
281
282
283
284
285
286
287
288
289
290
pipeline {
agent { label 'docker-slave' }
environment {
// docker_registry_ip = credentials('jenkins-docker-registry-ip')
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"
ssh_key_name = "jenkins-opera"
image_name = "ubuntu"
username = "ubuntu"
OPERA_SSH_USER = "ubuntu"
network_name = "orchestrator-network"
security_groups = "default,sodalite-remote-access,sodalite-rest"
flavor_name = "m1.medium"
ca_crt_file = credentials('xopera-ca-crt')
ca_key_file = credentials('xopera-ca-key')
// OIDC secrets
oidc_endpoint = credentials('oidc-endpoint')
oidc_secret = credentials('oidc-secret')
auth_api_key = credentials('auth-api-key')
// XOPERA SETTINGS
image_builder_debug = "false"
image_builder_log_level = "debug"
// CI-CD vars
// When triggered from git tag, $BRANCH_NAME is actually 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 image-builder'){
steps {
sh """ #!/bin/bash
python3 -m venv venv-test
. venv-test/bin/activate
pip3 install --upgrade pip
pip3 install --no-cache-dir -r requirements.txt
./generate.sh
cd src/
python3 -m pytest image_builder --pyargs -s --junitxml=results.xml --cov=./image_builder/api/ --cov=./image_builder/tests --cov=./image_builder/api/settings --cov=./image_builder/api/service --cov=./image_builder/api/util --cov=./image_builder/api/controllers --cov-report xml
"""
junit 'src/results.xml'
}
}
stage('SonarQube analysis'){
environment {
scannerHome = tool 'SonarQubeScanner'
}
steps {
withSonarQubeEnv('SonarCloud') {
sh """ #!/bin/bash
${scannerHome}/bin/sonar-scanner
"""
}
}
}
stage('Build image-builder-api') {
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
./make_docker.sh build image-builder-api Dockerfile
"""
}
}
stage('Build image-builder-cli') {
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
./make_docker.sh build image-builder-cli Dockerfile-cli
"""
}
}
stage('Push image-builder-api to DockerHub for staging') {
when {
allOf {
expression{tag "*"}
expression{
TAG_STAGING == 'true'
}
}
}
steps {
withDockerRegistry(credentialsId: 'jenkins-sodalite.docker_token', url: '') {
sh """#!/bin/bash
./make_docker.sh push image-builder-api sodaliteh2020 staging
"""
}
}
}
stage('Push image-builder-cli to DockerHub for staging') {
when {
allOf {
expression{tag "*"}
expression{
TAG_STAGING == 'true'
}
}
}
steps {
withDockerRegistry(credentialsId: 'jenkins-sodalite.docker_token', url: '') {
sh """#!/bin/bash
./make_docker.sh push image-builder-cli sodaliteh2020 staging
"""
}
}
}
stage('Push image-builder-api 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 image-builder-api sodaliteh2020 production
"""
}
}
}
stage('Push image-builder-cli 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 image-builder-cli sodaliteh2020 production
"""
}
}
}
stage('Install deploy dependencies') {
when {
allOf {
expression{tag "*"}
expression{
TAG_STAGING == 'true' || TAG_PRODUCTION == 'true'
}
}
}
steps {
sh """#!/bin/bash
python3 -m venv venv-deploy
. venv-deploy/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install opera[openstack]==0.6.6 docker
ansible-galaxy install geerlingguy.pip,2.0.0 --force
ansible-galaxy install geerlingguy.docker,3.0.0 --force
ansible-galaxy install geerlingguy.repo-epel,3.0.0 --force
rm -rf image-builder-rest-blueprint/openstack/modules/
git clone -b 3.5.0 https://github.com/SODALITE-EU/iac-modules.git image-builder-rest-blueprint/openstack/modules
rm -rf image-builder-rest-blueprint/openstack/library/
cp -r image-builder-rest-blueprint/library/ image-builder-rest-blueprint/openstack/library/
cp ${ca_crt_file} image-builder-rest-blueprint/openstack/modules/docker/artifacts/ca.crt
cp ${ca_crt_file} image-builder-rest-blueprint/openstack/modules/misc/tls/artifacts/ca.crt
cp ${ca_key_file} image-builder-rest-blueprint/openstack/modules/docker/artifacts/ca.key
cp ${ca_key_file} image-builder-rest-blueprint/openstack/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 = 'image-builder-dev'
}
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'xOpera_ssh_key', keyFileVariable: 'xOpera_ssh_key_file', usernameVariable: 'xOpera_ssh_username')]) {
sh """#!/bin/bash
truncate -s 0 image-builder-rest-blueprint/openstack/input.yaml
envsubst < image-builder-rest-blueprint/openstack/input.yaml.tmpl > image-builder-rest-blueprint/openstack/input.yaml
cat image-builder-rest-blueprint/openstack/input.yaml
. venv-deploy/bin/activate
cd image-builder-rest-blueprint/openstack/
rm -rf .opera
opera deploy -i input.yaml service.yaml
"""
}
}
}
stage('Deploy to openstack for production') {
// Only on production tags
when {
allOf {
expression{tag "*"}
expression{
TAG_PRODUCTION == 'true'
}
}
}
environment {
// add env var for this stage only
vm_name = 'image-builder'
}
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'xOpera_ssh_key', keyFileVariable: 'xOpera_ssh_key_file', usernameVariable: 'xOpera_ssh_username')]) {
sh """#!/bin/bash
truncate -s 0 image-builder-rest-blueprint/openstack/input.yaml
envsubst < image-builder-rest-blueprint/openstack/input.yaml.tmpl > image-builder-rest-blueprint/openstack/input.yaml
cat image-builder-rest-blueprint/openstack/input.yaml
. venv-deploy/bin/activate
cd image-builder-rest-blueprint/openstack/
rm -rf .opera
opera deploy -i input.yaml service.yaml
"""
}
}
}
}
}