pipeline {
  agent none
  stages {
    stage('Checkout') {
      agent any
        steps {
          /* calcolo numero versione... diverso x branch MASTER/DEVELOP */
          script {
            withEnv(['NEXT_BUILD_NUMBER=116']) {
              // env.versionNumber = VersionNumber(versionNumberString : '${BUILD_DATE_FORMATTED, "yyMM"}.${BUILDS_ALL_TIME}', projectStartDate : '2017-01-01', skipFailedBuilds: true)
              env.versionNumber = VersionNumber(versionNumberString : '${BUILD_DATE_FORMATTED, "yyMM"}.${BUILDS_ALL_TIME}', projectStartDate : '2017-01-01', skipFailedBuilds: true, overrideBuildsAllTime: '${NEXT_BUILD_NUMBER}')
                env.APP_NAME = 'MedAP'
            }
          }				
          script {
            currentBuild.displayName = "${env.versionNumber}"
            if (env.BRANCH_NAME == "develop" || env.BRANCH_NAME.contains("DEMO")) {
              currentBuild.description = "TEST ${env.versionNumber}"
            }
            else {
              currentBuild.description = "BUILD ${env.versionNumber}"
            }
          }
          /* CAMBIO numero versione in file sorgente!!! */
          bat "e:\\fart.exe ${WORKSPACE}\\VersGen\\VersGen.cs 000.000 ${env.versionNumber} || EXIT /B 0"
      }
    }
		stage('Build') {
			agent any
			steps {
				script {
					/* compilo tutti i progetti compresi... */
					if (env.BRANCH_NAME == "develop") {
						/* CAMBIO numero versione in file sorgente!!! */
						bat "e:\\fart.exe ${WORKSPACE}\\VersGen\\VersGen.cs 000.000 ${env.versionNumber} || EXIT /B 0"
						bat "e:\\nuget.exe restore ${WORKSPACE}\\MedAP.sln"
						parallel (
							BUILD: {
								sleep 0
								bat "\"${tool 'MSBuild-14.0'}\" MedAP/MedAP.csproj -target:Build /p:Configuration=Release /p:Platform=\"Any CPU\" /p:OutputPath=bin/ /m"
							},
							failFast: false)
						}
					else {
						echo 'Nothing to Build...'
					}	
				}			
			}	
    }
		stage('Test') {
			steps {
        echo 'Testing.. 2 be done...'
			}
		}
		stage('Deploy') {
			agent any
			steps {
				script {
					/* DEPLOY condizionale: develop / master */
					if (env.BRANCH_NAME == "master") { 
						/* CAMBIO numero versione in file sorgente!!! */
						bat "e:\\fart.exe ${WORKSPACE}\\VersGen\\VersGen.cs 000.000 ${env.versionNumber} || EXIT /B 0"
						bat "e:\\nuget.exe restore ${WORKSPACE}\\MedAP.sln"
						parallel (
							IIS01: {
								bat "\"${tool 'MSBuild-14.0'}\" \"/p:AspnetMergePath=C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v10.0A\\bin\\NETFX 4.6.2 Tools\" /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=IIS01.pubxml /p:VisualStudioVersion=14.0 /p:RunCodeAnalysis=false /p:Configuration=IIS01 /p:username=jenkins /p:Password=viadante16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ MedAP/MedAP.csproj /p:EnableMSDeployAppOffline=true /p:MSDeployUseChecksum=true"
							},
							IIS02: {
								bat "\"${tool 'MSBuild-14.0'}\" \"/p:AspnetMergePath=C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v10.0A\\bin\\NETFX 4.6.2 Tools\" /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=IIS02.pubxml /p:VisualStudioVersion=14.0 /p:RunCodeAnalysis=false /p:Configuration=IIS02 /p:username=jenkins /p:Password=viadante16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ MedAP/MedAP.csproj /p:EnableMSDeployAppOffline=true /p:MSDeployUseChecksum=true"
							},
							failFast: false)
					}
					else {
						echo 'Nothing to deploy...'
					}
				}
			}
		}
		stage('RemoteDeploy') {
			agent any
			steps {
				script {
					/* compilo installers in base al BRANCH del cliente... */
					if (env.BRANCH_NAME.contains("PROD-OVH")) {
						/* CAMBIO numero versione in file sorgente!!! */
						bat "e:\\fart.exe ${WORKSPACE}\\VersGen\\VersGen.cs 000.000 ${env.versionNumber} || EXIT /B 0"
						bat "e:\\nuget.exe restore ${WORKSPACE}\\MedAP.sln"
						parallel (
						WEBDEPLOY: {
							sleep 0
							bat "\"${tool 'MSBuild-14.0'}\" \"/p:AspnetMergePath=C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v10.0A\\bin\\NETFX 4.6.2 Tools\" /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=${env.BRANCH_NAME}.pubxml /p:VisualStudioVersion=14.0 /p:RunCodeAnalysis=false /p:Configuration=${env.BRANCH_NAME} /p:username=WPROD01\\Steamware /p:Password=viaD@nte16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ MedAP/MedAP.csproj /p:EnableMSDeployAppOffline=true /p:MSDeployUseChecksum=true"
						},
						failFast: false)
					}
					else if (env.BRANCH_NAME == "DEMO-OVH") {
						/* CAMBIO numero versione in file sorgente!!! */
						bat "e:\\fart.exe ${WORKSPACE}\\VersGen\\VersGen.cs 000.000 ${env.versionNumber} || EXIT /B 0"
						bat "e:\\nuget.exe restore ${WORKSPACE}\\MedAP.sln"
						parallel (
						WEBDEPLOY: {
							sleep 0
							bat "\"${tool 'MSBuild-14.0'}\" \"/p:AspnetMergePath=C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v10.0A\\bin\\NETFX 4.6.2 Tools\" /p:DeployOnBuild=true /p:Targets=Publish /p:PublishProfile=DEMO-OVH.pubxml /p:VisualStudioVersion=14.0 /p:RunCodeAnalysis=false /p:Configuration=DEMO-OVH /p:username=WPROD01\\Steamware /p:Password=viaD@nte16 /p:AllowUntrustedCertificate=true /p:OutputPath=bin/ MedAP/MedAP.csproj /p:EnableMSDeployAppOffline=true /p:MSDeployUseChecksum=true"
						},
						failFast: false)
					}
					else{
						echo 'Questo BRANCH non necessita di RemoteDeploy...'
					}
				}
			}
		}
		/*stage('Archive') {
            steps {
				echo 'Archiviazione build Release'
                archiveArtifacts artifacts: 'ScratchTest/ScratchTest/bin/Release/**' 				
            }
        }*/
  }
  post {
    success {
        mail to:"samuele@steamware.net", subject:"SUCCESS: ${currentBuild.fullDisplayName}", body: "Build passed."
    }
    failure {
        mail to:"samuele@steamware.net", subject:"FAILURE: ${currentBuild.fullDisplayName}", body: "Build failed."
    }
    unstable {
        mail to:"samuele@steamware.net", subject:"UNSTABLE: ${currentBuild.fullDisplayName}", body: "Build is unstable."
    }
    /*changed {
        mail to:"samuele@steamware.net", subject:"CHANGED: ${currentBuild.fullDisplayName}", body: "Build status changed."
    }*/
  }
}