-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathJenkinsfile
More file actions
77 lines (64 loc) · 2.23 KB
/
Jenkinsfile
File metadata and controls
77 lines (64 loc) · 2.23 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
68
69
70
71
72
73
74
75
76
77
import groovy.json.JsonSlurper
def getFtpPublishProfile(def publishProfilesJson) {
def pubProfiles = new JsonSlurper().parseText(publishProfilesJson)
for (p in pubProfiles)
if (p['publishMethod'] == 'FTP')
return [url: p.publishUrl, username: p.userName, password: p.userPWD]
}
node {
withEnv([
'AZURE_SUBSCRIPTION_ID=3a08d5f2-f291-4e2f-a8ac-5db510eeda18',
'AZURE_TENANT_ID=b7df592e-345a-402d-8ed0-08cdd72ceb56'
]) {
stage('init') {
checkout scm
}
stage('build') {
// this is what the sample project uses
sh 'mvn clean package'
}
stage('deploy') {
// 👉 your Azure info
def resourceGroup = 'jenkins-get-started-rg'
def webAppName = 'jenkins-hexiaoxu-app'
// 👉 login to Azure using the service principal you created in Jenkins
withCredentials([
usernamePassword(
credentialsId: 'jenkins-azure-sp',
passwordVariable: 'AZURE_CLIENT_SECRET',
usernameVariable: 'AZURE_CLIENT_ID'
)
]) {
sh """
az login --service-principal \
-u $AZURE_CLIENT_ID \
-p $AZURE_CLIENT_SECRET \
-t $AZURE_TENANT_ID
az account set --subscription $AZURE_SUBSCRIPTION_ID
"""
}
// 👉 get publish profile of the web app
def pubProfilesJson = sh(
script: "az webapp deployment list-publishing-profiles -g ${resourceGroup} -n ${webAppName} --output json",
returnStdout: true
).trim()
def ftpProfile = getFtpPublishProfile(pubProfilesJson)
// 👉 upload the WAR Jenkins just built
// your mvn build made target/calculator-1.0.war (that’s from the lab sample)
sh "curl -T target/calculator-1.0.war ${ftpProfile.url}/webapps/ROOT.war -u '${ftpProfile.username}:${ftpProfile.password}'"
// 👉 logout
sh 'az logout'
}
}
}
// ===== helper =====
def getFtpPublishProfile(String jsonText) {
def json = new groovy.json.JsonSlurper().parseText(jsonText)
// in Web App publish profiles, FTP profile usually has publishMethod == "FTP"
def ftp = json.find { it.publishMethod == 'FTP' }
return [
url : "ftp://${ftp.publishUrl}",
username: ftp.userName,
password: ftp.userPWD
]
}