Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

Determining the triggering branch with a multi-repo CI setup in Azure Devops

In ADO, you can create a "repository resource" per this documentation. The "trigger" section allows you to define a CI trigger for any Azure repo in your space. Therefore, given the following config:

Repos:

  • AzureRepo1 - Contains project files that should be built
  • AzureRepo2 - Contains pipeline file 'pipeline.yml'
resources:
  repositories:
    - repository: "Azure_Repo_1"
      type: git
      name: AzureRepo1
      ref: development
      trigger: 
        branches:
          include: 
            - development
            - staging
            - production

pool:
  vmImage: 'windows-latest'

jobs:
- template: Template.yml
  parameters:
    service: "development"
    run_tests: true

When I make a change to AzureRepo1, the pipeline triggers. At runtime, how would I determine which branch ("production", "staging", or "development") of the target repo (AzureRepo1) triggered the build? Ideally, the "service" parameter being fed into the example template would dynamically reflect which branch triggered the build.

Note: "Build.SourceBranch" and "Build.SourceBranchName" seem to pull the branch from the repo that hosts the YML file (in this case, AzureRepo2).

I was wrong. These function as intended. Use the below solution.

question from:https://stackoverflow.com/questions/65887734/determining-the-triggering-branch-with-a-multi-repo-ci-setup-in-azure-devops

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to documentation here:

When an update to one of the repositories triggers a pipeline, then the following variables are set based on triggering repository:

  • Build.Repository.ID
  • Build.Repository.Name
  • Build.Repository.Provider
  • Build.Repository.Uri
  • Build.SourceBranch
  • Build.SourceBranchName
  • Build.SourceVersion
  • Build.SourceVersionMessage

For the triggering repository, the commit that triggered the pipeline determines the version of the code that is checked out. For other repositories, the ref defined in the YAML for that repository resource determines the default version that is checked out.

If triggers happens on AzureRepos1 you should have correct branch name in Build.SourceBranchName

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...