diff --git a/Jenkinsfile b/Jenkinsfile index bb4b4b52..45c233f2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -8,33 +8,70 @@ def getFtpPublishProfile(def publishProfilesJson) { } node { - withEnv(['AZURE_SUBSCRIPTION_ID=', - 'AZURE_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 = '' - def webAppName = '' - // login Azure - withCredentials([usernamePassword(credentialsId: '', 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 + ] +}