-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJenkinsfile_k8s_azcli
More file actions
67 lines (60 loc) · 2.4 KB
/
Jenkinsfile_k8s_azcli
File metadata and controls
67 lines (60 loc) · 2.4 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
import groovy.json.JsonSlurper
def parseJsonFromOutput(cmd, def callback) {
def json = sh script: cmd, returnStdout: true
return callback(new JsonSlurper().parseText(json))
}
node {
stage('init') {
// git clone
checkout scm
}
stage('build') {
// maven build
sh 'mvn clean package'
}
stage('deploy') {
def acrGroup = 'kenchenacrtest'
def acrName = 'kenchenacrtest'
def acsGroup = 'kenchenacskube1'
def acsName = 'kenchenacskube1'
def imageName = 'calculator'
def azureCred = 'vs_china_jenkins'
def acsCred = 'kenchenacskube1'
withCredentials([azureServicePrincipal(azureCred)]) {
// login to Azure
sh '''
az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID
az account set -s $AZURE_SUBSCRIPTION_ID
'''
// docker build
// generate version, it's important to remove the trailing new line in git describe output
def version = sh script: 'git describe | tr -d "\n"', returnStdout: true
// get login server
def loginServer = parseJsonFromOutput("az acr show -g $acrGroup -n $acrName", { o -> o.loginServer })
// login docker
// docker.withRegistry only supports credential ID, so use native docker command to login
// you can also use docker.withRegistry if you add a credential
sh "docker login -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET $loginServer"
// build image
def imageWithTag = "$loginServer/$imageName:$version"
def image = docker.build imageWithTag
// push image
image.push()
// logout docker
sh "docker logout $loginServer"
// get kubernetes config
def serverAndUser = parseJsonFromOutput("az acs show -g $acsGroup -n $acsName", { o -> [server: o.masterProfile.fqdn, user: o.linuxProfile.adminUsername] })
sshagent([acsCred]) {
sh "scp -o StrictHostKeyChecking=no $serverAndUser.user@$serverAndUser.server:.kube/config kubeconfig"
}
// update deployment.yaml with image tag and secret name and deploy
sh "kubectl apply -f service.yaml --kubeconfig=kubeconfig"
def escaped = imageWithTag.replace('/', '\\/')
sh "sed 's/\$IMAGE_TAG/$escaped/g;s/\$KUBERNETES_SECRET_NAME/$acrName/g' deployment.yaml | kubectl apply --kubeconfig=kubeconfig -f -"
// remote config file
sh 'rm kubeconfig'
// logout from Azure
sh 'az logout'
}
}
}