forked from azure-devops/javawebappsample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile14
55 lines (47 loc) · 2.28 KB
/
Jenkinsfile14
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
import groovy.json.JsonSlurper
def getAcrLoginServer(def acrSettingsJson) {
def acrSettings = new JsonSlurper().parseText(acrSettingsJson)
return acrSettings.loginServer
}
node {
withEnv(['AZURE_SUBSCRIPTION_ID=783ed336-9aff-4c71-b6c0-8efb879033a5',
'AZURE_TENANT_ID=cc0361ca-deca-4db3-b421-ee1ca53c7f00']) {
stage('init') {
checkout scm
}
stage('build') {
sh 'mvn clean package'
}
stage('deploy') {
"az login --service-principal --username 6a174562-61ac-45e2-b6b9-334a797c00ce --password SWKn1aWnNy-tu_Jn28_~GVdd.5y1gyp2eM --tenant cc0361ca-deca-4db3-b421-ee1ca53c7f00"
def webAppResourceGroup = 'testnsv'
def webAppName = 'testnsv'
def acrName = 'testnsv'
def imageName = 'monimage'
// 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: 'thejenkinsID', passwordVariable: 'AZURE_CLIENT_SECRET', usernameVariable: 'AZURE_CLIENT_ID')]) {
// login Azure
sh ('az login --service-principal --username $AZURE_CLIENT_ID --password $AZURE_CLIENT_SECRET --tenant $AZURE_TENANT_ID')
sh '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 $acrName.azurecr.io -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"
}
}
}
}