我在之前的文章里介紹了如何一步步配置CI/CD來部署Teams App(?之前的文章?),隨著Azure DevOps的發展,微軟推出了Azure Pipelines。在這篇文章中,主要介紹什么是Azure Pipelines,以及如何使用Azure Pipelines來進行Teams App的構建、測試和部署工作負載
什么是Azure Pipelines
微軟發布了Azure Pipelines,他們新的 CI/CD 服務,是 Azure DevOps 產品的一部分。Azure Pipelines 可用于構建、測試和部署工作負載,并可以讓各種語言、項目類型和平臺協同工作。
作為 Visual Studio Team Services(VSTS)的后續產品,Azure DevOps 由幾個組件組成,Azure Boards、Azure Repos、Azure Test Plans、Azure Artifacts和 Azure Pipelines。Azure DevOps 提供了端到端服務,用于共享代碼、跟蹤工作并提供類似于其他服務(如Atlassian Stack)的解決方案。 這些組件都是 Azure DevOps 鏈中的一個鏈接,Azure Pipelines 實現了 CI/CD 管道的角色。 此外,Azure Pipelines 具有以前在 VSTS 中可用的所有功能,并補充了一些新功能:
- Azure Pipelines 是一項獨立服務,可以獨立于其他 Azure DevOps 組件使用。
- 可以直接通過 GitHub Marketplace 獲取和配置新的管道。
- 更好地與 GitHub 集成,包括拉取請求的構建和跟蹤代碼提交及其相關問題。
- 通過容器作業來支持原生容器。
- 開源項目可以免費使用 Azure Pipelines。
- 相比 VSTS,Azure Pipelines 提供了更加靈活的免費使用限制。
如何利用Azure Pipelines來構建Build
之前的文章中,提到如何利用Azure DevOps來Build、Deploy應用到Azure上,實現CI/CD的整個部署過程,簡單回顧下配置的步驟:
1、在Azure DevOps中新建Repo, 本地Clone,Push代碼
2、在Azure DevOps中并編譯生成一個 .NET Core應用, 配置持續集成環境
3、將Azure添加到Azure DevOps的Service Endpoint(僅限中國區的Azure)
4、在Azure中創建App Service
5、配置Azure DevOps中構建的應用Release到Azure App Service
下面,一步步的演示,如何使用Azure Pipelines構建一個新的Build,其中一個文件azure-pipelines.yml
,是整個Azure Pipelines服務的關鍵, 我們將逐個分析整個yml配置的含義
一、配置build pipelines
1、進入到Azure Devops, 看到整個的功能模塊導航,和之前已經有很大的變化,這里我們略過新建Repos,Check in代碼的過程, 找到 “Pipelines”,點擊 “Build”,新建New build pipelines
2、這里“Location” 選擇 “Azure Repos”?
3、選擇項目代碼的Repos?
4、Build的配置模板,選擇“ASP.NET Core”,?
5、配置完成,開始整個Build的過程?
到此整個自動化Build的過程就已經配置好了,和之前最大的一個不同是,回到Code Repos, 看到在我們的項目代碼中,自動生成了一個名為azure-pipelines.yml
文件
打開這個yml文件,看下里面的配置參數
pool:vmImage: 'vs2017-win2016'variables:buildConfiguration: 'Release'steps:
- script: dotnet build --configuration $(buildConfiguration)displayName: 'dotnet build $(buildConfiguration)'- task: DotNetCoreCLI@2inputs:command: publishpublishWebProjects: Falseprojects: 'MicrosoftTeams.OutgoingWebhook.csproj'arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'zipAfterPublish: True
二、Azure Pipelines中yml文件釋義
1、首先,pool vmImage
參數
pool:vmImage: 'vs2017-win2016'
這里的pool vmImage
參數表示:Build Environment, 其中的可選項有 ‘ubuntu-16.04’、’macOS-10.13’、’vs2017-win2016’,在Microsoft-hosted agents中有不同 的系統環境及其版本供選擇
2、variables
variables:buildConfiguration: 'Release'
這里的buildConfiguration
參數表示: Build的版本是Release版本
3、steps
steps:
- script: dotnet build --configuration $(buildConfiguration)displayName: 'dotnet build $(buildConfiguration)'
這里的script: dotnet build
表示:執行build的工作腳本,命令,并且進行build的輸出配置,或者使用.NET Core task
的模式進行配置,如下:
steps:
- task: DotNetCoreCLI@2inputs:command: restoreprojects: '**/*.csproj'feedsToUse: confignugetConfigPath: NuGet.config # Relative to root of the repositoryexternalFeedCredentials: <Name of the NuGet service connection>
在restore之前,如果我們想要在Microsoft-hosted agent指定 .Net Core SDK的版本進行Build,可以在yml配置中加入如下配置
```yml
- task: DotNetCoreInstaller@0inputs:version: '2.1.300' # replace this value with the version that you need for your project
4、task
- task: DotNetCoreCLI@2inputs:command: publishpublishWebProjects: Falseprojects: 'MicrosoftTeams.OutgoingWebhook.csproj'arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'zipAfterPublish: True
其中task
表示:使用.NET Core task
的進行模式執行任務構建,?command
則別是構建的執行的命令,比如常用的build、test、publish等
三、總結
從上面的yaml的腳本配置,結合微軟的官方文檔, 如下截圖:
我們可以這樣去理解,相當于以前從可視化的界面進行CI的配置(截圖中的Designer模式),現在在Azure Pipelines中使用yaml文件進行配置(截圖中的YAML模式),這進一步方便了對CI過程的配置,當我們需要改變 我們的CI行為,比如增減組建,步驟等,直接修改配置文件,當push代碼,merge到分支后,則整個構建流程會按照新的配置文件進行構建,這無疑提供了更大的靈活性。
參考文檔
- 1、Build, test, and deploy .NET Core apps in Azure Pipelines
- 2、Build .NET Core projects with Azure Pipelines or Team Foundation Server
- 3、Free Azure Build Pipelines for Open Source Projects
- 4、微軟發布 Azure Pipelines,開源項目可無限制使用 CI/CD