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

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


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


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)


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.