練習 - 升階至測試階段

已完成

您的發行管線還是只有兩個階段,但已經變得和之前不同。 這兩個階段分別是組建開發。 您推送至 GitHub 的每個變更都會觸發組建階段的執行。 只有在變更位於發行分支時,才會執行開發階段。 在這裡,您要將測試階段新增至管線。

回想一下,小組決定使用排定的觸發程式,將組建從開發階段升級至上午 3 點的測試階段。 若要設定已排程的觸發程序:

  • 在組建設定中定義排程。
  • 定義測試階段,並於其中納入條件,使該階段只會在組建原因標示為 Schedule 時才執行。

為了方便學習,請在這裡定義排程,但允許組建直接從開發階段進入到測試階段。 此設定可讓您不必等候排程的觸發。 當您完成此課程模組之後,請嘗試使用不同的 cron 運算式進行實驗,以便只在排定的時間執行測試階段。

將變更升階至測試階段

在這裡,您會修改管線設定,以將組建部署到測試階段。

  1. 在 Visual Studio Code 中修改 azure-pipelines.yml,如下所示:

    trigger:
    - '*'
    
    variables:
      buildConfiguration: 'Release'
      releaseBranchName: 'release'
    
    schedules:
    - cron: '0 3 * * *'
      displayName: 'Deploy every day at 3 A.M.'
      branches:
        include:
        - release
      always: false 
    
    stages:
    - stage: 'Build'
      displayName: 'Build the web application'
      jobs: 
      - job: 'Build'
        displayName: 'Build job'
        pool:
          vmImage: 'ubuntu-20.04'
          demands:
          - npm
    
        variables:
          wwwrootDir: 'Tailspin.SpaceGame.Web/wwwroot'
          dotnetSdkVersion: '6.x'
    
        steps:
        - task: UseDotNet@2
          displayName: 'Use .NET SDK $(dotnetSdkVersion)'
          inputs:
            version: '$(dotnetSdkVersion)'
    
        - task: Npm@1
          displayName: 'Run npm install'
          inputs:
            verbose: false
    
        - script: './node_modules/.bin/node-sass $(wwwrootDir) --output $(wwwrootDir)'
          displayName: 'Compile Sass assets'
    
        - task: gulp@1
          displayName: 'Run gulp tasks'
    
        - script: 'echo "$(Build.DefinitionName), $(Build.BuildId), $(Build.BuildNumber)" > buildinfo.txt'
          displayName: 'Write build info'
          workingDirectory: $(wwwrootDir)
    
        - task: DotNetCoreCLI@2
          displayName: 'Restore project dependencies'
          inputs:
            command: 'restore'
            projects: '**/*.csproj'
    
        - task: DotNetCoreCLI@2
          displayName: 'Build the project - $(buildConfiguration)'
          inputs:
            command: 'build'
            arguments: '--no-restore --configuration $(buildConfiguration)'
            projects: '**/*.csproj'
    
        - task: DotNetCoreCLI@2
          displayName: 'Publish the project - $(buildConfiguration)'
          inputs:
            command: 'publish'
            projects: '**/*.csproj'
            publishWebProjects: false
            arguments: '--no-build --configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)/$(buildConfiguration)'
            zipAfterPublish: true
    
        - publish: '$(Build.ArtifactStagingDirectory)'
          artifact: drop
    
    - stage: 'Dev'
      displayName: 'Deploy to the dev environment'
      dependsOn: Build
      condition: |
        and
        (
          succeeded(),
          eq(variables['Build.SourceBranchName'], variables['releaseBranchName'])
        )
      jobs:
      - deployment: Deploy
        pool:
          vmImage: 'ubuntu-20.04'
        environment: dev
        variables:
        - group: Release
        strategy:
          runOnce:
            deploy:
              steps:
              - download: current
                artifact: drop
              - task: AzureWebApp@1
                displayName: 'Azure App Service Deploy: website'
                inputs:
                  azureSubscription: 'Resource Manager - Tailspin - Space Game'
                  appName: '$(WebAppNameDev)'
                  package: '$(Pipeline.Workspace)/drop/$(buildConfiguration)/*.zip'
    
    - stage: 'Test'
      displayName: 'Deploy to the test environment'
      dependsOn: Dev
      #condition: eq(variables['Build.Reason'], 'Schedule')
      jobs:
      - deployment: Deploy
        pool:
          vmImage: 'ubuntu-20.04'
        environment: test
        variables:
        - group: 'Release'
        strategy:
          runOnce:
            deploy:
              steps:
              - download: current
                artifact: drop
              - task: AzureWebApp@1
                displayName: 'Azure App Service Deploy: website'
                inputs:
                  azureSubscription: 'Resource Manager - Tailspin - Space Game'
                  appName: '$(WebAppNameTest)'
                  package: '$(Pipeline.Workspace)/drop/$(buildConfiguration)/*.zip'
    

    schedules 區段會定義一個 cron 運算式。 您可以在設定中定義多個運算式。 此運算式會在每天凌晨 3 點觸發管線,以針對發行分支來執行。 always 旗標會設定為 false,以便讓管線只會在發行分支包含上次執行後的變更時才執行。

    Test 階段會定義條件,以便只在組建原因等於 Schedule 時才開始執行。 (內建變數 Build.Reason 會定義組建原因。) 如果此條件為 false,則會略過此階段,但先前的階段會繼續執行。

    注意

    會顯示此條件以方便您學習。 其已加上註解,以便讓變更可以從開發進入到測試,而不必等待排程的觸發。

  2. 在整合式終端機中,將 azure-pipelines.yml 新增至索引。 然後認可變更,並將其推送至 GitHub。

    提示

    執行這些 Git 命令之前,請先儲存 azure-pipelines.yml

    git add azure-pipelines.yml
    git commit -m "Deploy to the Test stage"
    git push origin release
    
  3. 在 Azure Pipelines 中,移至該組建。 在建置執行時加以追蹤。

  4. 組建完成後,若要返回 [摘要] 頁面,請選取 [返回] 按鈕。

    A screenshot of Azure Pipelines showing three completed stages: Build, Dev, and Test.

    您會看到部署已順利完成。

  5. 從網頁瀏覽器,移至與您測試環境之 App Service 實例相關聯的 URL。

    如果您的瀏覽器索引標籤仍保持開啟,請重新整理頁面。 如果您不記得 URL,請在 Azure 入口網站的 [App Service 詳細資料] 頁面中找到該 URL。

    您會看到 Space Game 網站已部署至 App Service,且正在執行。

    A screenshot of a web browser showing the Space Game website in the Test environment.

  6. (選擇性步驟) 在 Azure Pipelines 中選取 [環境]。 然後,選取 [測試] 環境。

    Azure Pipelines 會記錄您的部署歷程記錄。 在歷程記錄中,您可以透過環境中的變更逆向追蹤到程式碼認可和工作項目。

    A screenshot of Azure Pipelines showing the deployment history. The history shows one successful deployment.

Andy 和 Mara 將測試階段新增至管線。 他們會向 Amita 顯示結果。

Amita:對於會每天早上建置和部署變更而讓我能夠進行測試,我感到非常滿意。 但我不知道要如何控制變更抵達預備環境的時間。

Mara: 是,透過自動化部署可節省大量時間。 請記住,我們只納入了已排程的觸發程序。 當我們設定 Tim 的 預備 環境時,讓我們為您新增發行核准。 如此一來,變更只會在您準備好時移至 預備 環境。