查看 ADO 管道 [YAML] 中的 github 个存储库

Check out github repository in ADO pipeline [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

问题出在 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

在下一页,select Grant authorization 如果尚未 select 编辑。为 OAuth 配置选择 AzurePipelines,然后单击 Authorize。确认 GitHub pop-up 并输入您的 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 和 select GitHub 类型以创建 GitHub 连接。

我分享的示例是发布GitHub repo as Artifacts。

GitHub 回购。

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 repo。

结果