How to get Git Tag in Azure Pipelines

前端 未结 8 1089
青春惊慌失措
青春惊慌失措 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:05

    To answer your second question. If you don't mind having a separate pipeline for triggering through tags, then you can enable continuous integration and override the YAML trigger like shown below. This example will trigger builds with tags having the pattern 'test-*' (regardless of branch).

    By doing this, you can just execute git describe in your pipeline and it will output the name of the tag that triggered the build.

    Here you can see the result:

    0 讨论(0)
  • 2021-02-07 08:09

    When you configure the pipeline to be triggers with tag the meaning that when new tag is pushed the pipeline start to run. so:

    1) The pipeline will start from the git tag.

    2) I don't understand the question, if you pushed tag test so the tag name will be test.

    If you want to know programmatically if the build trigger was a tag and what is the tag name you can check the environment variable Build.SourceBranch if the build is from a tag the value will be: refs/tags/tagName.

    So just add a PowerShell task and print the value:

    Write-Host $env:Build_SourceBranch
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-07 08:17

    This need to consider different situations. If you just push tag or create it with UI, the pipeline are started from git tag. Just commit without any tag, it will started from git commit. No doubt, the build will be triggered just once.

    But if you push commit with tag, the build will be triggered twice. First is triggered by commit, and second is by tag. Check this pic.

    These means the pipeline started from a commit instead of tag.

    All in all, no matter which is first, the tag which trigger the build are all you pushed or created.

    For getting more intuitive view about this, you can add variable ' $(Build.SourceBranch)' in your build number. Here is my code about how to configure build number in YAML file:

    name: $(Build.SourceBranch)-$(date:yyyyMMdd)$(rev:.r)
    trigger:
      branches:
        include:
        - '*'
      tags:
        include:
        - '*'
    

    Here is the result of what triggered the build. If tag, it will shows refs_tags_{tagname}, if it's commit, it will shows refs_heads_{branchname}.

    0 讨论(0)
  • 2021-02-07 08:20

    The other answers here cover the first part of the question, so as Alex Kaszynski has already pointed out, you can use a YAML condition:

    startsWith(variables['Build.SourceBranch'], 'refs/tags/')
    

    Getting the tag name is now a bit easier than it was at the time the question was asked:

    Build.SourceBranchName
    

    This variable contains the last git ref path segment, so for example if the tag was refs/tags/1.0.2, this variable will contain 1.0.2: the tag name.

    Full docs are now here.

    0 讨论(0)
  • 2021-02-07 08:24

    To check if the commit was from a tag, use:

    startsWith(variables['Build.SourceBranch'], 'refs/tags/')
    

    From James Thurley:

    Get the name of the tag with:

    $tags = git tag --sort=-creatordate
    $tag = $tags[0]
    

    This sorts the tags correctly for both annotated and unannotated tags, and so the first result is the most recent tag.

    I've removed the original answer and replaced it with the correct one from James Thurley. I'd delete my answer, but it appears you can't delete an accepted answer.

    0 讨论(0)
提交回复
热议问题