TeamCity Build Parameters for PowerShell
Tags: Powershell, TeamCity
In this article I will describe how you can add custom parameters to your TeamCity build configuration and read them in Powershell. Although adding parameters is not difficult and it is documented ok it is not really straightforward and you'd need a few trials until get it right.
Let's say we have a web project which needs to be deployed. For this purpose we'll add two parameters:
- A boolean with the name deploy
- A path where the service will be deployed.
This is just an example of how to set two parameters and read them in powershell. You can use this for your own scenarios, not just for setting up deployment
Open your TeamCity configuration project and on the Configuration Steps click the Build Parameters. Click 'Add new parameter' button and provide the name 'Deploy'. Select Configuration parameter from the Kind drop down. Type false in the Value field. Click on the Edit… button, give it a label and description. In Type select Checkbox and set Checked value to true and Unchecked to false. Click Save.
Add new parameter for the deployment path. I named it DeployPath with Text Type and Allowed Value to Any
Next we need to add a PowerShell script. In the TeamCity Build Steps add a new build and set the type drop down to Powershell.
Create a PowerShell file on your disk and set the path to that file (in the Script file text box).
In the Script arguments add the following:
Open the PowerShell file you’ve created on the hard disck and add the following lines.
param ( [Parameter(Mandatory=$false)] [String]$deploy, [Parameter(Mandatory=$false)] [String]$deployPath ) Write-Host "Deploy " + $deploy Write-Host "Deploy path is: " + $deployPath
Now you need to add the part which will deploy your web site or do whatever you need.
You can read the TeamCity parameters inside PowerShell but I prefer to set them outside of PowerShell and feed to PowerShell as command parameters. In this way your script doesn't depend on TeamCity and you can test it locally.