-
Notifications
You must be signed in to change notification settings - Fork 566
/
Jenkinsfile2
52 lines (48 loc) · 2 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
50
51
52
import groovy.json.JsonSlurper
def getAcrLoginServer(def acrSettingsJson) {
def acrSettings = new JsonSlurper().parseText(acrSettingsJson)
return acrSettings.loginServer
}
node {
withEnv(['AZURE_SUBSCRIPTION_ID=<mySubscriptionId>',
'AZURE_TENANT_ID=<myTenantId>']) {
stage('init') {
checkout scm
}
stage('build') {
sh 'mvn clean package'
}
stage('deploy') {
def webAppResourceGroup = 'pcappservicelinuxrg'
def webAppName = '<resource_group>'
def acrName = '<app_name>'
def imageName = '<registry>'
// 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([usernamePassword(credentialsId: '<service_principal>', passwordVariable: 'AZURE_CLIENT_SECRET', usernameVariable: 'AZURE_CLIENT_ID')]) {
// 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"
}
}
}
}