-
Notifications
You must be signed in to change notification settings - Fork 3
/
Jenkinsfile
237 lines (229 loc) · 8.92 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
pipeline {
agent { label 'docker-slave' }
environment {
// OPENSTACK SETTINGS
ssh_key_name = "jenkins-opera"
image_name = "centos7"
network_name = "orchestrator-network"
security_groups = "default,sodalite-remote-access,sodalite-rest,sodalite-uc"
flavor_name = "m1.medium"
// DOCKER SETTINGS
docker_network = "sodalite"
//docker_registry_ip = credentials('jenkins-docker-registry-ip')
docker_public_registry_url = "registry.hub.docker.com"
xopera_endpoint = credentials('xopera-http-endpoint')
modak_endpoint = "http://modak.sodalite.eu:5000"
// OPENSTACK DEPLOYMENT 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"
// 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 iac-blueprint-builder') {
steps {
sh """ #!/bin/bash
python3 -m venv venv-test
. venv-test/bin/activate
pip3 install -r requirements.txt
pip3 install -e .
python3 -m pytest --pyargs -s ${WORKSPACE}/test --junitxml="results.xml" --cov=src --cov-report xml test/
"""
junit 'results.xml'
}
}
stage('SonarQube analysis'){
environment {
scannerHome = tool 'SonarQubeScanner'
}
steps {
withSonarQubeEnv('SonarCloud') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage('Build iac-blueprint-builder') {
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 iac-blueprint-builder
"""
}
}
stage('Push iac-blueprint-builder 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 iac-blueprint-builder sodaliteh2020 staging
"""
}
}
}
stage('Push iac-blueprint-builder 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 iac-blueprint-builder 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.4 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 -r -f openstack-blueprint/modules/
git clone -b 3.4.1 https://github.com/SODALITE-EU/iac-modules.git openstack-blueprint/modules/
cp ${ca_crt_file} openstack-blueprint/modules//docker/artifacts/ca.crt
cp ${ca_crt_file} openstack-blueprint/modules//misc/tls/artifacts/ca.crt
cp ${ca_key_file} openstack-blueprint/modules//docker/artifacts/ca.key
cp ${ca_key_file} openstack-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 = 'iac-blueprint-builder-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 < openstack-blueprint/input.yaml.tmpl > openstack-blueprint/input.yaml
. venv-deploy/bin/activate
cd openstack-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 = 'iac-blueprint-builder'
}
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 < openstack-blueprint/input.yaml.tmpl > openstack-blueprint/input.yaml
. venv-deploy/bin/activate
cd openstack-blueprint
rm -r -f .opera
opera deploy service.yaml -i input.yaml
"""
}
}
}
}
post {
failure {
slackSend (color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
fixed {
slackSend (color: '#6d3be3', message: "FIXED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}