admin管理员组

文章数量:1393850

I am working with Azure Pipelines. We are using a set of templates for different applications where each references a template. Part of the pipeline template includes updating updating work items with references. This works fine but when testing changes to the template files I have been unable to implement any kind of checks for whether the template used is from the main branch or not. Anybody know how to access the specific branch when it is set through resources in the devops UI?

All the obvious answers such as:

  • pwsh:

    # Print information about the triggering source
    Write-Host "Triggering Repository: $env:BUILD_REPOSITORY_NAME"
    Write-Host "Triggering Branch: $env:BUILD_SOURCEBRANCH"
    Write-Host "Commit ID: $env:BUILD_SOURCEVERSION"
    

simply return info on the parent repository where the pipeline runs, I am interested in similar "commands"/approaches but for the template repository instead.

I am working with Azure Pipelines. We are using a set of templates for different applications where each references a template. Part of the pipeline template includes updating updating work items with references. This works fine but when testing changes to the template files I have been unable to implement any kind of checks for whether the template used is from the main branch or not. Anybody know how to access the specific branch when it is set through resources in the devops UI?

All the obvious answers such as:

  • pwsh:

    # Print information about the triggering source
    Write-Host "Triggering Repository: $env:BUILD_REPOSITORY_NAME"
    Write-Host "Triggering Branch: $env:BUILD_SOURCEBRANCH"
    Write-Host "Commit ID: $env:BUILD_SOURCEVERSION"
    

simply return info on the parent repository where the pipeline runs, I am interested in similar "commands"/approaches but for the template repository instead.

Share Improve this question asked Mar 27 at 14:08 user30080501user30080501 1 2
  • Hi @user30080501, Welcome to SO. Have you got a chance to check the answer below and do the repo resource variables provide the values you intend to use? Thx for the update. – Alvin Zhao - MSFT Commented Mar 28 at 1:47
  • Hi @user30080501, Good day to you. Can you please let us know if you have tried to use the variables in the answer below? If it resolves your query in this post, you may consider this, which may help others with similar concerns. Your sharing is much appreciated. – Aguy Commented Apr 1 at 2:03
Add a comment  | 

1 Answer 1

Reset to default 1

When you add a reference to a repository, there are a set of properties available on the resources object that you can use:

resources.repositories.<Alias>.name
resources.repositories.<Alias>.ref
resources.repositories.<Alias>.type
resources.repositories.<Alias>.id
resources.repositories.<Alias>.url
resources.repositories.<Alias>.version

You can access those properties using a runtime expression $[ <exp> ]

trigger: none

resources:
  repositories:
  - repository: templates # alias
    type: git
    name: Temp # projectName/repositoryName or just repositoryName
    ref: main # optional

variables:
  templateName: $[ resources.repositories.templates.name ]
  templateRef: $[ resources.repositories.templates.ref ]
  templateId: $[ resources.repositories.templates.id ]
  templateVersion: $[ resources.repositories.templates.version ]

steps:
- pwsh: |
    write-host "name: $(templateName)"
    write-host "ref: $(templateRef)"
    write-host "id: $(templateId)"
    write-host "version: $(templateVersion)"
  displayName: "show details about template repository"

Which produces the following output:

本文标签: Azure PipelinesReading Template Branch nameStack Overflow