【Azure DevOps系列】使ASP.NET Core應用程式託管到Azure Web App Service
阿新 • • 發佈:2020-09-11
## 使用Azure DevOps Project設定ASP.NET專案
![file](https://blog.stackable.cn/uploads/img-55eeeba1-1ade-4a64-a992-34ced1401506.png)
我們需要先在Azure面板中建立一個Azure WebApp服務,此處步驟我將省略,然後點選部署中心如下圖所示:
![file](https://blog.stackable.cn/uploads/img-952c1e0f-fdb7-4cb5-8ee5-e43e9670fb99.png)
此處我選擇的是Azure Repos,當然大家也可以選擇`Github`、`Local Git`、`FTP`
![file](https://blog.stackable.cn/uploads/img-55a2d381-4cd2-4f8f-bd52-c0d120928888.png)
我們需要提前在Azure DevOps中提前建立好應用程式,我這邊已經提前建立好了名稱為`Blog`
.
![file](https://blog.stackable.cn/uploads/img-dc48bac6-0829-4ee3-81b4-b0e31e64e31c.png)
建立完後我們會在Azure DevOps Pipeline中看到預設為我們生成的管道資訊,他是一個構建剛才那個應用程式併發布的過程。這個過程還是挺方便的,省去了我們一些的配置直接將這些給我們配置好,當然CD其實也配置好了,最終這個應用程式會發布到我們在在這之前建立的Azure WebApp中。
![file](https://blog.stackable.cn/uploads/img-22901f03-3b37-421b-b383-6dad179b9efd.png)
![file](https://blog.stackable.cn/uploads/img-bcf3c550-ee94-47dc-89b4-6305cb87a68f.png)
通過如下該圖我們可以看到已經可以訪問通了,雖然
為錯誤頁面其實也沒關係的,是因為我資料庫一些資訊未進行配置,現在呢我們已經將應用程式部署到Azure WebApp中了。當然CI和CD規則我們也可以將其進行修改的。
![file](https://blog.stackable.cn/uploads/img-55105b71-ff74-433c-ac0e-b22357063887.png)
我們來看看管道的預設配置,預設的構建過程如下圖所示。
![file](https://blog.stackable.cn/uploads/img-ead66c7a-3133-4e45-b56c-9928d206e1bb.png)
下面程式碼片段是`azure-pipelines.yml`檔案的相關配置如下所示
```
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
- master
jobs:
- job: ASPNETCore
pool:
vmImage: 'ubuntu-latest'
steps:
# - task: UseDotNet@2
# inputs:
# packageType: 'sdk'
# version: '3.1.x'
- task: DotNetCoreCLI@2
displayName: Build
inputs:
command: build
projects: '**/*.sln'
arguments: '--configuration Release -p:Version=10.5.$(Build.BuildId)-official'
- task: DotNetCoreCLI@2
displayName: Test
inputs:
command: test
projects: '**/*Tests/*.csproj'
arguments: '--configuration Release'
- task: DotNetCoreCLI@2
inputs:
command: publish
publishWebProjects: True
arguments: '--configuration Release -p:Version=10.5.$(Build.BuildId)-official --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True
- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: 'Drop'
```
當然在上面步驟中的`DotNetCoreCLI@2`任務會發布並並打包應用程式,我們可以在下圖所示頁面中進行下載釋出後的應用程式。
![file](https://blog.stackable.cn/uploads/img-c9eee010-7b57-4dde-8758-6ba1ea2e1