Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)."
like image 341
MarkdotH Avatar asked Jun 27 '19 16:06

MarkdotH


2 Answers

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://${{ variables.repoName}}@${{ variables.branchRef }}

OR

 - checkout: git://${{ parameters.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
      url_with_token=$(echo $(System.CollectionUri) | sed -e "s/https\:\/\//https\:\/\/$(System.AccessToken)\@/g")
      git clone $url_with_token/$(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 }}
    
like image 100
scorpio Avatar answered Sep 30 '22 06:09

scorpio


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)
like image 31
Mengdi Liang Avatar answered Sep 30 '22 07:09

Mengdi Liang