You are here

How to automate deployment using msbuild through command prompt

Submitted by Asif Nowaj, Last Modified on 2019-11-22

Pre-requisite is .NET framework

Assuming that .net framework is already installed in your machine.

Now follow the below instruction whether MSbuild command is working or not.
1. Open command prompt
2. Write msbuild /? and hit Enter

If you are getting below message, then it is certain that correct path is not configured to recognize the command.
'msbuild' is not recognized as an internal or external command, operable program or batch file.

Then browse the path C:\Program Files (x86)\MSBuild\14.0\Bin
See whether msbuild.exe is there or not. If not find the msbuild.exe location and follow below steps.

If it is there then
1. Go to “My Computer”,
2. Right click on it select “Properties”
3. => “Advanced system settings”
4. => “Advanced” tab
5. => “Environment Variables”
6. => “System variables” section
7. => Select “Path”
8. => “Edit”
9. => Go to end of the Variable Value [Please do not remove any of this value]
10. Add “;C:\Program Files (x86)\MSBuild\14.0\Bin;” at the end of the value.
11. Then accept the change in all the opened window.
12. Then close the command prompt and open again.
13. Try the command “msbuild /?” again. It should show the help messages.

This means that msbuild is now recognized command.

Now, let’s move on to the preparation of the command file which will take the required parameters for deploying the published artefacts to the application server.
Copy below commands into a text file and name it like DeployApplication.cmd then you can run this command file from your command prompt.


@echo off

rem =======================================================================
:start
rem =======================================================================
cls
echo everyEthing application Deployment Script
echo.
echo Select environment:
echo 1. DEV (Development)
echo 2. SYS (System Testing)
echo 3. UAT (User Acceptance Test)
echo 4. PRE-PRD (Pre-production)
echo 5. PRD (Production)
echo 6. EXIT
echo.
set EnvChoice=
set /p EnvChoice="[1/2/3/4/5/6]: "

if not '%EnvChoice%'=='' set EnvChoice=%EnvChoice:~0,1%
if '%EnvChoice%'=='1' goto DEV
if '%EnvChoice%'=='2' goto SYS
if '%EnvChoice%'=='3' goto UAT
if '%EnvChoice%'=='4' goto PRE-PRD
if '%EnvChoice%'=='5' goto PRD
if '%EnvChoice%'=='6' goto end
goto start

rem =======================================================================
:DEV
rem =======================================================================
set DEPLOYMENT_ENVIRONMENT=DEV
set RFC_ID=
goto GetRFCNumber

rem =======================================================================
:SYS
rem =======================================================================
set DEPLOYMENT_ENVIRONMENT=SYS
set RFC_ID=
goto GetRFCNumber

rem =======================================================================
:UAT
rem =======================================================================
set DEPLOYMENT_ENVIRONMENT=UAT
set RFC_ID=
goto GetRFCNumber

rem =======================================================================
:PRE-PRD
rem =======================================================================
set DEPLOYMENT_ENVIRONMENT=PRE-PRD
set RFC_ID=
goto GetRFCNumber

rem =======================================================================
:PRD
rem =======================================================================
set DEPLOYMENT_ENVIRONMENT=PRD
set RFC_ID=
goto GetRFCNumber

rem =======================================================================
:GetRFCNumber
rem =======================================================================
rem Identifier for the deployment
cls
echo everyEthing application Website Script
echo.
echo Environment: %DEPLOYMENT_ENVIRONMENT%
set /p RFC_ID="Enter the RFC (Request for Change) number for this deployment: "
if '%RFC_ID%'=='' goto GetRFCNumber
goto Confirmation

rem =======================================================================
:Confirmation
rem =======================================================================
cls
echo everyEthing application Deployment Script
echo.

echo Environment: %DEPLOYMENT_ENVIRONMENT%
echo RFC Number: %RFC_ID%

echo.
set choice=
set /p choice="Confirm [y/n]: "
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='y' goto StartDeploy
if '%choice%'=='n' goto start
goto Confirmation

rem =======================================================================
:StartDeploy
rem =======================================================================

rem Delegate the actual deployment to MSBuild.
msbuild everyEthing.build /target:Deploy /property:TargetEnvironment="%DEPLOYMENT_ENVIRONMENT%" /property:DeploymentId="%RFC_ID%" /nologo /logger:FileLogger,Microsoft.Build.Engine;logfile=everyEthingDeployment.log
:End
pause

everyEthing.build is the project file for MSbuild and starting target to execute is Deploy. Here TargetEnvironment and DeploymentId is sent as parameter to the command.

In a summary, following tasks should be taken care by this command.
Unzip application artefact
Prepare backup location
Take backup of existing files
Copy new files to the deployment location

Please stay tuned for the next article, I’ll explain how to do all of the above tasks using MSbuild.

Discussion or Comment

If you have anything in mind to share, please bring it in the discussion forum here.

https://forum.everyething.com/others-f41/