Oldfighter
Oldfighter

Reputation: 175

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

Upvotes: 2

Views: 1804

Answers (4)

Robin
Robin

Reputation: 31

This works on my side:

steps:
  - checkout: self
    persistCredentials: true

  # Rest of the pipeline

  - script: |
      git config --global user.name "BuildService"
      git config --global user.email "[email protected]"
      git tag "Build_$(Build.BuildNumber)"
      git push origin --tags
    workingDirectory: '$(Build.Repository.LocalPath)'

Running Git commands inside Azure DevOps pipeline

Upvotes: 0

Oldfighter
Oldfighter

Reputation: 175

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.

Upvotes: 1

Mr Qian
Mr Qian

Reputation: 23818

Please check if you choose the wrong repo's security settings. Or you could set the security settings for all Repositories under Project Settings--> Repositories.

enter image description here

Besides, check if your current account is under Platform Build Service group. And you could also add your current account there and set all the permission for your account.

Upvotes: 0

lylam
lylam

Reputation: 46

If you don't have any specific rule on which branches/commits to tag, you can set the pipeline to add Tag automatically upon completing the build

Add Tag automatically

Upvotes: 1

Related Questions