Use Powershell to automatically deploy the asp.net mvc website project (1)

Source: Internet
Author: User

In this article, we will write some code about automated deployment. We will usePowershellWrite this type of code.

You will find that the content in this article is very specific, some of which are demanding and may not be universal. This is because the deployment has always been dealing with the environment, and there are too many cooperative components in the deployment process, so the intersection between them cannot be too large. The only thing that can be universally used is the basic principles of automated deployment (only the basic principles of this article ):

  • After each automated deployment, the application will have the same initial state.
  • The automatically deployed machines are very clean and only the correspondingWindows ServerSystem and. NET Framework. In particular, there is no Visual Studio.

We need to disclose some basic environment information:

  • 64-bit Windows Server 2008/2012/2012 R2 or 64-bit Windows 7/8/8.1
  • Our project is to useMicrosoft Visual maxcompute 2013Developed;
  • . NET FrameworkVersion: 4.5
  • ASP. NET MVCVersion 5.0.0
  • Microsoft Build ToolVersion 12.0

AboutMSBuildI would like to say a few more here. InVisual Studio2013 before release,MSBuildYes. NET FrameworkReleased together. This means we do not need to installVisual StudioYou can build a. NET application.

What a wonderful world! Therefore, the last sentence is false. Especially when you buildASP. NETWhen you use a Web application, you will immediately encounter "Cannot found... WebApplication.tar get "error. If you have StackOverflow, you can see that what we need to do is installVisual StudioOr you have installedVisual StudioTo copy the relevant files. This is really a shameful design! Linux fans have another space for venting.

So Microsoft decided to face up to this problem. SlaveVisual Studio2013,MSBuildAndVisual StudioInstead. NET FrameworkPublish together. This caused a buzz. I have to say that everyone should be calm and never get any benefit if they lose their temper. The fact is that because. NET FrameworkThe release cycle is relatively long, so Microsoft is more and more inclined to use NuGet to release class libraries, which will help reduce. NET FrameworkThe size of the base class library and shorten the release cycle of the base class library. CorrespondingMSBuildAndVisual StudioThe release cycle also requires independent changes.MSBuildMerge andVisual StudioThis does not mean that the release cycle is consistent.MSBuildDependent onVisual Studio. So of course we can downloadMSBuildThe independent installation package Microsoft Build ToolsVisual StudioServer.

Again, if you are lucky, you can directly run the example in this article. But you don't want to copy the script of this article to your project to work properly. Deployment is a task tailored to local conditions and requires specific analysis. You may encounter various environmental problems, but I think the most important thing is the idea.

What is deployment?

Simply put, deployment is "Build"-> "copy"-> "configuration ". Then we start to solve it one by one. This article will focus on building.

Compiling and building applications-ideas

How to build an application? The answer is to copy all the required items and then build them using tools. Great. What do we need to copy? Of course, the source code is copied first.

┌────────────────┐│ Src of MyApp   │└────────────────┘

The source code does not work, because our program depends on a third-party library (arrows represent dependencies ).

┌────────────────┐│ Dependent libs │└────────────────┘        ↑┌────────────────┐ │ Src of MyApp   │└────────────────┘

For example, for the simple ASP. net mvc 5 application we built, we need to download the dependent class library before building:

  • Microsoft. AspNet. Mvc 5.0.0
  • Microsoft. AspNet. Razor 3.0.0
  • Microsoft. AspNet. WebPages 3.0.0
  • Microsoft. Web. Infrastructure 1.0.0.0

The source code and dependent libraries are all copied, so we need to copy the build tool as well:

┌────────────────┐│ Dependent libs │└────────────────┘        ↑┌────────────────┐     ┌───────────────────┐│ Src of MyApp   │────→│Tools for compiling│└────────────────┘     └───────────────────┘

For us, there is only one build tool:

  • MSBuild

Today, tools and source code are readily available, so we can start compiling.

Install MSBuild

If you have installedVisual Studio2013 -- the Express version is also available -- you can skip this step; otherwise, downloadMicrosoft Build ToolsAnd install.

Download application code

Our code is ready for use. You only need to clone it from git repository. You can also clone the sample code from this address and convert it to the corresponding commit.

git clone https://github.com/lxconan/MvcFromZero.gitgit reset --hard 340abb32433c99975bd6485f79db6ca077119477

Well, it's done. So far everything has been so easy and pleasant.

Download application dependent Libraries

We will use nuget to manage the dependencies of packages. Therefore, we need to downloadNuGetCommand Line client, which can be downloaded from here.

The following actions require a clear directory structure. For ease of description, we will create the following directory structure.

MvcFromZero |- src |   |- FromZero.App |   |- packages | |- build     |- tools

The src directory stores the source code of Solution, and the build directory stores various tools and scripts required for automated building. The tools required for automated building will be placed under the build/tools directory. Therefore, we will also put nuget.exe in this directory.

Next we will create the deploy. ps1 script in the build directory. We will complete the following tasks in this script. We will first download the dependent package. NuGet packages are managed throughPackage. configFile. It should be noted thatPackage. configIt also has a certain level of relationship. First, the Solution-level package information is stored in two places:

  • The first is. Nuget/package. configFile, which is stored in a non-specific project package (if this type of package does not exist, the directory does not exist, or the directory does not contain any files );
  • The second is under the Solution directory.Packages/repositories. configFile, which storesPackages. configFile Path.

The package definition of each Project under Solution is stored in the Project directoryPackage. config.

To download the dependent packages, do you need to traverse them manually?ConfigWhat about files? Originally, nuget 2.7 can directly support downloading packages within the Solution range, so we can use the following simple functions to download the package.

Function Install-SolutionPackages() {    iex "$global_nugetPath restore $global_solutionFilePath"}

Where,$global_nugetPathIs the path of nuget.exe, and$global_solutionFilePathIs the path of the project file (FromZero. App. csproj.

Next we need to determine the location of the build tool. We can use the registry to findMSBuildBecause we use the 2013MSBuild(Toolset version 12.0), so our search script is:

Function Get-MsBuildPath() {    $msBuildRegPath = "HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\12.0"    $msBuildPathRegItem = Get-ItemProperty $msBuildRegPath -Name "MSBuildToolsPath"    $msBuildPath = $msBuildPathRegItem.MsBuildToolsPath + "msbuild.exe"    return $msBuildPath}

After completing the above two steps, you can compile our project:

Function Compile-Project() {    iex -Command "& '$global_msBuildPath' $project_path"}

You can't wait to experiment with your code, but you find something wrong,MSBuildReport not foundMicrosoft.WebApplication.tar getsFile. What's going on here?MSBuildInstalled inMicrosoft.WebApplication.tar getsNow? This is becauseMicrosoft Visual Studio2010 Engineering Compatibility of SP1,Visual Studio2012/2013 of projects will by default$(VisualStudioVersion)Set the environment variable10.0. In this way, the query path of the target file becomes:

$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0

Instead:

$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v12.0

Therefore, we need to modify this environment variable. Open in a text editorFromZero. App. csprojFile, find the following definition:

<PropertyGroup>  <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>  <VSToolsPath    Condition="'$(VSToolsPath)' == ''">    $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)  </VSToolsPath></PropertyGroup>

ReplaceVisualStudioVersionSection replaced:

<VisualStudioVersion>12.0</VisualStudioVersion>

Run again: Congratulations! You have successfully downloaded all dependencies and compiled the project! At the end of this article, add all the code for deploy. ps1 so far:

$ErrorActionPreference = 'Stop'# Environment helpers ------------------------------------Function Get-MsBuildPath() {    $msBuildRegPath = "HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\12.0"    $msBuildPathRegItem = Get-ItemProperty $msBuildRegPath -Name "MSBuildToolsPath"    $msBuildPath = $msBuildPathRegItem.MsBuildToolsPath + "msbuild.exe"    return $msBuildPath}# Environment variables ----------------------------------$global_buildDirPath = Get-Location$global_msBuildPath = Get-MsBuildPath$global_solutionPath = "$global_buildDirPath\..\src"$global_solutionFilePath = "$global_solutionPath\src.sln"$global_nugetPath = "$global_buildDirPath\tools\nuget.exe"# Install nuget packages ---------------------------------Function Install-SolutionPackages() {    iex "$global_nugetPath restore $global_solutionFilePath"}$project_path = $global_solutionPath + '\FromZero.App\FromZero.App.csproj'Function Compile-Project() {    iex -Command "& '$global_msBuildPath' '$project_path'"}Install-SolutionPackagesCompile-Project

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.