iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤖

Retrieving the Trigger Tag Name in Azure Pipelines

に公開
2022/06/22

I originally wrote a different method, but I've corrected it after @noxi515 shared a better way with me. Thank you very much!

When using Git tags as a build trigger, it is common to want to retrieve the version from the tag at build time.

In Git, tags are objects under the path "refs/tags". In Azure DevOps, you can trigger CI by including "refs/tags" patterns as trigger branches.

Additionally, the triggering branch can be obtained from the Build.SourceBranch variable (or ${env:BUILD_SOURCEBRANCH} in PowerShell).

Therefore, for example, if you want to publish with a tag like "release/1.0.0", you can capture the tag into a variable by defining it as follows:

trigger:
  branches:
    include:
    - refs/tags/release/*

pool:
  vmImage: 'windows-latest'

steps:
- pwsh: |
    $source = ${env:BUILD_SOURCEBRANCH}
    $tag = $source.Substring('refs/tags/'.Length)
    Write-Host "##vso[task.setvariable variable=tag]$tag"
    Write-Host "##[debug]Debug Tag=$tag"
  displayName: Get trigger tag

Now you can refer to it as $(tag) from this point forward.

Discussion