'Azure pipelines only add extra docker tag if a git tag is pushed

I'm building a CI pipeline within azure pipelines and am struggling with the following, I have a buildstep that is always triggered. Hence I give my image the commit hash as tag for future reference. However, I want to add the gittag name whenever this has triggered the push. Is there a way to conditionally add this tag? My current task is:

- task: Docker@2                                                               
  inputs:                                                                      
    containerRegistry: 'my-registry'                                           
    repository: '$(System.TeamProject)/$(Build.Repository.Name)'               
    command: 'buildAndPush'                                                    
    Dockerfile: 'Dockerfile'                                                   
    tags: |                                                                    
      $(Build.SourceVersion)                                                   
      $(Build.SourceBranchName)

This adds the tag, but whenever a commit to a branch was the trigger I also get a branch tag (develop / main), I'd like to prevent this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source