Automatic Deployment
Ivan Antsipau
Minsk .NET Meetup #2
Process
•
•
•
•
•
•
•

Build correct version of source code
Change configuration
Stop the application(s)
Deploy (copy file...
Automatic Deployment
• “Web Deployment: If You're Using XCopy, You're Doing
It Wrong”
– SCOTT HANSELMAN (HTTP://GOO.GL/MHW...
Manual Deployment Drawbacks
• A lot of time:
– Can’t run integration tests
– Cant’ execute often (no visibility for manage...
Components
• MSBuild (The Microsoft Build Engine)
– Build and package

• Web Deploy:
– Move to target server

• CI Server:...
Manage Configuration
• Differ by Environments (server name, etc)
• Differ by Build configuration (Debug vs Release)
• We w...
Manage Configuration: transformations

Automatic Deployment on Microsoft .Net stack

7
Preview Transformation

Automatic Deployment on Microsoft .Net stack

8
Transformations (easy xslt)

• Locator (can be omitted):
– how to find

• Transform:
– what to do

“The Transform attribut...
Locators
•

Match
–
–

•

Condition
–
–

•

Locator="Match(comma-delimited list of one or more attribute names)“
<add name...
Transforms
• Replace
<connectionStrings xdt:Transform="Replace">
<add name="SqlServer" connectionString=“str1;" />
<add na...
Publish Profile
• Corresponds to target Environment (server)
– Where to publish
– How to publish
– How to transform

Autom...
Publish Profile Settings

Automatic Deployment on Microsoft .Net stack

13
Profile is a Versioned XML File

Automatic Deployment on Microsoft .Net stack

14
Profile-Specific Config Transformations
• Connections strings
• External services addresses
• Other environment-specific s...
Profile-Specific Config Transformations

Automatic Deployment on Microsoft .Net stack

16
Chained Transformations
Original web.config
file

Configurationspecific

Publish profile –
specific

(Web.release.config)
...
Web Deploy (msdeploy)
• The right way to deploy to IIS.
• IIS extension:
– Install via MS Web Platform Installer

• Comman...
Features
• Non-admin deployments (delegation)
• Deploy over https (secure)
• Powerful:
–
–
–
–
–
–
–
–

Copy files
Remove ...
Command-line vs MSBuild wrappers

Msbuild.exe Web.csproj /p:DeployOnBuild=true /p:Configuration=Release
/p:PublishProfile=...
Web Deploy Settings in Publish Profile

Automatic Deployment on Microsoft .Net stack

21
Command-line run
Msbuild.exe Web.csproj /p:DeployOnBuild=true /p:Configuration=Release
/p:PublishProfile=staging /p:Visual...
Run From CI

Msbuild.exe Web.csproj /p:DeployOnBuild=true /p:Configuration=Release
/p:PublishProfile=staging /p:VisualStud...
Update Database
• Migrations:
– Run on AppStart
– Run before/after deployment

• Database project:
– Run before/after depl...
Database Project

Automatic Deployment on Microsoft .Net stack

25
Command-line publishing

Msbuild.exe /p:VisualStudioVersion=11.0 /t:Rebuild;Publish
/p:SqlPublishProfilePath=Database.stag...
Process
Build correct version of source code
Change configuration
• Stop the application(s)
Deploy (copy files), skip u...
Tips and Tricks: version
•
•

MSBuild Community Tasks (https://github.com/loresoft/msbuildtasks)
NuGet package available
T...
Usage

Automatic Deployment on Microsoft .Net stack

29
Output Version

Automatic Deployment on Microsoft .Net stack

30
Customize Web Deploy
• ProjectName.Wpp.targets – imported for any web
project before build
– Web.csproj -> Web.Wpp.targets...
Ignore user-generated files

Automatic Deployment on Microsoft .Net stack

32
Set ACL Permissions
• Use .wpp.targets (see
http://sedodream.com/2011/11/08/settingfolderpermis
sionsonwebpublish.aspx ).
...
App_offline.htm
• Takes down the application
• Serve static app_offline.htm to notify users

Automatic Deployment on Micro...
App_offline.htm
•
•
•
•
•
•
•

Store as app_offline.template.htm
Rename to app_offline.htm before deployment
Update db, do...
Complex scenarios
• Using MSBuild to:
– Prepare
– Build
– Update Configuration
– Execute Deployment
– Etc

• Regex-based t...
Thanks for Your Attention
ivan.antsipau@gmail.com
https://www.facebook.com/Ivan.Antsipau
Links
• App_offline
http://sedodream.com/2012/01/08/HowToTakeYourWebAppOffli
neDuringPublishing.aspx
• VS2010 Guide http:/...
Links
• http://msbuildbook.com/ (examples on github)
• MSDN http://msdn.microsoft.com/enus/library/0k6kkbsd.aspx
• Sayed I...
Upcoming SlideShare
Loading in …5
×

Automatic deployment on .NET web stack (Minsk .NET meetup 12.02.14)

1,993 views

Published on

Automatic deployment on .NET web stack (presentation for Minsk .NET meetup 12.02.14)

Published in: Self Improvement
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,993
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Automatic deployment on .NET web stack (Minsk .NET meetup 12.02.14)

  1. 1. Automatic Deployment Ivan Antsipau Minsk .NET Meetup #2
  2. 2. Process • • • • • • • Build correct version of source code Change configuration Stop the application(s) Deploy (copy files) Backup DB(s) Update DB(s) Start the application(s) Automatic Deployment on Microsoft .Net stack 2
  3. 3. Automatic Deployment • “Web Deployment: If You're Using XCopy, You're Doing It Wrong” – SCOTT HANSELMAN (HTTP://GOO.GL/MHWJ77) • Mooney’s Law Of Guaranteed Failure: – In the software business, every manual process will suffer at least a 10% failure rate, no matter how smart the person executing the process. No amount of documentation or formalization will truly fix this, the only resolution is automation. – (http://goo.gl/nuoVzM) Automatic Deployment on Microsoft .Net stack 3
  4. 4. Manual Deployment Drawbacks • A lot of time: – Can’t run integration tests – Cant’ execute often (no visibility for managers and stake-holders) – Can’t execute when business decides to • Error-prone: 0.25h CI executes Daily Build Automatic Deployment on Microsoft .Net stack Cost to fix the issue – Didn’t you deploy the Service? – Which service?! – Manual updates of the configuration. 4h Issue Detection time Manual QA 4
  5. 5. Components • MSBuild (The Microsoft Build Engine) – Build and package • Web Deploy: – Move to target server • CI Server: Atlassian Bamboo, TFS, TeamCity, etc – Trigger the process, report the results Automatic Deployment on Microsoft .Net stack 5
  6. 6. Manage Configuration • Differ by Environments (server name, etc) • Differ by Build configuration (Debug vs Release) • We want: – Changes to be automated – Differences to be versioned Automatic Deployment on Microsoft .Net stack 6
  7. 7. Manage Configuration: transformations Automatic Deployment on Microsoft .Net stack 7
  8. 8. Preview Transformation Automatic Deployment on Microsoft .Net stack 8
  9. 9. Transformations (easy xslt) • Locator (can be omitted): – how to find • Transform: – what to do “The Transform attribute specifies what you want to do to the elements that the Locator attribute finds.” MSDN (http://goo.gl/7J4Mz9) Automatic Deployment on Microsoft .Net stack 9
  10. 10. Locators • Match – – • Condition – – • Locator="Match(comma-delimited list of one or more attribute names)“ <add name="AWLT" connectionString="newstring" providerName="newprovider" xdt:Transform="Replace" xdt:Locator="Match(name)" /> Locator="Condition(XPath expression)” (XPath appended to the current element's XPath expression) <add name="AWLT" connectionString="newstring" providerName="newprovider" xdt:Transform="Replace" xdt:Locator="Condition(@name='oldname'or @providerName='oldprovider')" /> Xpath – – Locator="XPath(XPath expression)“(XPath IS NOT appended to the current element's XPath expression) <add name="AWLT" connectionString="newstring" providerName="newprovider" xdt:Transform="Replace" xdt:Locator="XPath(configuration/connectionStrings[@name='AWLT' or @providerName='System.Data.SqlClient'])" /> Automatic Deployment on Microsoft .Net stack 10
  11. 11. Transforms • Replace <connectionStrings xdt:Transform="Replace"> <add name="SqlServer" connectionString=“str1;" /> <add name="Entities" connectionString=“str2;" /> </connectionStrings> • • • • • • Insert InsertBefore InsertAfter Remove RemoveAll RemoveAttriburtes – <compilation xdt:Transform="RemoveAttributes(debug)" /> • SetAttributes More at MSDN (http://goo.gl/7J4Mz9) Automatic Deployment on Microsoft .Net stack 11
  12. 12. Publish Profile • Corresponds to target Environment (server) – Where to publish – How to publish – How to transform Automatic Deployment on Microsoft .Net stack 12
  13. 13. Publish Profile Settings Automatic Deployment on Microsoft .Net stack 13
  14. 14. Profile is a Versioned XML File Automatic Deployment on Microsoft .Net stack 14
  15. 15. Profile-Specific Config Transformations • Connections strings • External services addresses • Other environment-specific settings Automatic Deployment on Microsoft .Net stack 15
  16. 16. Profile-Specific Config Transformations Automatic Deployment on Microsoft .Net stack 16
  17. 17. Chained Transformations Original web.config file Configurationspecific Publish profile – specific (Web.release.config) (Web.staging.config) Automatic Deployment on Microsoft .Net stack 17
  18. 18. Web Deploy (msdeploy) • The right way to deploy to IIS. • IIS extension: – Install via MS Web Platform Installer • Command-line tool: – Comes with VS or installed separately • Bad news: scary syntax • Good news: VS team has prepared msbuild wrappers Automatic Deployment on Microsoft .Net stack 18
  19. 19. Features • Non-admin deployments (delegation) • Deploy over https (secure) • Powerful: – – – – – – – – Copy files Remove extra files Skip rules Take site offline/online Run executable remotely Install libs into GAC Auto back up Etc In House Azure Web Platform Installer (3-clicks) Pre-installed: (download publish profile) • More at TechNet (http://goo.gl/1aM0uu) and IIS.net (http://goo.gl/hnoMVR) Automatic Deployment on Microsoft .Net stack 19
  20. 20. Command-line vs MSBuild wrappers Msbuild.exe Web.csproj /p:DeployOnBuild=true /p:Configuration=Release /p:PublishProfile=staging /p:VisualStudioVersion=11.0 /p:Password=p0ssw0rd Automatic Deployment on Microsoft .Net stack 20
  21. 21. Web Deploy Settings in Publish Profile Automatic Deployment on Microsoft .Net stack 21
  22. 22. Command-line run Msbuild.exe Web.csproj /p:DeployOnBuild=true /p:Configuration=Release /p:PublishProfile=staging /p:VisualStudioVersion=11.0 /p:Password=p0ssw0rd Automatic Deployment on Microsoft .Net stack 22
  23. 23. Run From CI Msbuild.exe Web.csproj /p:DeployOnBuild=true /p:Configuration=Release /p:PublishProfile=staging /p:VisualStudioVersion=11.0 stack Automatic Deployment on Microsoft .Net /p:Password=p0ssw0rd 23
  24. 24. Update Database • Migrations: – Run on AppStart – Run before/after deployment • Database project: – Run before/after deployment – Has publish profiles Automatic Deployment on Microsoft .Net stack 24
  25. 25. Database Project Automatic Deployment on Microsoft .Net stack 25
  26. 26. Command-line publishing Msbuild.exe /p:VisualStudioVersion=11.0 /t:Rebuild;Publish /p:SqlPublishProfilePath=Database.staging.publish.xml Automatic Deployment on Microsoft .Net stack 26
  27. 27. Process Build correct version of source code Change configuration • Stop the application(s) Deploy (copy files), skip uploaded files • Backup DB(s) Update DB(s) • Start the application(s) Automatic Deployment on Microsoft .Net stack 27
  28. 28. Tips and Tricks: version • • MSBuild Community Tasks (https://github.com/loresoft/msbuildtasks) NuGet package available Task Description Add Add numbers AddTnsName Defines a database host within the Oracle TNSNAMES.ORA file. AppPoolController Allows control for an application pool on a local or remote machine with IIS installed. The default is to control the application pool on the local machine. If connecting to a remote machine, you can specify the and for the task to run under. Xslt A task to merge and transform a set of xml files. XslTransform XslTransform task for Sandcastle. AssemblyInfo Generates an AssemblyInfo files Attrib Changes the attributes of files and/or directories on Microsoft .Net stack Automatic Deployment 28
  29. 29. Usage Automatic Deployment on Microsoft .Net stack 29
  30. 30. Output Version Automatic Deployment on Microsoft .Net stack 30
  31. 31. Customize Web Deploy • ProjectName.Wpp.targets – imported for any web project before build – Web.csproj -> Web.Wpp.targets Automatic Deployment on Microsoft .Net stack 31
  32. 32. Ignore user-generated files Automatic Deployment on Microsoft .Net stack 32
  33. 33. Set ACL Permissions • Use .wpp.targets (see http://sedodream.com/2011/11/08/settingfolderpermis sionsonwebpublish.aspx ). Automatic Deployment on Microsoft .Net stack 33
  34. 34. App_offline.htm • Takes down the application • Serve static app_offline.htm to notify users Automatic Deployment on Microsoft .Net stack 34
  35. 35. App_offline.htm • • • • • • • Store as app_offline.template.htm Rename to app_offline.htm before deployment Update db, do other stuff Skip on deployment (do not delete) Rename to app_offline.template.htm when done See details at http://goo.gl/qbGrKA Alternatively: use AppOffline rule (http://goo.gl/tC0qxz) Automatic Deployment on Microsoft .Net stack 35
  36. 36. Complex scenarios • Using MSBuild to: – Prepare – Build – Update Configuration – Execute Deployment – Etc • Regex-based transformations for multiple similar environments (QA1-QA10, Test1-Test10). Automatic Deployment on Microsoft .Net stack 36
  37. 37. Thanks for Your Attention ivan.antsipau@gmail.com https://www.facebook.com/Ivan.Antsipau
  38. 38. Links • App_offline http://sedodream.com/2012/01/08/HowToTakeYourWebAppOffli neDuringPublishing.aspx • VS2010 Guide http://www.troyhunt.com/2010/11/youdeploying-it-wrong-teamcity.html • VS2012 Deployment Guide http://www.asp.net/mvc/tutorials/deployment/visual-studioweb-deployment/introduction • http://stackoverflow.com/questions/tagged/webdeploy • http://stackoverflow.com/questions/tagged/msdeploy • http://blog.richardszalay.com/tag/msdeploy/ Automatic Deployment on Microsoft .Net stack 38
  39. 39. Links • http://msbuildbook.com/ (examples on github) • MSDN http://msdn.microsoft.com/enus/library/0k6kkbsd.aspx • Sayed Ibrahim Hashimi http://sedodream.com/ , @SayedIHashimi • http://www.msbuildexplorer.com/ • http://stackoverflow.com/questions/tagged/ms build • http://msdn.microsoft.com/enus/library/ms171483.aspx incremental build Automatic Deployment on Microsoft .Net stack 39

×