gitazure-devopsazure-pipelines

Cannot push git tags in Azure Devops pipeline


I am currently experiencing a problem that is really puzzling me. I want to tag a git commit via an Azure Devops build pipeline. I followed this tutorial: https://elanderson.net/2020/04/azure-devops-pipelines-manual-tagging/ and the only thing that I am diverging in is the use of bash instead of PowerShell. (I tried with PowerShell too, but I got the same error)

The error I am getting is this:

enter image description here

My build definition is here

variables:
  BuildConfiguration: release
  BuildPlatform: 'any cpu'

stages:
  - stage: DeployArtifacts
    jobs:
   
    - job: TagSources
      displayName: 'Tag Sources'
      pool:
        vmImage: 'ubuntu-20.04'
      steps:
      - checkout: self
        persistCredentials: true
        clean: true
        fetchDepth: 1

      - task: Bash@3
        inputs:
          targetType: 'inline'
          failOnStderr: false
          script: |                        
            #pwd
            #ls -alR
            Version="1.00"
            echo "Tagging branch with $Version"
            git tag $(Version)
            echo "Successfully tagged branch"
            git push --tags
            echo "Pushed tag to branch $Version"       

I also added the contribute permissions to the build account as suggested in this post (Azure pipeline does't allow to git push throwing 'GenericContribute' permission is needed) but I still get the same error. I am not sure what else I can try.

enter image description here

Any help is much appreciated. Thanks


Solution

  • I managed to fix the issue in the meanwhile myself. It is not very pretty but it works. I commented out persistCredentials: true and added the PAT in the origin of the git repository.

            # Fix as persistCredentials is not returning the desired result
            REMOTE_NAME=$(git remote get-url origin)
            echo "Remote name: $REMOTE_NAME"
            NEW_REMOTE="${REMOTE_NAME//https:\/\/XXXXX@/https:\/\/XXXXX:$(PAT)@}"
            echo "New Remote name: $NEW_REMOTE"
            git remote set-url origin $NEW_REMOTE
    

    Push now works without any issues.

    I am not sure why the proposed changes do not work in my Azure Devops account. Thanks for the help and answers anyway.