forked from azure-devops/javawebappsample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile_k8s_azcli
49 lines (45 loc) · 1.66 KB
/
Jenkinsfile_k8s_azcli
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
import groovy.json.JsonSlurper
def getAcrLoginServer(def acrSettingsJson) {
def acrSettings = new JsonSlurper().parseText(acrSettingsJson)
return acrSettings.loginServer
}
node {
stage('init') {
checkout scm
}
stage('build') {
sh 'mvn clean package'
}
stage('deploy') {
def acrName = '<myRegistry>'
def imageName = 'calculator'
// 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
withCredentials([azureServicePrincipal('<myServicePrincipal>')]) {
// login 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
'''
// get login server
def acrSettingsJson = sh script: "az acr show -n $acrName", returnStdout: true
def loginServer = getAcrLoginServer acrSettingsJson
// 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()
// update deployment.yaml with latest tag
sh "sed 's/\$version/$version/g' deployment.yaml > target/deployment.yaml"
// update deployment
sh 'kubectl apply -f target/deployment.yaml'
// log out
sh 'az logout'
sh "docker logout $loginServer"
}
}
}