Thursday, 31 October 2013

TFS - Lab Build - Deployment Script - Copy Build Output Zip to Lab Env. Machine and Extract

As a continuation to my last post "TFS - Lab Build - Create Zip File with Build Output in the Drop Folder", today I am going to explain how to use a deployment script with lab build (Build-Deply-Test workflow), to copy the build out put zip file, to Lab Environment Machine and extract.

1. First I am going to create a batch file and a PS script. My PS script will do all the work. Batch file (.bat) will be used to initiate the powershell script from Lab Workflow. To get this to work I need to make sure my batch file and the PS script is copied to build output folder, since it is the place that I can execute a deployment script from Lab Work Flow.

I have added my .bat file and the PS script file under a VS project that is under a solution I am building, and set the Copy to Output as always.

2. This will copy my .bat and PS script to Build Drop folder.

3. This is what I have in my .bat file. It is simply executing the PS script making sure execution policy is not blocking anything. Build location here is passed as an argument from the Lab Workflow.

4. In my Lab Build in the deployment steps I specify the .bat file to run as below. For more information and how to use xcopy to copy build out put have a look at "Creating Deployment Scripts for Build-Deploy-Test Workflows". I used a poweshell script since I am going to do lot more than just copying files, like adding dlls to GAC, etc which I will explain in a next post.

$BuildLocation is giving the build output location of the build that is queued/used by  Lab workflow.

5. In my poweshell script I am copying zip to my lab environment machine. The path C:\ ..refers to the path in the lab environment machine, since  the deployment script get executed in the context of given lab environment machine as specified in the above screenshot.

After copying this script will extract contents to the copied location and remove the zip file from the Build Drop folder. Build Location is passed to the poweshell script from the above .bat file as a parameter.

6. I queue a build and I can see my zip file copying actions get executed in the Build Log as below.

7. In my lab environment machine I see the extracted files .

This is cool .. don't you think..


Anonymous said...

Hi Chaminda im trying to execute exe on remote server using build defination how can i do that please emil me

Chaminda Chandrasekara said...

Make sure your exe is attached to a prject in your solutions and it is always copied to output (In the properties widow of VS for the .exe set "Copy to Output Directory" to "Copy always") .

Then use code similar to below code segment in a powershell script. My script here execute an exe to intiate a Dynamics CRM customization deployment and log the output of exe (console.writeline) to the build output.

"Deployment to CRM --> $CRMUrl"
#Create Command Path
$CrmCommanFile = join-path $DeployFolder "CrmCommandLineUtilities.exe"

"Importing Unmanaged Solutions $CRMSolutions"
& $CrmCommanFile /O:$CRMUrl /U:$UName /P:$Pwd /C:I /S:$CRMSolutions /F:$ImportFiles /L:$DeployFolder /T:$CRMTimeOut | Out-Host
if ($lastexitcode -ne 0)
{write-error "Execution failed for $CrmCommanFile /O:$CRMUrl /U:$UName /P:$Pwd /C:I /S:$CRMSolutions /F:$ImportFiles /L:$DeployFolder /T:$CRMTimeOut"}

"Deployment to CRM completed"

Anonymous said...

Was able to publish to remote server both exe and ps file but exe is not getting executed.

Can you zip and emai be xaml temlate to do this.