首页 > 解决方案 > 从 Azure DevOps 经典管道执行 YAML 模板

问题描述

我将通过以下几点提出我的问题,希望现在已经清楚了:

  1. 应用程序源代码位于 application_code 存储库中。
  2. 管道代码(YAML)在 pipeline_code 存储库中。因为我想对其进行版本化并且不喜欢保留在 application_code repo 中。只是为了避免将控制权交给开发团队来管理它。

问题陈述:

我已经尝试使用经典管道和 YAML 模板实现上述目标,但这不能一起工作。因为我只能从 YAML 管道执行 YAML 模板,而不能从如下经典管道执行:

#azure-pipeline.yaml
jobs:  
  - job: NewJob
  - template: job-template-bd1.yaml 

有什么想法或比上面更好的解决方案吗?

标签: azure-devopsyamlazure-pipelines

解决方案


YAML 管道的多存储库支持功能将很快用于 azure devops 服务。此功能将支持根据在多个存储库之一中所做的更改触发管道。请查看Azure DevOps 功能时间线此处。此功能预计将于 2020 年第一季度推出,用于 ​​azure devops 服务。

目前,您可以按照以下解决方法使用Build Completion实现上述目标(管道将在另一个构建完成时触发)。

1、设置触发管道

为 application_code repo 创建一个空的经典管道作为触发管道,它将始终成功并且什么都不做。

并选中触发器选项卡下的启用持续集成并设置Bracnh 过滤器 在此处输入图像描述

2、设置触发管道

在 pipeline_code 存储库中,使用Checkout检查管道中的多个存储库。您可以专门签出 application_code repo 的源代码来构建。请参考以下示例:

steps: 
  - checkout: git://MyProject/application_code_repo@refs/heads/master # Azure Repos Git repository in the same organization

  - task: TaskName
     ...

然后在yaml pipeline编辑页面,点击右上角的3点,点击Triggers。然后单击Build Completion旁边的+Add并选择上面在步骤 1 中创建的触发管道作为触发构建

在此处输入图像描述

完成以上两步后,当对 application_code repo 进行更改时,触发管道将被执行并成功完成。然后触发的管道将被触发以运行真正的构建作业。

更新:

在 Bitbucket 中显示 Azure DevOps 构建管道状态。

您可以在 yaml 管道末尾添加一个 python 脚本任务来更新 Bitbucket 构建状态。condtion: always()即使其他任务失败,您也需要设置 a以始终运行此任务。

您可以使用 env variable获取构建状态Agent.JobStatus。对于以下示例:

有关更多信息,请参阅文档将您的构建系统与 Bitbucket Cloud 集成,以及此线程

  - task: PythonScript@0
      condition: always()
      inputs: 
        scriptSource: inline
        script: |
          import os
          import requests

          # Use environment variables that your CI server provides to the key, name,
          # and url parameters, as well as commit hash. (The values below are used by
          # Jenkins.)
          data = {
              'key': os.getenv('BUILD_ID'),
              'state': os.getenv('Agent.JobStatus'),  
              'name': os.getenv('JOB_NAME'),
              'url': os.getenv('BUILD_URL'),
              'description': 'The build passed.'
          }

          # Construct the URL with the API endpoint where the commit status should be
          # posted (provide the appropriate owner and slug for your repo).
          api_url = ('https://api.bitbucket.org/2.0/repositories/'
                    '%(owner)s/%(repo_slug)s/commit/%(revision)s/statuses/build'
                    % {'owner': 'emmap1',
                        'repo_slug': 'MyRepo',
                        'revision': os.getenv('GIT_COMMIT')})

          # Post the status to Bitbucket. (Include valid credentials here for basic auth.
          # You could also use team name and API key.)
          requests.post(api_url, auth=('auth_user', 'auth_password'), json=data)

推荐阅读