How to get Git Tag in Azure Pipelines

前端 未结 8 1112
青春惊慌失措
青春惊慌失措 2021-02-07 07:28

In Azure Pipelines, I have enabled git tags to trigger pipelines like so:

trigger:
  branches:
    include:
    - \'*\'
  tags:
    include:
    - \'*\'
<         


        
8条回答
  •  旧巷少年郎
    2021-02-07 08:11

    git describe can provide you with the (closest) tag name for a given git hash and Azure can give you the current hash with $(Build.SourceVersion).

    Use the --exact-match to limit git describe to only use a tag from the specific commit:

    git describe --exact-match $(Build.SourceVersion)
    

    If there is a tag, it'll be returned on stdout:

    $ git describe --exact-match d9df242
    v1.0.0
    

    If there is no tag, git describe --exact-match exits with exit code 128:

    $ git describe --exact-match cc1f9d2
    fatal: no tag exactly matches 'cc1f9d23854c37dec000485c6c4009634516a148'
    $ echo $?
    128
    

    so you can use this in a test or simply fail the task in pipelines that trigger on more than just tagged revisions.

提交回复
热议问题