Build and deploy in a Windows environment
Use Apsara DevOps Flow to automate the building and deployment of web services in a Windows environment.
Prerequisites
Prepare two servers. The servers can be Alibaba Cloud ECS instances or servers from other providers. This guide refers to them as Server A and Server B.
Server A: Hosts the Internet Information Services (IIS) web service environment.
Server B: Hosts the build environment.
Step 1: Install the Internet Information Services (IIS) web service component
On Server A, open Server Manager. In the top menu bar, select Manage and then click Add Roles and Features. Follow the steps in the configuration wizard.
In the Installation Type step, select Role-based or feature-based installation.
In the Server Selection step, select the default local machine or another machine as required.
In the Server Roles step, select Web Server (IIS) and the following three options:
IIS Management Scripts and Tools
Management Service
IIS Management Console

In the Features step, no selections are required because IIS does not require extra features.
In the Role Services step, select the required settings or keep the default settings.

On the Confirmation page, click Install. After the installation is complete, click Close.
Step 2: Install the Web Deploy plugin
On Server A, install Web Deploy. Make sure that you select the complete installation option instead of the typical one.
NoteDownload the Web Deploy 4.0 plugin.
Web Deploy is an extensible client-server tool that synchronizes content and configuration to IIS. For example, developers use it to publish compiled web applications, such as ASP.NET or PHP applications, from developer tools like Visual Studio or WebMatrix to IIS.
If you select a custom installation, you must install all the components shown in the following figure. If any components are missing, it indicates that you did not install the management components during the IIS installation. In this case, you must reinstall the web service components using the Add Roles and Features wizard.
(Optional) To verify that Web Deploy is running correctly, navigate to and check whether the following services are running:
Web Deployment Agent Service. The service name may be different in older versions.
The web management service is running.
NoteIf the Web Deployment Agent Service is not running, restart it.
If the Web Deploy Agent Service does not exist, go to and look for "Microsoft Web Deploy <version>". Select 'Change', ensure that the 'Will be installed on local hard drive' option is selected for the Web Deploy component, and complete the steps to modify the installation.
Step 3: Install the dotnet-hosting component
On Server A, install the dotnet-hosting component to ensure that website resources are accessible. If this component is not installed, an HTTP 500.19 error occurs when you access service resources.
Download and run the windows-hosting-bundle installer.
NoteThe dotnet-hosting component allows IIS to host, recognize, and manage .NET Core applications.
Step 4: Create a website and configure Web Deploy publishing
On Server A, open the Start menu, select Windows Administrative Tools, and then click Internet Information Services (IIS) Manager. Alternatively, you can run the
inetmgr.execommand.In the navigation pane on the left, right-click Sites and select Add Website.
In the Add Website window, enter a Site name and Physical path as required, and then click OK.

Right-click the website you created and select .
NoteIf the Deploy option is not displayed, Web Deploy was not installed correctly or is not running.
Review the parameters in the panel that appears. The default port for the publishing server connection URL is 8172. You can use this port or change it. You do not need to change the host name in the URL. The IP address is used instead of the host name during deployment from the build machine.

(Optional) If you are using an Alibaba Cloud ECS instance, you must configure the firewall to allow external access to the server on ports 80 and 8172.
NoteSelect the security group for the destination instance. Add inbound rules for ports 80 and 8172 as required. Set the source to a specific IP address range or allow all traffic.
Step 5: Configure the Windows build machine
On Server B, install the required environment tools. Select the versions based on your requirements.
Visual Studio 2022 webdeploy_amd64_zh-CN.msi dotnet-sdk-6.0.202-win-x64.exeAfter the installation is complete, configure the environment variables. Add the required paths to the
Pathsystem variable or user variable.C:\Windows\Microsoft.NET\Framework64\v4.0.30319 C:\Program Files\IIS\Microsoft Web Deploy V3 C:\Program Files\dotnetNoteThe environment variable for dotnet is automatically added during installation. If the `dotnet` command is not found, you must add the path manually.
After you configure the environment variables, open a Windows PowerShell window. Run the
msbuild,msdeploy, anddotnetcommands to verify that the tools are installed and the environment variables are configured correctly.
Step 6: Create a pipeline
Add a private Windows build machine in Flow. For more information, see Build clusters.
Log on to the Apsara DevOps Flow console. In the console, create a pipeline. Select a template based on your requirements and click Create.
Add a Pipeline Source task. In the right-side panel, select a code source and configure the task as required.
Configure the build task. In the next stage, add an Execute Command task and configure its parameters in the panel.
Task Name: Enter a custom name for the task.
Build Cluster: From your private build clusters, select the private Windows build host.
Specify Build Node: Select the
Windows/amd64build environment.Build Environment: Select the default VM environment.
In the task steps, configure the parameters for the command.
Step Name: Specify a custom name.
Execute Command: Enter the build command.
dotnet msbuild /p:Configuration=Debug /t:PublishSpecify Shell: Select this option and choose
powershellas the shell type.

Configure the deployment task. In the next stage, add an Execute Command task and configure its parameters in the panel, similar to the previous step.
In the task steps, configure the parameters for the command.
Step Name: A custom name for the step.
Execute Command: Enter the Web Deploy automatic deployment command.
# input your command here $PUBLISH_BASE = (Get-Item .).FullName | Out-String $PUBLISH_PATH = Join-Path -Path $PUBLISH_BASE.Trim() -ChildPath "bin\Debug\net6.0\publish" & 'C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy' -verb:sync -source:contentPath=${PUBLISH_PATH} -dest:contentPath=DemoWin,computerName=https://xx.xxx.xxx.xx:8172/MsDeploy.axd?site=DemoWin,userName=YOURUSERNAME,password=YOUPASSWORD,authtype="Basic",includeAcls="False" -allowUntrusted -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -verboseNoteCommand parameters:
-source:contentPath: Specifies the path from which to publish the content. This is the output path from the previous `dotnet msbuild` command.
-dest:contentPath: Specifies the address of the destination machine and the site name. The site name is `DemoWin`. For `computerName`, provide the public IP address of Server A, the website name, and the logon username and password of the server.
Specify Shell: Select this option and choose
powershellas the shell type.

Save and run the pipeline. After the pipeline is successfully built and run, you can access the IIS website in a browser.

Step 7: Add Packages (Optional)
You can use Apsara DevOps Packages to create a NuGet repository to manage internal and third-party artifacts. Add the following dependency library to your source code. The `msbuild` command fails because the dependency package is missing.
using Newtonsoft.Json;Go to the official website to download the Newtonsoft.Json NuGet package.
In Apsara DevOps Packages, create a NuGet repository.

Follow the instructions in the repository guide to configure your local environment. First, set the repository credentials. Then, push the Newtonsoft.Json package to the repository.

After the package is pushed, confirm that the package is displayed in Packages.
Follow the third instruction in the repository guide to test adding the package in the local build environment on Server B. Then, run the
dotnet msbuildcommand. The command should run successfully.To use the package in the pipeline, change the pipeline source to the following repository. This repository already includes the `using Newtonsoft.Json;` statement.
gi*@atomgit.com:oauth2_xxxxxx/PackagesDemo.gitYou can extend the pipeline build task.
dotnet nuget add source http://yunxiao-poc.devops.aliyuncs.com:80/packages/02e873bb-xxxx-xxxx-xxxx-ec2298c5385d/nuget/repo-hzaul/index.json -n repo-hzaul -u "xxxxxxxxxxx" -p "xxxxxxxxx" dotnet add package Newtonsoft.Json --source http://yunxiao-poc.devops.aliyuncs.com:80/packages/02e873bb-xxxx-xxxx-xxxx-ec2298c5385d/nuget/repo-hzaul/index.json dotnet msbuild /p:Configuration=Debug /t:PublishSave and run the pipeline. The pipeline can now use packages from the repository.



