azure-devopsazure-pipelinescheckout

Using variable for tag in Repository Resource Definition in Azure DevOps


I want to use computed variable in the tag of Repository Resource Definition.

parameters:
  - name: tag
    displayName: tag
    type: string
    default: '0.0.1'

variables:
  - name: tag_name
    value: ${{ parameters.tag }}

resources:
  repositories:
  - repository: tag-checkout
    type: github
    ref: 'refs/tags/$(tag_name)'
    name: myrepo
    endpoint: 'myrepo'

stages:
  - stage: stage1
    jobs:
    - job: jobA
      steps:
        - task: CmdLine@2
          displayName: 'Set Version'
          inputs:
            script: |
              // compute the variable $myVariable then set variable tag_version to $myVariable
              echo "##vso[task.setvariable variable=tag_version;]$myVariable"
        - checkout: tag-checkout

I want to compute the tag version dynamically within stage1 and then use that computed tag (e.g., tag_version) to check out a specific tag version in the pipeline.

Using the ref: 'refs/tags/$(tag_name)' format, I can check out a tag version like 0.0.1, but I need to determine the tag during pipeline execution. I want to checkout $(tag_version) which is a computed variable in stage1.

I also explored the option of using inline syntax. However, according to the doc, inline syntax is limited to Azure Repos Git repositories within the same organization.

I need to checkout the repository in Github. Is there a way to use a computed variable for the ref parameter in this case?


Solution

  • I am afraid that the Pipeline runtime variable( $(tag_version) ) can not be used in the Repo Resources ref field.

    The ref field in the repo resources will be expanded at compile time, but the computed variable will be set at runtime. So it cannot be used in the repo resources.

    For a workaround, you can use the git command to clone the target repo tag source in the next tasks. You can use the variable: $(tag_version) in the git command.

    For example:

    stages:
      - stage: stage1
        jobs:
        - job: jobA
          steps:
            - checkout: none
            - task: CmdLine@2
              displayName: 'Set Version'
              inputs:
                script: |
                  // compute the variable $myVariable then set variable tag_version to $myVariable
                  echo "##vso[task.setvariable variable=tag_version;]$myVariable"
            - script: |
                git clone  --branch $(tag_version) githubrepoURL