forked from msa-school/model-for-ops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
81 lines (70 loc) · 3.22 KB
/
Jenkinsfile
File metadata and controls
81 lines (70 loc) · 3.22 KB
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
pipeline {
agent any
environment {
REGISTRY = 'user00.azurecr.io'
SERVICES = 'order,delivery,product' // fix your microservices
AKS_CLUSTER = 'user00-aks'
RESOURCE_GROUP = 'user00-rsrcgrp'
AKS_NAMESPACE = 'default'
AZURE_CREDENTIALS_ID = 'Azure-Cred'
TENANT_ID = 'f46af6a3-e73f-4ab2-a1f7-f33919eda5ac'
}
stages {
stage('Clone Repository') {
steps {
checkout scm
}
}
stage('Build and Deploy Services') {
steps {
script {
def services = SERVICES.tokenize(',') // Use tokenize to split the string into a list
for (int i = 0; i < services.size(); i++) {
def service = services[i] // Define service as a def to ensure serialization
dir(service) {
stage("Maven Build - ${service}") {
withMaven(maven: 'Maven') {
sh 'mvn package -DskipTests'
}
}
stage("Docker Build - ${service}") {
def image = docker.build("${REGISTRY}/${service}:v${env.BUILD_NUMBER}")
}
stage('Azure Login') {
withCredentials([usernamePassword(credentialsId: env.AZURE_CREDENTIALS_ID, usernameVariable: 'AZURE_CLIENT_ID', passwordVariable: 'AZURE_CLIENT_SECRET')]) {
sh 'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET --tenant ${TENANT_ID}'
}
}
stage("Push to ACR - ${service}") {
sh "az acr login --name ${REGISTRY.split('\\.')[0]}"
sh "docker push ${REGISTRY}/${service}:v${env.BUILD_NUMBER}"
}
stage("Deploy to AKS - ${service}") {
sh "az aks get-credentials --resource-group ${RESOURCE_GROUP} --name ${AKS_CLUSTER}"
sh 'pwd'
sh """
sed 's/latest/v${env.BUILD_ID}/g' kubernetes/deployment.yaml > output.yaml
cat output.yaml
kubectl apply -f output.yaml
kubectl apply -f kubernetes/service.yaml
rm output.yaml
"""
}
}
}
}
}
}
stage('CleanUp Images') {
steps {
script {
def services = SERVICES.tokenize(',')
for (int i = 0; i < services.size(); i++) {
def service = services[i]
sh "docker rmi ${REGISTRY}/${service}:v${env.BUILD_NUMBER}"
}
}
}
}
}
}