-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
264 lines (256 loc) · 10 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
pipeline {
agent { label 'docker-slave' }
environment {
// OPENSTACK SETTINGS
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,sodalite-uc"
flavor_name = "m1.medium"
// 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 = "dragan.radolovic@xlab.si"
cert_path = "/home/xopera/certs"
cert_files_prefix = "image.docker.local"
// POSTGRES SETTINGS
postgres_address = "xopera-postgres"
postgres_user = credentials('postgres-user')
postgres_password = credentials('postgres-password')
postgres_db = "postgres"
postgres_port = "5432"
// XOPERA SETTINGS
xopera_debug = "false"
xopera_log_level = "debug"
invocation_service_workers = '10'
// 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 xOpera') {
steps {
sh """ #!/bin/bash
python3 -m venv venv-test
. venv-test/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install --no-cache-dir -r requirements.txt
python3 -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ opera==0.6.9.dev2
./generate.sh
cd src/
python3 -m pytest --junitxml=results.xml --cov=./opera/api/ --cov=./opera/api/gitCsarDB --cov=./opera/api/blueprint_converters --cov=./opera/api/settings --cov=./opera/api/service --cov=./opera/api/util --cov=./opera/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 xopera-rest-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 xopera-rest-api
"""
}
}
stage('Push xopera-rest-api to DockerHub for staging') {
// Push during staging and production
when {
allOf {
expression{tag "*"}
expression{
TAG_STAGING == 'true'
}
}
}
steps {
withDockerRegistry(credentialsId: 'jenkins-sodalite.docker_token', url: '') {
sh """#!/bin/bash
./make_docker.sh push xopera-rest-api sodaliteh2020 staging
"""
}
}
}
stage('Push xopera-rest-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 xopera-rest-api sodaliteh2020 production
"""
}
}
}
stage('Install deploy dependencies') {
when {
allOf {
expression{tag "*"}
expression{
TAG_STAGING == 'true' || TAG_PRODUCTION == 'true'
}
}
}
steps {
sh """#!/bin/bash
rm -rf venv-deploy
python3 -m venv venv-deploy
. venv-deploy/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install opera[openstack]==0.6.4 docker
ansible-galaxy install geerlingguy.pip,2.1.0 --force
ansible-galaxy install geerlingguy.docker,3.1.2 --force
ansible-galaxy install geerlingguy.repo-epel,3.0.0 --force
rm -r -f xOpera-rest-blueprint/modules/
git clone -b 3.5.0 https://github.com/SODALITE-EU/iac-modules.git xOpera-rest-blueprint/modules/
cp ${ca_crt_file} xOpera-rest-blueprint/modules/docker/artifacts/ca.crt
cp ${ca_crt_file} xOpera-rest-blueprint/modules/misc/tls/artifacts/ca.crt
cp ${ca_key_file} xOpera-rest-blueprint/modules/docker/artifacts/ca.key
cp ${ca_key_file} xOpera-rest-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 = 'xOpera-dev'
}
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'xOpera_ssh_key', keyFileVariable: 'xOpera_ssh_key_file', usernameVariable: 'xOpera_ssh_username')]) {
sh """#!/bin/bash
# create input.yaml file from template
envsubst < xOpera-rest-blueprint/inputs/input.yaml.tmpl > xOpera-rest-blueprint/input.yaml
. venv-deploy/bin/activate
cd xOpera-rest-blueprint
rm -r -f .opera
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 = 'xOpera'
}
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'xOpera_ssh_key', keyFileVariable: 'xOpera_ssh_key_file', usernameVariable: 'xOpera_ssh_username')]) {
sh """#!/bin/bash
envsubst < xOpera-rest-blueprint/inputs/input.yaml.tmpl > xOpera-rest-blueprint/input.yaml
. venv-deploy/bin/activate
cd xOpera-rest-blueprint
rm -r -f .opera
opera deploy service.yaml -i input.yaml
"""
}
}
}
}
}