Skip to content

Instantly share code, notes, and snippets.

@SingingBush
Last active June 12, 2020 11:55
Show Gist options
  • Save SingingBush/b0ea858f0facd6ce8acb to your computer and use it in GitHub Desktop.
Save SingingBush/b0ea858f0facd6ce8acb to your computer and use it in GitHub Desktop.
This powershell script can be used within TeamCity to deploy a Java web app (war file) to a Tomcat installation on a remote Windows Server
<#
Deployment Script (SingingBush, 9-may-2014)
Usage:
In TeamCity 'Buildstep: Powershell' make sure that script is set to file and
in 'Failure Conditions' the checkbox is ticked for: 'the build process exit code is not zero'.
YOU MUST ENSURE THAT ALL THE FOLLOWING ARGS ARE USED:
$server - The hostname for the machine being deployed to.
$path - The full directory path on the remote server to correct Tomcat webapps folder
$service - The service name for the application
$war - The war filename (excluding extension), eg 'mywebapp'
$buildType - The type of build to get from the TeamCity repo, eg: 'MyWebapp_Main';
$environment - [Optional] Handy when more than one of same application in one IIS, suggested values are:
'-live', '-uat' or '-systest'. It will be appended to the war file name.
#>
param (
[string]$server = $(throw "-server is required."),
[string]$path = $(throw "-path is required."),
[string]$service = $(throw "-service is required"),
[string]$war = $(throw "-war is required"),
[string]$buildType = $(throw "-buildType is required")
)
$teamCity = "http://REMOTE_HOSTNAME/teamcity/repository/download";
net use \\$remoteServer /user:USERNAME_HERE PASSWORD_HERE
try {
Write-Host ("Attempting to get the '{0}' service on {1}" -f $service, $remoteServer)
$svc = get-service -computerName $remoteServer -name $service
Write-Host ("Service is currently {0}" -f $svc.Status)
if ($svc.Status -eq "Running") {
Write-Host ("Stopping {0} service" -f $service)
$svc.Stop()
# wait for 10 seconds
$svc.WaitForStatus('Stopped','00:00:10')
# if still not stopped, then error
if ($svc.Status -ne 'Stopped') {
Write-Host 'Service not stopped, exiting'
exit 1
}
}
} Catch [System.Management.Automation.MethodInvocationException] {
Write-Host ("There was an Exception while trying to stop the {0} service - Exiting with ERROR 1" -f $service)
exit 1
}
$storageDir = $pwd
$url = "$teamCity/$buildType/.lastFinished/$war.war?guest=1"
$file = "$storageDir\$war$environment.war"
try {
$webclient = New-Object System.Net.WebClient
Write-Host ("Downloading {0}.war from TeamCity" -f $war)
$webclient.DownloadFile($url,$file)
} Catch [System.Net.WebException] {
Write-Host ("There was an Exception while trying to download {0} - Exiting with ERROR 1" -f $url)
exit 1
}
try {
Write-Host "removing old war file..."
Remove-Item \\$remoteServer\$remotePath\$war -recurse
Remove-Item \\$remoteServer\$remotePath\$war.war
} catch [System.Management.Automation.ItemNotFoundException] {
Write-Host ("Unable to remove '{0}', Item Not Found!" -f $remotePath)
# will continue rather than exit here as it's no problem if there is nothing to delete
}
try {
Write-Host ("Copying new {0}.war to application server..." -f $war)
copy-item -Path $file -Destination \\$remoteServer\$remotePath
} Catch [System.IO.DirectoryNotFoundException] {
Write-Host ("The destination directory '{0}' was not found! Exiting with ERROR 1" -f $remotePath)
exit 1
}
try {
$svc = get-service -computerName $remoteServer -name $service
Write-Host ("Service is currently {0}, calling start()" -f $svc.Status)
$svc.Start()
} Catch [System.Management.Automation.MethodInvocationException] {
Write-Host ("There was an Exception while trying to start the {0} service - Exiting with ERROR 1" -f $service)
exit 1
}
Write-Host "Deployment Completed Successfully"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment