适用于所有存储库的 Azure DevOps 通用分支策略构建管道

时间:2021-03-08 12:47:42

标签: azure azure-devops azure-pipelines devops azure-repos

我们正在为我们的组织制定政策。我们的一个需求是在每个使用 Sonarcloud 的 Pull Request 中都有一个构建和注释。有没有办法创建一个通用的 ci 构建管道,该管道将在每个 PR 上运行,检查相应的 repo,检测项目类型(或读取清单文件等)进行代码分析和构建,注释 PR?< /p>

因此,在 Azure DevOps 存储库中,您可以为所有主分支创建通用分支策略。我尝试使用独立的 yaml 管道,但在我创建 pr 时它从未启动。有人可以帮助我走上正轨吗?我需要在 yaml 中创建资源吗?我可以使用 PR 中的任何变量来检测 repo 和分支吗?

仅供大家理解,您可以创建通用的分支策略和个人。

非常感谢

1 个答案:

答案 0 :(得分:0)

由于 this thread 中报告的已知问题,您需要在 yaml 的资源部分添加所有存储库。

resources:
  repositories:
    - repository: MyRepo
      type: git
      name: MyRepo

但是,如果您在 Build Validation 中使用经典 UI 管道而不是 yaml 管道。您不需要在资源部分添加所有存储库。但是你需要跳过管道来同步源:(转到管道编辑页面-->点击获取源-->勾选不同步源)

enter image description here

您可以使用 predefined variables 获取有关拉取请求的信息。见下文:

$(Build.Repository.Name)
$(System.PullRequest.PullRequestId)

然后您可以在脚本任务中运行 git 命令来检查拉取请求分支。见下例:

resources:
  repositories:
    - repository: MyRepo
      type: git
      name: MyRepo
pool: 
  vmImage: windows-latest

steps:
- checkout: none
- powershell: |
    
    git clone "https://$(System.AccessToken)@dev.azure.com/OrganizationName/$(System.TeamProject)/_git/$(Build.Repository.Name)"
  
    cd "$(Build.Repository.Name)"
    git fetch origin pull/$(System.PullRequest.PullRequestId)/merge:pr-$(System.PullRequest.PullRequestId) 
    # checkout pr branch
    git checkout pr-$(System.PullRequest.PullRequestId)

注意:您需要为目标仓库授予build service account读取权限

相关问题