iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 19
0
Microsoft Azure

Azure 的自我修煉系列 第 19

Day19 Azure Pipelines服務 YAML 說明與設定

Azure Pipelines服務

上一篇介紹了怎麼創建 Azure Pipelines,
再來延續上一篇怎麼使用YAML 來設定 Azure Pipelines 步驟
Yaml 的語法官方說明: Azure Pipelines YAML schema reference

管線 PipeLine 的管理方式,就是如下圖,
編輯程式碼->修改YAML檔->上傳到雲端倉庫->透過Azure Pipeline -> 部署服務
https://ithelp.ithome.com.tw/upload/images/20200917/20072651r0IzcGvfGd.png

延續 Build, test, and deploy .NET Core apps 教學

預設 azure-pipelines.yaml

# 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

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'

steps:
- script: dotnet build --configuration $(buildConfiguration)
  displayName: 'dotnet build $(buildConfiguration)'

Push trigger

監控的Git分支,這邊可以設定單一個或是多個分支,
被監控的分支如果有更新就會驅動pipeline做動作。
單個範例

trigger:
- master

多個範例

trigger:
- master
- develop

Pool

建置環境可以設定 Windows、Linux 或是 macOS
可以查看可用環境 Microsoft-hosted agents
以預設的YAML檔來說,就是使用 ubuntu-latest(ubuntu-18.04) 環境

pool:
  vmImage: 'ubuntu-latest'

Variables 變數

如果有需要在下面重複使用,可以用變數來取代
設定 buildConfiguration 變數,內容為 Release

variables:
  buildConfiguration: 'Release'

還有變數群組可以使用 Add & use variable groups

Stage 階段

階段包含多個Job(工作)

stages:
- stage: Build
  jobs:
  - job: BuildJob
    steps:
    - script: echo Building!
- stage: Test
  jobs:
  - job: TestOnWindows
    steps:
    - script: echo Testing on Windows!
  - job: TestOnLinux
    steps:
    - script: echo Testing on Linux!
- stage: Deploy
  jobs:
  - job: Deploy
    steps:
    - script: echo Deploying the code!

Job 工作

工作包含多個step(步驟)


jobs:
- job: MyJob
  displayName: My First Job
  continueOnError: true
  workspace:
    clean: outputs
  steps:
  - script: echo My first job
Container Job
jobs:
- job: RunsInContainer
  container: # inline container specification
    image: ubuntu:16.04
    options: --hostname container-test --ip 192.168.0.1

Deployment job

jobs:
  # track deployments on the environment
- deployment: DeployWeb
  displayName: deploy Web App
  pool:
    vmImage: 'Ubuntu-16.04'
  # creates an environment if it doesn't exist
  environment: 'smarthotel-dev'
  strategy:
    # default deployment strategy, more coming...
    runOnce:
      deploy:
        steps:
        - script: echo my first deployment

Step 步驟

steps:
- script: echo This runs in the default shell on any machine
- bash: |
    echo This multiline script always runs in Bash.
    echo Even on Windows machines!
- pwsh: |
    Write-Host "This multiline script always runs in PowerShell Core."
    Write-Host "Even on non-Windows machines!"
Step 的 Schema 腳本
  • Script
  • Bash
  • pwsh
  • PowerShell
  • Checkout
  • Task
  • Step templates
Step 的 Properties 屬性
  • displayName
  • name
  • condition
  • continueOnError
  • enabled
  • env
  • timeoutInMinutes

如果使用 Step templates 可以呼叫不同的檔案

# File: steps/build.yml

steps:
- script: npm install
- script: npm test
# File: azure-pipelines.yml

jobs:
- job: macOS
  pool:
    vmImage: 'macOS-10.14'
  steps:
  - template: steps/build.yml # Template reference

- job: Linux
  pool:
    vmImage: 'ubuntu-16.04'
  steps:
  - template: steps/build.yml # Template reference

- job: Windows
  pool:
    vmImage: 'vs2017-win2016'
  steps:
  - template: steps/build.yml # Template reference
  - script: sign              # Extra step on Windows only

Task 任務

steps:
- task: VSBuild@1
  displayName: Build
  timeoutInMinutes: 120
  inputs:
    solution: '**\*.sln'

建置專案

- task: DotNetCoreCLI@2
inputs:
  version: '3.1.401'
  command: 'build'
  arguments: '--configuration $(buildConfiguration)'
displayName: 'dotnet build $(buildConfiguration)'

測試專案

- task: DotNetCoreCLI@2
  inputs:
    version: '3.1.401'
    command: test
    projects: '**/*Tests/*.csproj'
    arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'
  displayName: 'dotnet test $(buildConfiguration)'

發布專案

- task: DotNetCoreCLI@2
  inputs:
    version: '3.1.401'
    command: publish
    publishWebProjects: True
    arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
    zipAfterPublish: True
  displayName: 'dotnet publish $(buildConfiguration)'

提交部署

修改 azure-pipelines.yaml 檔案
修改之後 1.加入索引 2.提交版本 3. 上傳 之後 Azure pipeline 就會開始部署

git add .
git commit -m "update azure pipeline yaml"
git push

查看部署結果
https://ithelp.ithome.com.tw/upload/images/20200919/20072651evoWpYDN06.png

完整YAML檔案

# 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

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'

steps:
- task: DotNetCoreCLI@2
  inputs:
    version: '3.1.401'
    command: 'build'
    arguments: '--configuration $(buildConfiguration)'
  displayName: 'dotnet build $(buildConfiguration)'

- task: DotNetCoreCLI@2
  inputs:
    version: '3.1.401'
    command: test
    projects: '**/*Tests/*.csproj'
    arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'
  displayName: 'dotnet test $(buildConfiguration)'


- task: DotNetCoreCLI@2
  inputs:
    version: '3.1.401'
    command: publish
    publishWebProjects: True
    arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
    zipAfterPublish: True
  displayName: 'dotnet publish $(buildConfiguration)'

相關連結:

上一篇 Day18 Azure Pipelines服務
下一篇 Day20 實作 Dotnet Test 測試範例


上一篇
Day18 Azure Pipelines服務
下一篇
Day20 實作 Dotnet Test 測試範例
系列文
Azure 的自我修煉30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言