println("Job name: ${JOB_NAME}") switch(JOB_NAME) { case "production.helpcenter.onlyoffice.com.pl": DESTSITE = 'helpcenter.teamlab.com' DESTHOST = '54.186.188.62' MONITOR_ID = '58439000008116012' break case "release.test-helpcenter.onlyoffice.pl": DESTSITE = 'test-helpcenter.teamlab.com' DESTHOST = '54.186.188.62' break default: println("JOB: ${JOB_NAME} not known.") System.exit(1) } println("SITE: ${DESTSITE}") pipeline { agent { label 'master' } options { disableConcurrentBuilds() buildDiscarder (logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5')) } parameters { choice(name: 'DEPLOY', choices: ['False','True'],description: 'Run deploy? Set "True" to run, "False" to update job') } stages { stage('build') { when { expression { return env.DEPLOY == "True" } } environment { DESTSITE = "${DESTSITE}" DESTHOST = "${DESTHOST}" } steps { withCredentials([usernamePassword(credentialsId: 'jenkins_user_api', passwordVariable: 'jenkins_token', usernameVariable: 'jenkins_user')]) { powershell ''' $ErrorActionPreference='Stop' (get-content Web\\web.config) | %{$_ -replace '', ''} | set-content Web\\web.config (get-content sync.bat) | %{$_ -replace "%poolname%", "$Env:DESTSITE"} | set-content sync.bat if ( $Env:JOB_NAME -eq "production.helpcenter.onlyoffice.com.pl") { (get-content publish.proj) | %{$_ -replace 'Updateable="true"', 'Updateable="false"'} | set-content publish.proj } ''' bat ''' echo " == Build == " %msbuild% %WORKSPACE%\\publish.proj /t:PrecompileWeb if not %errorlevel% == 0 ( echo "Error: cannot deploy the project" exit -1 ) echo "Add changelog.xml for check build information" curl -k -s -u %jenkins_user%:%jenkins_token% "%BUILD_URL%api/xml" > %WORKSPACE%\\Precompiled\\changelog.xml echo " == Precompile == " %aspnetmerge% "%WORKSPACE%\\Precompiled" -w ASC.dll ''' } } } stage('stop_monitor') { when { allOf { expression { return env.DEPLOY == "True" }; expression { return env.JOB_NAME == 'production.helpcenter.onlyoffice.com.pl' } } } steps { build job: 'monitor.24x7.start.stop', parameters: [string(name: 'monitor_id', value: "${MONITOR_ID}"), string(name: 'action', value: 'suspend')] } } stage('deploy') { when { expression { return env.DEPLOY == "True" } } environment { DESTSITE = "${DESTSITE}" DESTHOST = "${DESTHOST}" } steps { withCredentials([usernamePassword(credentialsId: 'sites.onlyoffice.com_21H1', passwordVariable: 'deploymentpass', usernameVariable: 'deploymentuser')]) { bat ''' echo " == Deploy == " %msdeployv2% -verb:sync -source:iisapp="%WORKSPACE%\\Precompiled" -dest:iisapp="%DESTSITE%",computerName="%DESTHOST%",username=%deploymentuser%,password=%deploymentpass% -preSync:runCommand="%WORKSPACE%\\sync.bat",waitInterval=120000 -postSync:runCommand="%WORKSPACE%\\sync.bat",waitInterval=120000 ''' } } } stage('notify telegram') { when { allOf { expression { return env.DEPLOY == "True" }; expression { return env.JOB_NAME == 'production.helpcenter.onlyoffice.com.pl' } } } steps { withCredentials([string(credentialsId: 'chat_dep_automation_operation', variable: 'TEL_CHAT'), string(credentialsId: 'token_bot_jenkins_notifier', variable: 'TEL_TOKEN')]) { powershell ''' $MESSAGE = "helpcenter.onlyoffice.com is deployed." $TOKEN = "$Env:TEL_TOKEN" $CHAT_ID = "$Env:TEL_CHAT" $URL = "https://api.telegram.org/bot$TOKEN/sendMessage" if ( $Env:BRANCH_NAME -eq "production_deploy") { Invoke-WebRequest -Uri $URL -Body @{chat_id=$CHAT_ID; text=$MESSAGE} -UseBasicParsing } ''' } } } stage('start_monitor') { when { allOf { expression { return env.DEPLOY == "True" }; expression { return env.JOB_NAME == 'production.helpcenter.onlyoffice.com.pl' } } } steps { powershell ''' [int]$statusCode = 0 for ($i=30; $i -gt 0; $i--) { $statusCode = (cmd.exe /c "curl --connect-timeout 2 -L -s -o NUL -w %{http_code} https://helpcenter.onlyoffice.com/") | Out-String if ( $statusCode -eq 200 ) { exit 0 } else { Write-Host "helpcenter.onlyoffice.com not available" Start-Sleep -s 60 } } Write-Host "helpcenter.onlyoffice.com do not available after deploy within 30 minutes" ''' build job: 'monitor.24x7.start.stop', parameters: [string(name: 'monitor_id', value: "${MONITOR_ID}"), string(name: 'action', value: 'activate')] } } } }