从Azure DevOps经典管道执行YAML模板

时间:2020-02-12 15:37:18

标签: azure-devops yaml azure-pipelines

我将通过以下几点来提出我的问题,希望现在可以弄清楚:

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

问题陈述:

  • 除非基于事件pr,commit等在源代码存储库中,否则不会触发管道YAML。
  • 只要application_code存储库中触发了事件,我们是否可以触发或执行Pipeline_repo中的YAML文件?

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

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

有没有比以上任何想法或更好的解决方案了?

1 个答案:

答案 0 :(得分:0)

Azure devops服务即将推出功能对YAML管道的多存储库支持。此功能将支持基于多个存储库之一中的更改触发管道。请检查Azure DevOps Feature Timelinehere。预计该功能将于2020年第一季度针对天蓝色devops服务推出。

当前,您可以按照以下变通办法使用 Build Completion (构建完成)来实现以上目的(管道将在另一个构建完成时触发)。

1,设置触发管道

为application_code repo创建一个空的经典管道作为触发管道,该管道将始终成功并且不执行任何操作。

并在触发器标签下选中启用持续集成,并设置 Bracnh过滤器 enter image description here

2,设置触发的管道

在pipeline_code存储库中,使用CheckoutCheck out multiple repositories in your pipeline。您可以专门签出要构建的application_code repo的源代码。请参考以下示例:

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

  - task: TaskName
     ...

然后在yaml管道编辑页面中,点击右上角的 3点,然后点击触发。然后,点击构建完成旁边的 +添加,然后在步骤1中创建的触发管道上方选择<触发触发构建。

enter image description here

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

更新:

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

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

您可以使用环境变量Agent.JobStatus获得build status。对于下面的示例:

有关更多信息,请参阅文档Integrate your build system with Bitbucket Cloudthis thread

  - 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)