如何使用 YAML 文件部署到 Azure Devops 中的不同环境

How to deploy to different environment in Azure Devops using YAML file

你能帮我解决下面的问题吗?

我正在构建 Azure Function 应用程序 V3 并使用 Azure Devops YAML 管道构建 Azure 函数应用程序和 ARM 基础设施并将其部署到开发环境。现在我想将其部署到 UAT。我不确定如何使用 YAML 拥有不同的环境。 请找到我正在使用的azure-pipeline.yml文件

name: $(Build.DefinitionName)-$(Date:yyyyMMdd)$(Rev:.r)

trigger:
  - dev
resources:
  repositories:
    - repository: pipeline
      name: Pipeline
      type: git

pool:
  vmImage: 'windows-latest'

variables:
- name: Folder.BaseRepo # Location in repo where the templates are stored
  value: $(Build.SourcesDirectory)/Finance
- name: Folder.Templates # Location in repo where the templates are stored
  value: infrastructure

stages:
- stage: Build
  displayName: 'Build'
  jobs:
    - job: PublishTemplatesAndScripts
      displayName: 'Publish Templates and Scripts'
      steps:
        - template: 'publish-templates.yml@pipeline'
          parameters:
            templateFolder: '$(Folder.Templates)'
            artifactName: 'templates'
            pipelineRepository: pipeline
            pipelineRepositoryPath: pipeline
            
          
        - task: DotNetCoreCLI@2
          displayName: 'Restore dependencies'
          inputs:
            command: 'restore'
            projects: '$(Folder.BaseRepo)/Finance.sln'
            feedsToUse: 'select'
            vstsFeed: 'ac1301c4-6618-4824-a09e-0042d9871fb5/58ed1ece-4d06-46f7-b947-XXXX36281c4'
          enabled: true
        - task: DotNetCoreCLI@2
          displayName: 'Build function app'
          inputs:
            projects: '$(Folder.BaseRepo)/Finance.sln'
            command: 'build'
            arguments: '--configuration Release'
          enabled: true
        - task: DotNetCoreCLI@2
          displayName: 'Test function app'
          inputs:
            projects: '$(Folder.BaseRepo)/Finance.sln'
            command: 'test'
            arguments: '--configuration Release'
          enabled: false
        - task: DotNetCoreCLI@2
          displayName: 'Publish function app'
          inputs:
            command: 'publish'
            publishWebProjects: false
            projects: '$(Folder.BaseRepo)/src/Finance/Finance.csproj'
            arguments: '--output $(Build.ArtifactStagingDirectory)/publish --configuration Release'
          enabled: true
        - task: PublishPipelineArtifact@1
          displayName: 'Publish function app output'
          inputs:
            targetPath: '$(Build.ArtifactStagingDirectory)/publish'
            artifact: 'drop'
            publishLocation: 'pipeline'
          enabled: true

- stage: Development
  displayName: 'Development'
  jobs:
    - deployment: DevelopmentAzure
      displayName: 'Development Azure'
      environment: 'Development'
      #uses runtime expression
        strategy:
         runOnce:
           deploy:
             steps:
               - template: 'deploy-template.yml@pipeline'
                 parameters:
                   entryTemplateName: MyArm.json
                   templateParametersName: MyArm.dev.parameters.json
                   deploymentResourceManagerConnection: '$(Azure.NonProd.ResourceManagerConnection)'
                   deploymentSubscriptionIdentifier: '$(Azure.NonProd.SubscriptionId)'
                   resourceManagerConnection: '$(Azure.Dev.ResourceManagerConnection)'
                   subscriptionIdentifier: '$(Azure.Dev.SubscriptionId)'
                   resourceGroupName: '$(Resource_Group)'
                   outputVariablePrefix: AzureDeployment

    - deployment: DevelopmentFunctions
      displayName: 'DevelopmentFunctions'
      environment: 'Development'
      dependsOn: DevelopmentAzure
      strategy:
        runOnce:
          deploy:
            steps:
  
              - task: AzureFunctionApp@1
                inputs:
                  azureSubscription: 'ServiceConnection-XXXXX-DevTest'
                  appType: 'functionApp'
                  appName: 'XXX-xxx-dev-funcapp'
                  package: '$(Pipeline.Workspace)/drop/*.zip'
                  deploymentMethod: 'zipDeploy'
                enabled: true 

那么如何部署到测试环境呢?我是否需要在同一回购中创建另一个具有不同触发器的 yaml 文件?或同一 yaml 文件中的不同阶段,并在发生 UAT 分支更改时在阶段应用一些条件,然后仅部署到 UAT 阶段(而非开发阶段)。

感谢任何帮助!!提前致谢

您只需要添加另一个阶段,并为部署到测试环境提供一些条件。

通常,您可以设置一个多阶段管道,其中包含应用程序的主要流程,例如“构建”、“测试”和“部署”。和发布管道一样,您也可以在同一个管道中为每个部署环境设置一个阶段。

你的情况,如果你希望当UAT分支有新变化时,可以触发部署到测试环境,你可以在测试环境的阶段设置如下条件。

stages:
. . .
- stage: Test 
  displayName: 'Deploy to Test environment'
  dependsOn: Build
  condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/UAT'))
. . .

更多详情,您可以查看: