Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 55 additions & 18 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,70 @@ def getFtpPublishProfile(def publishProfilesJson) {
}

node {
withEnv(['AZURE_SUBSCRIPTION_ID=<subscription_id>',
'AZURE_TENANT_ID=<tenant_id>']) {
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') {
def resourceGroup = '<resource_group>'
def webAppName = '<app_name>'
// login Azure
withCredentials([usernamePassword(credentialsId: '<service_princial>', 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 -s $AZURE_SUBSCRIPTION_ID
'''

// 👉 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 settings
def pubProfilesJson = sh script: "az webapp deployment list-publishing-profiles -g $resourceGroup -n $webAppName", returnStdout: true
def ftpProfile = getFtpPublishProfile pubProfilesJson
// upload package
sh "curl -T target/calculator-1.0.war $ftpProfile.url/webapps/ROOT.war -u '$ftpProfile.username:$ftpProfile.password'"
// log out

// 👉 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
]
}