练习 - 向管道添加预览阶段

已完成

你想要向管道添加一个额外的阶段,以便可以检查将对 Azure 环境进行的更改。

在此过程中,你将:

  • 更新管道 YAML 文件以添加新的预览阶段。
  • 向 Azure Pipelines 添加环境。
  • 将环境配置为需要审批。
  • 更新管道 YAML 文件,将环境用于部署阶段。
  • 查看 What-if 结果并批准管道运行。

更新管道定义以添加预览阶段

在这里,可以向运行 What-if 操作的管道添加一个新阶段。

  1. 在 Visual Studio Code 中,打开 deploy 文件夹中的 azure-pipelines.yml 文件。

  2. 在“验证”和“部署”阶段之间,为“预览”阶段添加以下定义:

    - stage: Preview
      jobs:
      - job: PreviewAzureChanges
        displayName: Preview Azure changes
        steps:
          - task: AzureCLI@2
            name: RunWhatIf
            displayName: Run what-if
            inputs:
              azureSubscription: $(ServiceConnectionName)
              scriptType: 'bash'
              scriptLocation: 'inlineScript'
              inlineScript: |
                az deployment group what-if \
                  --resource-group $(ResourceGroupName) \
                  --template-file deploy/main.bicep \
                  --parameters environmentType=$(EnvironmentType)
    
  3. 保存对该文件所做的更改。

添加环境

  1. 在浏览器中,转到“管道”>“环境”。

    Screenshot of the Azure DevOps interface that shows the Pipelines menu, with the Environments item highlighted.

  2. 选择“创建环境”。

    Screenshot of the Azure DevOps interface that shows the Environments page, with the button for creating an environment highlighted.

  3. 输入“Website”作为环境名称。

    将说明保留为空。 对于“资源”,选择“无”。

    备注

    在 Azure Pipelines 中,环境用于启用部署功能。 其中一些功能仅在部署到 Kubernetes 或虚拟机时适用。 在本模块中,我们不会使用这些功能,可以将其忽略。

  4. 选择创建

    Screenshot of the Azure DevOps page for a new environment, with the details completed and the Create button highlighted.

向环境添加审批检查

  1. 选择屏幕左上角的“审批和检查”选项卡。

    Screenshot of the Azure DevOps interface that shows the Website environment, with the Approvals and checks tab highlighted.

  2. 选择“审批”。

    Screenshot of the Azure DevOps interface that shows the page for adding a check, with the Approvals item highlighted.

  3. 在“审批者”文本框中,键入你自己的姓名并选择你自己。

  4. 选择“高级”旁边的箭头按钮。

    请注意,默认情况下,审批者可以批准其自身触发的运行。 由于你是唯一将使用此管道的人,因此请选中此复选框。

  5. 选择创建

    Screenshot of the Azure DevOps interface that shows the page for adding an approval check, with the details completed and the Create button highlighted.

将管道定义更新为需要环境和审批

接下来,将“部署”阶段配置为针对之前创建的“网站”环境运行。 将“部署”阶段转换为运行部署作业而不是标准作业,并将其配置为部署到环境中。

  1. 在 Visual Studio Code 中的 azure-pipelines.yml 文件中,将“部署”阶段定义替换为以下代码:

    - stage: Deploy
      jobs:
      - deployment: DeployWebsite
        displayName: Deploy website
        environment: Website
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: self
    
                - task: AzureResourceManagerTemplateDeployment@3
                  name: DeployBicepFile
                  displayName: Deploy Bicep file
                  inputs:
                    connectedServiceName: $(ServiceConnectionName)
                    deploymentName: $(Build.BuildNumber)
                    location: $(deploymentDefaultLocation)
                    resourceGroupName: $(ResourceGroupName)
                    csmFile: deploy/main.bicep
                    overrideParameters: >
                      -environmentType $(EnvironmentType)
    

    请注意,你定义了一个新的 checkout 步骤。 与普通作业不同,部署作业需要配置为从 Git 存储库签出(下载)文件。 如果不执行此步骤,部署作业将无法读取 Bicep 文件。 可以考虑使用管道工件在管道阶段之间发送文件。 我们在总结中提供了指向工件详细信息的链接。

  2. 保存文件。

验证并提交管道定义

  1. 验证 azure-pipelines.yml 文件是否如下代码所示:

    trigger:
      batch: true
      branches:
        include:
        - main
    
    pool:
      vmImage: ubuntu-latest
    
    variables:
      - name: deploymentDefaultLocation
        value: westus3
    
    stages:
    
    - stage: Lint
      jobs:
      - job: LintCode
        displayName: Lint code
        steps:
          - script: |
              az bicep build --file deploy/main.bicep
            name: LintBicepCode
            displayName: Run Bicep linter
    
    - stage: Validate
      jobs:
      - job: ValidateBicepCode
        displayName: Validate Bicep code
        steps:
          - task: AzureResourceManagerTemplateDeployment@3
            name: RunPreflightValidation
            displayName: Run preflight validation
            inputs:
              connectedServiceName: $(ServiceConnectionName)
              location: $(deploymentDefaultLocation)
              deploymentMode: Validation
              resourceGroupName: $(ResourceGroupName)
              csmFile: deploy/main.bicep
              overrideParameters: >
                -environmentType $(EnvironmentType)
    
    - stage: Preview
      jobs:
      - job: PreviewAzureChanges
        displayName: Preview Azure changes
        steps:
          - task: AzureCLI@2
            name: RunWhatIf
            displayName: Run what-if
            inputs:
              azureSubscription: $(ServiceConnectionName)
              scriptType: 'bash'
              scriptLocation: 'inlineScript'
              inlineScript: |
                az deployment group what-if \
                  --resource-group $(ResourceGroupName) \
                  --template-file deploy/main.bicep \
                  --parameters environmentType=$(EnvironmentType)
    
    - stage: Deploy
      jobs:
      - deployment: DeployWebsite
        displayName: Deploy website
        environment: Website
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: self
    
                - task: AzureResourceManagerTemplateDeployment@3
                  name: DeployBicepFile
                  displayName: Deploy Bicep file
                  inputs:
                    connectedServiceName: $(ServiceConnectionName)
                    deploymentName: $(Build.BuildNumber)
                    location: $(deploymentDefaultLocation)
                    resourceGroupName: $(ResourceGroupName)
                    csmFile: deploy/main.bicep
                    overrideParameters: >
                      -environmentType $(EnvironmentType)
    

    如果不是,请更新它以匹配此示例,然后保存。

  2. 通过在 Visual Studio Code 终端中运行以下命令来提交更改并将其推送到 Git 存储库:

    git add .
    git commit -m "Add preview stage"
    git push
    

运行管道并审阅 What-if 输出

  1. 在浏览器中,转到你的管道。

  2. 选择管道的最新运行。

    等待管道完成“Lint 分析”、“验证”和“预览”阶段。 虽然 Azure Pipelines 会使用最新状态自动更新页面,但仍建议偶尔刷新页面。

  3. 如果要求授予访问资源的权限,请选择“查看”,然后选择“允许”。

  4. 请注意,Azure Pipelines 会提示你进行审批。 你还会收到一封电子邮件,告知你需要审批管道。

    Screenshot of the Azure DevOps interface that shows the pipeline run, with the approval requirement highlighted.

    在批准继续运行管道之前,需要审阅 What-if 结果,确保它们符合预期。

  5. 选择“预览”阶段。

  6. 选择“运行 What-if”步骤以检查 What-if 命令报告的更改。

  7. 请注意,管道日志提供类似于以下代码的 What-if 结果:

    Resource and property changes are indicated with these symbols:
      + Create
      ~ Modify
      = Nochange
    
    The deployment will update the following scope:
    
    Scope: /subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/ToyWebsiteTest
    
      ~ Microsoft.Web/sites/toy-website-nbfnedv766snk [2021-01-15]
        + properties.siteConfig.localMySqlEnabled:   false
        + properties.siteConfig.netFrameworkVersion: "v4.6"
    
      = Microsoft.Insights/components/toywebsite [2020-02-02]
      = Microsoft.Storage/storageAccounts/mystoragenbfnedv766snk [2021-04-01]
      = Microsoft.Web/serverfarms/toy-website [2021-01-15]
    
    Resource changes: 1 to modify, 3 no change.
    

    What-if 操作已检测到对网站资源的更改。 但是,它检测到的更改是干扰项。 它们并不代表对资源的实际更改。 随着时间的推移,Azure 团队会努力减少干扰。 同时,对于这两个特定属性,可以忽略检测到的更改。

    你还可能会在资源类型 microsoft.alertsmanagement/smartDetectorAlertRules/Failure Anomalies - toywebsite 的 What-if 输出中看到一个项。 Application Insights 会自动创建此资源。 What-if 命令检测到不会对资源进行任何更改。

批准管道运行

  1. 选择向左箭头,返回到管道运行详细信息。

    Screenshot of the Azure DevOps interface that shows the pipeline log menu, with the back arrow highlighted.

  2. 选择审批面板上的“审阅”按钮。

  3. 在“注释”框中,输入“已审阅的 What-if 结果”。

  4. 选择“批准”。

    Screenshot of the Azure DevOps interface that shows the pipeline approval page, with the Approve button highlighted.

观察部署是否成功

  1. 在批准管道运行后,请注意“部署”阶段开始运行。

    等待该阶段完成。

  2. 请注意,管道运行成功完成。