dotnet publish: AfterTargets="AfterPublish" triggers before publish is complete

catmanjan 111 Reputation points
2025-02-26T22:37:25.7833333+00:00

Can someone look at this Github issue and help resolve it for us?

https://github.com/dotnet/sdk/issues/46999

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,960 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sirra Sneha 550 Reputation points Microsoft External Staff Moderator
    2025-03-26T05:16:51.2966667+00:00

    Hi @catmanjan,

    • Modify your .csproj by replacing AfterTargets="AfterPublish" with BeforeTargets="AfterPublish" in the RestartPod target.

    This ensures it executes just before AfterPublish, which may resolve the issue if AfterPublish isn't recognized properly.

    Here's the .csproj :

    <?xml version="1.0" encoding="utf-8"?>
    <!--
    https://go.microsoft.com/fwlink/?LinkID=208121.
    -->
    <Project>
      <PropertyGroup>
        ...snip...
      </PropertyGroup>
      <Target Name="CustomActionsBeforePublish" BeforeTargets="BeforePublish">
        <Exec Command="az acr login -n icognitionhydra" />
      </Target>
      
      <Target Name="RestartPod" BeforeTargets="AfterPublish">
        <Exec Command="az aks get-credentials -g hydraKubernetes -n hydra" />
        <Exec Command="kubectl rollout restart deployment hydra" />
      </Target>
    </Project>
    
    

    BeforeTargets="AfterPublish" ensures the target runs right before AfterPublish, preventing any execution timing issues.
    .
    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.