- 1 year ago
TeamCity and "net stop" command for deploy windows service
To stop a Windows service as part of your deployment process in TeamCity, you can use the net stop
command in a PowerShell script. TeamCity allows you to run custom PowerShell scripts during the deployment process. Here's a step-by-step guide on how to achieve this:
Create a PowerShell Script:
Create a PowerShell script (e.g.,Stop-MyService.ps1
) that will stop your Windows service using thenet stop
command.powershell# Stop-MyService.ps1
$serviceName = "MyServiceName"
net stop $serviceName
Replace
"MyServiceName"
with the actual name of your Windows service that you want to stop.Commit the Script to Your Source Control:
Commit theStop-MyService.ps1
script to your source control repository where TeamCity can access it during the deployment process.Configure TeamCity Build Step:
In your TeamCity project, create a new build configuration or update an existing one to include a build step for stopping the Windows service.- Go to your TeamCity project and navigate to the "Build Configuration Settings."
- Click on "Build Steps" and then click "Add build step."
- Choose "PowerShell" from the runner type dropdown.
- In the "Script" field, enter the relative path to your
Stop-MyService.ps1
script in the source code repository. - Click "Save" to save the build step.
Run the Build Configuration:
Now, when you run your build configuration, TeamCity will execute the PowerShell script, which will stop the specified Windows service using thenet stop
command.
Please note that stopping a Windows service might require administrator privileges. Ensure that the user running the TeamCity build agent has the necessary permissions to stop the service.
If you need to deploy the updated service binaries and start the service again, you can add additional build steps to copy the new files to the service directory and use the net start
command to start the service after stopping it.