batch file to deploy files

Posted by Martin Michalak on Server Fault See other posts from Server Fault or by Martin Michalak
Published on 2013-10-25T09:13:19Z Indexed on 2013/10/25 9:57 UTC
Read the original article Hit count: 272

Filed under:
|

hi I have created batch file which pulls info from *.txt file and deploy code from the source to destination:

   SET Source=%1
   if exist %Source% (
   ECHO Source for WEB exists
   ) else (
       ECHO Wrong build%Source% doesn't exist
   GOTO Menu
   )

   SET Server=%2
   SET AppPool=%3
   SET Destination=%4
   SET Folder=%5
   SET ENV=%6
   SET AppName=%7
   SET Envlog=%8
   ECHO Deployment of WEB > %Envlog% %Date% %Time%

   echo.
   @ECHO Stopping App Pools
   @ECHO Stopping App Pools >> %Envlog% %Date% %Time%
   D:\ICTTools\PSEXEC.EXE -d \\%Server% cmd.exe /c c:\windows\system32\inetsrv\appcmd STOP apppool /apppool.name:%AppPool%

  echo.
  @ECHO App Pools will be stopped in the background
  @ECHO App Pools will be stopped in the background >> %Envlog% %Date% %Time%
  Pause 
  echo.
  IF EXIST "%Destination%" (
  ECHO Deleting %AppName% %Folder% 
  RMDIR %Destination% /s /q 
  ECHO Destination Folder %Folder% Deleted
  ECHO Destination Folder %Folder% Deleted >> %Envlog% %Date% %Time%
  ) else (
       ECHO Destination Folder %Destination% does not exist, please check
       ECHO Destination Folder %Destination% does not exist, please check  >> %Envlog% %Date% %Time%
       Pause
  )

  echo.
  @ECHO Starting Robocopy for %AppName%
  @ECHO Starting Robocopy for %AppName% >> %Envlog% %Date% %Time%
  echo.
  START /WAIT /MIN ROBOCOPY.EXE %Source% %Destination% *.* /S /NP /R:3 /W:5 /LOG:"Logs\Robo%AppName%%ENV%.log"
  D:\Tools\Windiff\windiff.exe %Source% %Destination%
  echo.

  @ECHO Finished with Robocopy 
  @ECHO Finished with Robocopy >> %Envlog% %Date% %Time%
  echo.
  @ECHO Checking if App pools stopped:
  @ECHO Checking if App pools stopped: >> %Envlog% %Date% %Time%

  D:\ICTTools\PSEXEC.EXE \\%Server% c:\windows\system32\inetsrv\appcmd LIST apppool /apppool.name:%AppPool%
  @echo off
  set /p ask=All app pools stopped? (y/n)
  if %ask%==y (echo Great, please continue with deployemnt) else echo Before continuing please check why app pools did not stop
  @echo App pools stopped?: %ask% >> %Envlog% %Date% %Time%

  DEL %Source%\web.config

  echo.
  @ECHO Production Config check
  if exist "%Destination%\%ENV%-Web.config" (
        echo.
                  ECHO The Application production configuration file does exist.
        ECHO The Application production configuration file does exist. >> %Envlog% %Date% %Time%
                  COPY %Destination%\%ENV%-Web.config web.config
                  echo.
        ECHO Production %ENV%-Web.config has been renamed to web.config 
        ECHO Production %ENV%-Web.config has been renamed to web.config >> %Envlog% %Date% %Time%
                                  ) else (
                  ECHO The Application production configuration file is missing in Production %AppName%
        ECHO The Application production configuration file is missing in Production %AppName% >> %Envlog% %Date% %Time%
                  explorer %Destination%
                  Pause
                  )
  echo.
  @ECHO Confirm that configs were renamed correclty, if yes please hit any key to START APP Pools
  @ECHO Confirm that configs were renamed correclty, if yes please hit any key to START APP Pools >> %Envlog% %Date% %Time%
  Pause
  echo.
  @ECHO Start %AppName% Application Pool >> %Envlog% %Date% %Time%

  D:\ICTTools\PSEXEC.EXE \\%Server% c:\windows\system32\inetsrv\appcmd START apppool /apppool.name:%AppPool%

  @echo off
  set /p ask=All app pools started? (y/n)
  if %ask%==y (echo Great, please continue with deployemnt) else echo Before continuing       please check why app pools did not start
  @echo App pools started?: %ask% >> %Envlog% %Date% %Time%
  Pause 
  echo.
  @ECHO Build Version for %AppName%
  @ECHO Build Version for %AppName% >> %Envlog% %Date% %Time%
  type %Destination%\buildinfo.xml
  echo.
  ECHO ...............................................
  @ECHO ...........Deployment Compelted................
  @ECHO ...........Deployment Compelted................>> %Envlog% %Date% %Time%
  ECHO ...............................................

here are my issues:

Lets say I am running code for 3 servers, then for each instance:

  1. For all three servers I am performing destination folder delete even so destination folder is always the same, the code should only delete it in the 1st instance (when code is deployed to first server) then I would prefer if script would check if the code from the source and destination is the same and if it is it should delete the folder or not. Then based on 1:

a) deleting web.config and renaming should only happen if code in destination is new

b) Robocopy should not override files if they are the same I think there is /Xo option to do that

any idea how to achieve that? :)

© Server Fault or respective owner

Related posts about batch

Related posts about robocopy