4

I have the following problem and I'm surprised that I can't find any straightforward solution on SO or MSDN.

I have existing *.pubxml profiles in several of my web applications and I would like to execute post deployment script - powershell script - which reorganizes WebSite and its child applications slightly.

I'm not usign Web Deployment Package - just Web Deploy.

The script is deployed successfully but the problem is - how should I execute it automatically after deployment?

I have two scenarios:

  1. Execute by simply "Publish..." from Visual Studio.
  2. Execute as part of TFS Build definition (TFS 2013).
6
  • 1) exciting Publish from Visual Studio is an extremely bad practice. 2) Executing scripts as part of a build process is trivial. What have you tried? Commented Feb 25, 2017 at 20:13
  • 1) ok, but I already have it and it works fine. 2) I need to execute it as part of post DEPLOY. I know how to execute pre or post build scripts. I have script which is deployed to the destination location and I need a way to execute it. Thats all. Commented Feb 25, 2017 at 20:21
  • You execute it as part of the build. Don't deploy from VS only from a Build. Commented Feb 26, 2017 at 7:55
  • Could you be more specific? As I wrote, I have two options - one from VS and one from TFS Build Definition. In TFS build definition I pass /p:DeployOnBuild=true to msbuild.exe and I'm pointing to publish profile from the solution. This all works as expected, problematic is to add powershell step after everything is already on the server. Commented Feb 26, 2017 at 9:27
  • You can't add additional Scripts using that method. It's the "quick and dirty" option for those with simple needs. You need to create the package in one step, and run the PowerShell in another step. Commented Feb 26, 2017 at 13:13

2 Answers 2

2

You can try to define a “Target” by MSBuild to achieve your requirement.

For the first scenario:

The Visual Studio build process is defined by a series of MSBuild .targets files that are imported into your project file. One of these imported files, Microsoft.Common.targets. This file contains a set of predefined empty targets that are called before and after some of the major targets in the build process.

So you can define a "Target" element whose "AfterTarget" attribute's value is set to "MSDeployPublish":

<Target Name="CustomPostPublishActions" AfterTargets="MSDeployPublish" >
    <Exec Command="..\PostDeploymentScript.sp1 " />
</Target>

For the second scenario:

You can add a PowerShell build task as MrHinsh`s suggestion.

Sign up to request clarification or add additional context in comments.

1 Comment

Will PostDeploymentScript.sp1 being run in web app cloud environment?
1

You should switch to deploying from Build & Release only in VSTS/TFS.

enter image description here

You can then add a PowerShell build task and either point at a script or use Inline if it's short. If it is a script that you use in many builds you might want to write your own build task.

1 Comment

I'm using TFS 2013 On Premise for now. But thank you for the tip anyway. I will use TeamCity as it is free for small teams and has the same functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.