7
votes

I have two AzureDevOps Git branches:

master
feature/mybranch

I have a multi-stage build pipeline defined in yaml, where some of the steps are templated into separate .yml files.

In my outer azure-pipelines.yml I reference a repository where my template .yml's live:

resources:
  repositories:
    - repository: templates
      type: git
      name: MyProject/MyRepo

when I'm building in the 'master' branch everything is good as by default the repository will look in refs/heads/master.

when I'm working in the feature branch and I want to test experimental changes to my template .yml files, I don't want it to fetch them from the master branch, I want it to use the files from the branch I am working in.

The following works and allows me to do this:

resources:
  repositories:
    - repository: templates
      type: git
      name: MyProject/MyRepo
      ref: refs/heads/feature/mybranch

However, when I merge this back to master, I obviously don't want 'ref:' still pointing at the feature branch, so I'd like to generate the value of 'ref:' dynamically with a variable.

I've tried using ref: $(Build.SourceBranch) where $(Build.SourceBranch) should expand to 'refs/heads/feature/mybranch'

But it doesn't work. Error:

62638: "/azure-pipelines.yml: Could not get the latest source version for repository MySolution hosted on Azure Repos using ref refs/heads/$(Build.SourceBranch)."
3
What do you mean by the "current" branch? If you queue a build, you can choose the branch. If a build is triggered by a CI trigger, it will automatically use the branch that triggered CI. There's nothing in YAML you have to specify for this behavior.Daniel Mann
Daniel Mann - I've updated the question to hopefully make it clearer what I'm trying to achieveMarkdotH
Does your issue solved now? Feel free tot let us know the latest status.Merlin Liang - MSFT

3 Answers

6
votes

Instead of referencing the repo in resources, use inline checkout as described here

https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops#checking-out-a-specific-ref

- checkout: git://MyProject/MyRepo@features/tools

And this yaml element allows use of template expressions using variables, parameters e.g.

- checkout: git://$(System.TeamProject)/$(repoName)@${{ variables.branchRef }}

OR

 - checkout: git://$(System.TeamProject)/$(repoName)@${{ parameters.branchRef }}

And you can change that dynamically

Or the other alternative is use script task as below

- script: |
      # note checkout: git://$(System.TeamProject)/${{ parameters.repoName }}@${{ parameters.repoRef }} this does not work if this task is run multiple times in same pipeline
      # see here for more details :https://developercommunity.visualstudio.com/t/checkout-fails-with-an-error-occurred-while-loadin/1005991#T-N1270459
      repoDir=$(Agent.BuildDirectory)/${{ parameters.repoName }}
      /bin/rm -rf $repoDir
      git clone https://$(System.AccessToken)@$(System.CollectionUri)/$(System.TeamProject)/_git/${{ parameters.repoName }} $repoDir
      cd $repoDir
      git fetch origin '${{ parameters.repoRef }}':'localBranch'
      git checkout localBranch
    name: clone_script
    displayName: Checkout using script ${{ parameters.repoName }}@${{ parameters.repoRef }}
    
1
votes

Is it possible to use a variable in the ref property of resources:repository for Azure DevOps YAML?

For this question, the answer is Yes, it's possible.

About why you receive that error message, just is the variable($(Build.SourceBranch)) you used is incorrect. You should use $(Build.SourceBranchName).

As normal, for ref, we should input master or any other feature branches. Such as

ref: refs/heads/master

This may make you thought that this is same with the value of $(Build.SourceBranch). It looks same, I know, but different. In fact, for server, it will read the exactly branch name not the branch path, which we can clearly figure out with the classic editor type:

enter image description here

According with classic editor type, we can know here we should input the exactly branch name.

So, as the Predefined variables defined, the value of $(Build.SourceBranch) is the branch path, but for $(Build.SourceBranchName), it's represent a exactly branch name.

So, if you want to execute successfully, you need to use : $(Build.SourceBranchName). And it's worked on my side.

Hope this also can help you stay away from the error message.

Edit:

The complete script which is worked for me is:

resources:
  repositories:
    - repository: templates
      type: git
      name: MyApp/MyconApp
      ref: $(Build.SourceBranchName)
0
votes

The azure docs state

Variables can't be used to define a repository in a YAML statement.

So that seems to place some limitations on what you can do here. Perhaps there is a workaround that still allows you to do what you want.