The setup:
I have a database repo and a unit-test repo that tests the database repo, both of these share a project in Azure DevOps. I have a CI pipeline that resides in the database project that is triggered on pushes to the remote-tracking branch. The trigger will run the CI when any branch in that directory is pushed to.
The problem:
I need to check out the unit-test project as well so that I can call it. We won't know what names topic branches will have until we create the tasks etc, how can this be captured dynamically?
So an engineer can work on more than 1 branch with CI support without having to worry about tidy up if urgency is raised.
I have read this; https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops#repository-details.
I tried:
trigger:
- dev/a/*
steps:
- checkout: git://proj/repo-a@dev/a/*
- checkout: git://proj/repo-b@dev/a/*
Just to see if it was possible to get projects side-by-side.
It raises error:
The pipeline is not valid. Could not get the latest source version for repository repo-a hosted on Azure Repos using ref refs/heads/dev/a/. Could not get the latest source version for repository repo-b hosted on Azure Repos using ref refs/heads/dev/a/.
I also tried the above without the wildcard, no joy. Another point is that I would not want to checkout all branches, just the branch with the push.
Surely this is possible ...
CodePudding user response:
To dynamically checkout branches, you can use the predefined variable: Build.SourceBranchName
.
For example:
- checkout: git://proj/repo-a@$(Build.SourceBranchName)
Since you have referenced two repos, you also need to add a If expression to determine which repo trigger the build and use the target repo branch name.
You can use the variable: Build.Repository.Name
Here is an example:
steps:
- ${{ if eq(variables['BUILD.REPOSITORY.NAME'], 'repo-a') }}:
- checkout: git://proj/repo-a@$(Build.SourceBranchName)
- ${{ if eq(variables['BUILD.REPOSITORY.NAME'], 'repo-b') }}:
- checkout: git://proj/repo-b@$(Build.SourceBranchName)
Or you can use condition.
For example:
- checkout: git://proj/repo-a@$(Build.SourceBranchName)
condition: eq(variables['BUILD.REPOSITORY.NAME'], 'repo-a')
- checkout: git://proj/repo-b@$(Build.SourceBranchName)
condition: eq(variables['BUILD.REPOSITORY.NAME'], 'repo-b')
Here is the docs about expression and condition.