PostBuild event stuck in infinite loop

28 Views Asked by At

I'm trying to define a PostBuildEvent that will publish the project and then build the docker image:

<Target Name="DockerBuild" AfterTargets="PostBuildEvent">
    <Message Text="========== PostBuildEvent - Building Docker Image ==========" Importance="high" />
    <Exec Command="dotnet publish $(ProjectDir)\MyProject.csproj -c Release -o ./app/publish /p:UseAppHost=true /p:PostBuildEvent=" />
    <Exec Command="docker build -t myimagename ." />
    <Exec Command="docker image prune --force" />
</Target>

However, this seems to trigger an infinite loop of build events.

I'm aware that dotnet publish will also do a build, but I thought that the /p:PostBuildEvent= parameter would negate that.

How can I configure the publish command to skip the PostBuildEvent?

1

There are 1 best solutions below

3
Xenohon On

/p:PostBuildEvent= sets the $(PostBuildEvent) property to '', which is very different from resetting the target PostBuildEvent*. See: MSBuild CLI Reference.

I suggest adding a Condition attribute to the Target and setting that instead.

<!-- Target will run if RunDockerBuild is ''(default), 'true', or anything else. -->
<Target Name="DockerBuild" AfterTargets="PostBuildEvent"
  Condition="'$(RunDockerBuild)' != 'false'">
    <Message Text="========== PostBuildEvent - Building Docker Image ==========" Importance="high" />
    <!--
      Update /p:PostBuildEvent= to /p:PostBuildEvent=false
      so that it matches the new condition above.
    -->
    <Exec Command="dotnet publish $(ProjectDir)\MyProject.csproj -c Release -o ./app/publish /p:UseAppHost=true /p:PostBuildEvent=false" />
    <Exec Command="docker build -t myimagename ." />
    <Exec Command="docker image prune --force" />
</Target>

*: The target PostBuildEvent can be reset using <Target Name="PostBuildEvent" />.