Build and release pipelines in the Azure DevOps can have complex structure. Sometimes there is a need to add PowerShell as one of the steps in these pipelines. Why? For instance to update content of the files from the repository or to use some Azure PowerShell cmdlets to make some updates. In this article I would like to present how to use PowerShell Arguments in the Azure DevOps build and release pipelines.
PowerShell Arguments in the Build pipelines
Let’s start from the Build pipelines and PowerShell task. First we have to create sample PowerShell script which will be stored in the GIT repository. We will use this script in the Build piepeline step. I put this script in the “config” folder located in the “master” branch:
Below I present the content of this specific script. What we would like to achieve is to retrieve two variables and pass them to the script as parameters. There are two parameters:
These are declared in the “Variables” tab in the Build configuration:
[CmdletBinding()] param ( $ApiManagementServiceName, $ApiManagementServiceResourceGroup ) $apimServiceName = $ApiManagementServiceName $resourceGroupName = $ApiManagementServiceResourceGroup Write-Host "Api Management Service Name: $($apimServiceName)" Write-Host "Api Management Resource Group Name: $($resourceGroupName)"
Now let’s analyze build step. For this example lets add only one step with PowerShell script file from the repository above.
As you can see in the “Arguments” field we have to declare which Variables we want to pass to the PowerShell script as parameters:
-ApiManagementServiceName $(ApiManagementServiceName) -ApiManagementServiceResourceGroup $(ApiManagementServiceResourceGroup)
Now in the PowerShell script we can get these Variables as params:
[CmdletBinding()] param ( $ApiManagementServiceName, $ApiManagementServiceResourceGroup )
Then we can use them normally in the script:
$apimServiceName = $ApiManagementServiceName $resourceGroupName = $ApiManagementServiceResourceGroup Write-Host "Api Management Service Name: $($apimServiceName)" Write-Host "Api Management Resource Group Name: $($resourceGroupName)"
Try to run Build and see the result:
Open step with PowerShell script and see the logs. You should see names declared in the Variables earlier:
PowerShell Arguments in the Release pipelines
Steps for the Release pipelines are quite the same. First we have to publish PowerShell script from the repository in the Build artifact.
Now configure new Release pipeline and connect Build artifact with it:
Then we have to add Variables that we want to pass:
Now select PowerShell script from the artifacts:
Now we have to declare “Arguments” exactly like in the Build pipeline step:
Creat new release and verify logs:
In this article I presented how to inject Variables into PowerShell scripts which are the part of the Build and Release pipelines. Of course this is just a simple example but you can also use this method for more advanced scripts and scenarios. For instance you could get secrets from the Azure Key Vault, load them into Variables Groups and then access them from the PowerShell script.