24

I'm trying to merge the develop branch to the master branch when building with Azure Pipelines PowerShell task.

But while executing the command git push, I'm getting this error:

Fatal: Could not read password for 'https://OrganizationName@dev.azure.com': terminal prompts disabled

The code repository is "Azure Repos Git".

git checkout -b master
git config --global user.email "xxxxxxx@xxxx.xxx"
git config --global user.name "xxxxx"
git merge origin/develop 
git push origin master

After referring some URLs, I've created the Personal Access Token, and modified the push command as git push https://PAT@dev.azure.com/OrganizationName, but it's still not working.

Please let me know, if you find a solution for this issue.

mekb
  • 554
  • 8
  • 22
VKD
  • 633
  • 2
  • 12
  • 28

7 Answers7

26

As you mentioned you need to use PAT but in this way:

git push https://{PAT}@dev.azure.com/{organization}/{project}/_git/{repo-name}

Another solution is to "Allow scripts to access the OAuth token" in the job options:

photo

In the git push use the System.AccessToken:

git push https://$env:SYSTEM_ACCESSTOKEN@dev.azure.com/......

And give push permissions to the build user (in the repo settings):

enter image description here

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • 12
    For .yaml build pipelines (currently the default for Azure Pipelines), set the `persistCredentials: true`. See the [Checkout](https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema%2Cparameter-schema#checkout) options. – Jan Dolejsi Apr 15 '20 at 08:26
  • there is no`contribute` permission in Azure today, what could be an alternate permission ? – JAHelia Apr 06 '22 at 07:33
  • I can see the `Contribute` permissions today.. – Shayki Abramczyk Apr 06 '22 at 07:34
7

Add checkout as the first step:


steps:
- checkout: self
  persistCredentials: true

Make sure you set the git config

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

Make sure to Grant version control permissions to the build service.

  1. Go to project settings --> Repositories menu --> Your repository --> Security tab, and grant the following permissions to the Project Collection Build Service ({your organization}) identity:
  • Create branch: Allow
  • Contribute: Allow
  • Read: Allow
  • Create tag: Allow

You should now be able to use git commands without having to manually append the access token to any git commands.

More info see here: https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/git-commands?view=azure-devops&tabs=yaml

Darrell
  • 1,905
  • 23
  • 31
1

Similar to Shayki's answer, but if you are not running a powershell task use:

git push https://$(System.AccessToken)@dev.azure.com/......

I am notably using

  • classic pipelines
  • an onprem Windows build agent
    • Agent job settings has Allow scripts to access the OAuth token enabled
  • command line task
felickz
  • 4,292
  • 3
  • 33
  • 37
1
# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
  - master
  - your-branch-name-here

pr: none

pool:
  vmImage: "macos-latest"

jobs:
  - job: Perform_Commit_From_CI
    steps:
      - checkout: self
        persistCredentials: true #Important - Persist creds to run further git command
        clean: true
      - task: NodeTool@0
        inputs:
          versionSpec: "16.13.2"
        displayName: "Install Node.js"
      - script: |
          git config --global user.email test@gmail.com
          git config --global user.name "Test User"
        displayName: Configure git
      - script: |
          yarn install
          yarn start NAME_OF_THE_SCRIPT_YOU_WANT_TO_EXECUTE
          git add -A
          git commit -m 'Test commit [skip ci]'
          git push origin HEAD:your-branch-name-here 
        displayName: "Test Script"

This will work without PAT.

Kailash Uniyal
  • 878
  • 1
  • 10
  • 14
  • Something that is important to note with this example is the `[skip ci]` part in the commit message. This will prevent the CI from triggering on the same branch with your push – Newteq Developer May 02 '23 at 17:16
0

An alternative to personal access tokens is to use a Git credential helper such as Git Credential Manager (included in Git for Windows) or git-credential-azure (included in several Linux distributions). Both support authentication to Azure Repos (dev.azure.com).

The first time you authenticate, the helper opens a browser window to Microsoft login. Subsequent authentication is non interactive.

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
-1

The issue might come up because the Azure Repository you are using is a Private one.

Changing the Project visibility to Public solved the issue.

-6

Delete C:\program files\Git\dev\fd and then reinstall Microsoft git.

WeiHua Liu
  • 49
  • 1
  • 3
  • This is completely unrelated to the OP's question. The OP is asking about azure devops. Not a local windows installation. And even then, to delete the files an reinstall git, how would that even solve the problem? – Newteq Developer May 02 '23 at 16:59