is there any way to select branches from multiple repositories in CI build - azure-devops

I have trigger in azure-pipelines.yaml like below.
resources:
repositories:
- repository: APPLICATION
type: git
name: AAA/APPLICATION
ref: master
- repository: TESTS
type: git
name: AAA/TESTS
ref: master
STAGES:
- stage : BuildApplication
// checkout branch & build necessary things
- stage : BuildTests
// checkout branch & build necessary things
Since the yaml resides in Application repository, While creating manual CI build I am able to select the Branches in Application repository & for Tests repository the branch checkout will be master always.
is there any was I can able to set the branch details of Tests repository before creating release ?

Is there any was I can able to set the branch details of Tests repository before creating release ?
From your YAML sample, you need to select the Resource Repo branch when manually running the pipeline.
I am afraid that there is no out-of-box method can select the resource repo branch. The branch is defined at resources field. When you running the pipeline, it will use the default branch.
For a workaround, you can change to define the repo in check out field. You can use paramter to define the repo branches and then you can select the branch when you running the pipeline.
Refer to this doc: Inline syntax checkout
Here is an example:
parameters:
- name: test
values:
- refs/heads/main
- refs/heads/test1
pool:
vmImage: ubuntu-latest
steps:
- checkout: git://azure/Repo13#${{ parameters.test }}
Result:

Related

Triggering an Azure pipeline from a repository in a different organization

I'm trying to set up a pipeline that will trigger, when a commit is made in a repository that exists in a different organization.
In my own org, I've created a git repo with a yaml pipeline file in the main branch.
With the below setup, I can checkout the code from the other organization if I run the pipeline manually. But it is not triggered when a commit is pushed to that repository.
Looking at the documentation, this should be possible?
https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops#triggers
resources:
repositories:
- repository: OtherOrgRepo # In a different organization
endpoint: OtherOrgConnection
type: git
name: proj/reponame
ref: develop
trigger:
- develop
pool:
vmImage: ubuntu-latest
steps:
- checkout: OtherOrgRepo
The token used for the service connection has full access.
Is this not supported, or am I missing a step?
I guess I just need to read the big blue box:
https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops#triggers
Repository resource triggers only work for Azure Repos Git repositories in the same organization at present. They do not work for GitHub or Bitbucket repository resources.
I did however manage to trigger a classic pipeline by using a generic Git service connection, which will poll for changes at an interval.

Get the branch that triggered the pipeline in azure pipelines

I've an azure yaml based pipeline that has a CI trigger with a branch pattern filter. How can I get the name of the branch that has triggered the pipeline?
Pipeline looks like this -
parameters:
- name: TargetBranch
displayName: Target Branch
type: string
default: main
resources:
repositories:
- repository: <reponame>
type: git
name: <reponname>
ref: refs/heads/main
trigger:
branches:
include:
- es/branchtype1/*
pool:
<rest of the yml here that requires the branch name that triggered the pipeline>
There can be many branches with es/branchtype1/* name(created on daily basis). Whenever there
is a push to any of these branches, this pipeline gets triggered. I need to get the name of the branch on which the push was made.
You can use the Build variable Build.SourceBranch.
The branch of the triggering repo the build was queued for. Some examples:
Git repo branch: refs/heads/master
Git repo pull request: refs/pull/1/merge
TFVC repo branch: $/teamproject/main
TFVC repo gated check-in: Gated_2016-06-06_05.20.51.4369;username#live.com
TFVC repo shelveset build: myshelveset;username#live.com
When your pipeline is triggered by a tag: refs/tags/your-tag-name
When you use this variable in your build number format, the forward slash characters (/) are replaced with underscore characters (_).
Note: In TFVC, if you are running a gated check-in build or manually building a shelveset, you cannot use this variable in your build number format.
More information: Use predefined variables - Build variables (DevOps Services)

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

How to create a CI Trigger on a different Azure Repo than where the YAML pipeline resides?

I want my YAML in one repo RepoA and my code to build in RepoB. How do I configure the YAML to have a CI Trigger on the code RepoB only?
Note: these repos are in the same Azure DevOps project.
The YAML is in the default branch (master) of RepoA. I've seen that people have had issues with CI triggers if the pipeline is not in the default branch.
Here is the azure-pipeline.yml contents:
trigger: none
resources:
repositories:
- repository: RepoB
type: git
name: RepoB
ref: master
trigger:
- master
pool:
vmImage: 'windows-latest'
steps:
- checkout: RepoB
I also tried removing the line
trigger: none
to see if that would work. The build will then start as soon as I save the yaml, as I would expect, but not when I make a change to RepoB master.
Then I tried the following and many more guesses, but nothing ever enabled a CI Trigger on RepoB. That is, the pipeline never ran when I would make commits to the master branch in RepoB.
resources:
repositories:
- repository: RepoB
type: git
name: RepoB
ref: master
trigger:
branches:
include:
- master
pool:
vmImage: 'windows-latest'
steps:
- checkout: RepoB
Here is what I see when I look at the pipelines triggers in the Azure Pipelines UI. Should I see a trigger for RepoB?
Update 1:
Although these were not my original settings, I have updated the settings to be as open as possible (no limits). I then tried the following:
I committed a file to the branch in RepoB. No CI trigger occurred.
Deleted the pipeline, and recreated. I committed a file to the branch in RepoB. CI Trigger finally occurred!
I believe this is a bug because none of these settings should limit this scenario for the following reasons:
Both of the repos are in the same Project.
RepoB is explicitly referenced in the pipeline.
Also, you shouldn't have to delete and recreate a pipeline in order for a setting to take effect.
UPDATE 2:
I narrowed it down to this Organization or Project level setting: Limit job authorization scope to referenced Azure DevOps repositories
The documentation of this setting does not mention CI Triggers at all, but I do not think that it should affect this scenario regardless, because the repo is referenced explicitly.
Doc References:
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/access-tokens?view=azure-devops&tabs=yaml#limit-job-authorization-scope-to-referenced-azure-devops-repositories
https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/azure-repos-git?view=azure-devops&tabs=yaml#limit-job-authorization-scope-to-referenced-azure-devops-repositories
I believe this is a bug and I have reported it here:
https://developercommunity2.visualstudio.com/t/yaml-pipeline-ci-trigger-for-repository-resource-i/1314241
Azure DevOps enables some limitation to access to resources by default. Please check if this project enables below options in Project Settings page.
Testing in my side that if these options are enabled, this issue can be reproduced. Thus please disable them, and create a new yaml pipeline. The new yaml pipeline should work as expected.
See: Access repositories, artifacts, and other resources for details.
I used your yaml and all works (the only difference is that I have main not master branch)
trigger: none
resources:
repositories:
- repository: RepoB
type: git
name: azure-functions
ref: main
trigger:
- main
pool:
vmImage: 'windows-latest'
steps:
- checkout: RepoB
On this screen you have a trigger which fires for a change done on RepoB

azure pipeline pull dependency projects

I have a project which depends on 2-3 other projects, is there a way to pull them together with the master project?
When the build process starts projects will be on the file system and the master project can locate the other dependency projects?
As #Kehinde's said in comment, what you want could be achieved by the feature Multi-repo checkout.
Note:
Multi-repo checkout is the feature which only supported YAML. Because what the design logic is Checkouts from multiple repos in combination with YAML builds enable focusing the source level dependency management to one structured descriptor file in Git (the YAML biuld definition) for good visibility.
But for pipeline that configured by classic UI, you had to add those other repositories/projects as submodules, or as manual scripts to run git checkout in pipeline.
For personal, I strongly suggest you use YAML to achieve what you want.
Simple sample YAML definition:
resources:
repositories:
- repository: tools
name: Tools
type: git
steps:
- checkout: self
- checkout: tools
- script: dir $(Build.SourcesDirectory)
Here, image I have a repository called "MyCode" with a YAML pipeline, plus a second repository called "Tools".
In above third step(dir $(Build.SourcesDirectory)), it will show you two directories, "MyCode" and "Tools", in the sources directory.
Hope this helps.
For Bitbucket:
resources:
repositories:
- repository: MyBitBucketRepo
type: bitbucket
endpoint: MyBitBucketServiceConnection
name: {BitBucketOrg}/{BitBucketRepo}
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- checkout: self
- checkout: MyBitBucketRepo
- script: dir $(Build.SourcesDirectory)