forked from azure-devops/javawebappsample
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Jenkinsfile2
49 lines (45 loc) · 1.74 KB
/
Jenkinsfile2
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 webAppResourceGroup = '<myResourceGroup>'
def webAppName = '<app_name>'
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('<mySrvPrincipal>')]) {
// 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 web app docker settings
sh "az webapp config container set -g $webAppResourceGroup -n $webAppName -c $imageWithTag -r http://$loginServer -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET"
// log out
sh 'az logout'
sh "docker logout $loginServer"
}
}
}