在ADO管道中检查github存储库[YAML]

问题描述

我有一个github存储库:https://github.com/user-name/repo-name

我正在关注这份文档https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops#faq,以检出多个存储库。这是我的yaml文件

name: 1.0

resources:
  repositories:
  - repository: MyGitHubRepo
    type: github
    endpoint: https://github.com/user-name/repo-name
    name: repo-name
    ref: master

trigger:
- master
- develop
- features/*

pool:
  vmImage: "vs2017-win2016"

variables:
  "BuildConfiguration": 'debug'
  "Buildplatform": 'any cpu'

stages:

- checkout: MyGitHubRepo
- template: build.yml@MyGitHubRepo

enter image description here

解决方法

问题出在repositories.endpoint设置中。 endpoint设置应引用Azure Devops项目Service connection。项目服务连接是对Azure DevOps中项目级别的服务或资源的引用,允许您存储凭据等以安全地引用资源和服务,而无需在代码中存储这些资源的凭据。特定的Azure管道任务和azure-pipelines.yaml属性可以轻松引用服务连接。

要设置服务连接,请在Azure DevOps项目中的浏览器页面底部单击Project settings。然后在左侧菜单Pipelines下单击Service connections。在页面的右上角,单击New service connection,然后选择GitHub,然后单击Next

在下一页上,选择Grant authorization(如果尚未选择)。选择AzurePipelines作为OAuth配置,然后单击Authorize。确认GitHub弹出窗口,然后输入您的GitHub凭据。接下来,在GitHUb授权对话框中单击Authorize Azure pipelines。然后回到Azure DevOps页面,记下服务连接名称以供以后参考,然后单击Save完成创建服务连接。

然后返回到您的azure-pipelines.yaml修改,如下所示:

resources:
  repositories:
  - repository: MyGitHubRepo
    type: github
    endpoint: name_of_service_connection_you_created
    name: github-user-name/repo-name
    ref: master

确保将type设置为GitHub,并注意将name值设置为GitHub用户名和存储库名称(如username/reponame)的组合。

YAML schema中可以找到关于azure-pipelines.yaml中存储库资源的引用

创建文档here

,

步骤:

登录GitHub Token页面并创建PAT令牌。

打开项目设置->服务连接->单击按钮New service connection并选择GitHub类型以创建GitHub连接。

enter image description here

我分享的示例是将GitHub存储库发布为工件。

GitHub存储库。

enter image description here

YAML示例:

resources:
  repositories:
  - repository: vitol
    type: github
    endpoint: GithubTest
    name:  vitoliutest/vitol

trigger:
  - none
pool:
  name: Hosted Ubuntu 1604
steps:
  - checkout: vitol
  - task: CopyFiles@2
    inputs:
      SourceFolder: '$(Agent.BuildDirectory)'
      Contents: '**'
      TargetFolder: '$(build.artifactstagingdirectory)'
    
  - task: PublishBuildArtifacts@1
    inputs:
      PathtoPublish: '$(build.artifactstagingdirectory)'
      ArtifactName: 'drop'
      publishLocation: 'Container'

注意:请注意步骤checkout,它用于签出GitHub存储库。

结果

enter image description here