简体   繁体   English

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

[英]How to deploy to different environment in Azure Devops using YAML file

Can you please help me with below.你能帮我在下面吗?

I am building Azure Function app V3 and using Azure Devops YAML pipeline to build and deploy Azure function app and ARM infra to Dev environment. I am building Azure Function app V3 and using Azure Devops YAML pipeline to build and deploy Azure function app and ARM infra to Dev environment. Now I want to deploy the same to UAT.现在我想将其部署到 UAT。 I am not sure how to have different environment using YAML.我不确定如何使用 YAML 拥有不同的环境。 please find my azure-pipeline.yml file that I am using请找到我正在使用的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 

So what is the way to deploy it to Test environment.那么将它部署到测试环境的方法是什么。 Do I need to create another yaml file with different trigger in same repo?我是否需要在同一个仓库中创建另一个具有不同触发器的 yaml 文件? or different stage in same yaml file and apply some condition on stages when UAT branch changes happen then deploy to only UAT stage only(not dev stage).或同一 yaml 文件中的不同阶段,并在发生 UAT 分支更改时对阶段应用一些条件,然后仅部署到仅 UAT 阶段(而不是开发阶段)。

Any help is appreciated!!任何帮助表示赞赏! Thanks in advance提前致谢

You just need to add another stage with some conditions for the deployment to Test environment.您只需要添加另一个具有一些条件的阶段即可部署到测试环境。

Normally, you can set up a multi-stage pipeline that contains the main processes for your application, such as "Build", "Test" and "Deploy".通常,您可以设置一个多阶段管道,其中包含应用程序的主要流程,例如“构建”、“测试”和“部署”。 And like release pipeline, you also can set a stage for each deployment environment in the same pipeline.和发布管道一样,您也可以在同一管道中为每个部署环境设置一个阶段。

In your case, if you want that when new changes occur on the UAT branch, the deployment to Test environment can be triggered, you can set the condition like as below on the stage for Test environment.在您的情况下,如果您希望当 UAT 分支上发生新的更改时,可以触发部署到测试环境,您可以在测试环境的舞台上设置如下条件。

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

For more details, you can see:有关更多详细信息,您可以查看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM