Invoke the Powershell Script during local testing with MSBuild

103 Views Asked by At

I am trying to call a powershell script for local testing

<Target Name="RunPowerShellScript" BeforeTargets="Build" Condition="'$(LocalBuild)' == 'true'">
<Exec LogStandardErrorAsError="true" WorkingDirectory="$(EnlistmentRoot)" Command="pwsh -ExecutionPolicy Bypass -File  &quot;$(EnlistmentRoot)\src\helloworld.ps1&quot;" />
    </Target>

The current setup isn’t functioning as expected. If I eliminate $(LocalBuild), it operates correctly. However, it’s also invoked in the ADO pipeline, and I prefer not to have it there.

I’ve set LocalBuild to false in the props file and incorporated a target in the build file under build.target.

$(LocalBuild)' == 'true'
1

There are 1 best solutions below

10
Kevin Lu-MSFT On

I want to run this script while building the project locally, not in azure devops pipeline. I want to skip there.

Based on your requirement, you can add the msbuild argument: /p:LocalBuild=false when using Azure DevOps Pipeline.

For example:

steps:
- task: VSBuild@1
  displayName: 'Build solution'
  inputs:
    msbuildArgs: ' /p:LocalBuild=false'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

In this case, you don't need to change the LocalBuild Value in the props file. And the RunPowerShellScript target will skip in Azure DevOps Pipeline.

The msbuild argument will override the value in the props file.

Update:

csproj file sample:

  <PropertyGroup>
    <LocalBuild>true</LocalBuild>
  </PropertyGroup>

......

  <Target Name="RunPowerShellScript" BeforeTargets="Build" Condition="'$(LocalBuild)' == 'true'">
    <Exec LogStandardErrorAsError="true" WorkingDirectory="$(EnlistmentRoot)" Command="pwsh -ExecutionPolicy Bypass -File  &quot;$(EnlistmentRoot)\src\helloworld.ps1&quot;" />
  </Target>

When we run the project on local machine, it will run the PowerShell script.

When we run the project in Azure Pipeline, we can set the msbuild argument: /p:LocalBuild=false. Then the build target will be skipped.