SOLVED

powershell to run git commit using PAT in Azure github pipelines

%3CLINGO-SUB%20id%3D%22lingo-sub-1085081%22%20slang%3D%22en-US%22%3Epowershell%20to%20run%20git%20commit%20using%20PAT%20in%20Azure%20github%20pipelines%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1085081%22%20slang%3D%22en-US%22%3E%3CP%3EI%20created%20a%20Personal%20Access%20Token%20for%20my%20repository%20and%20named%20it%20as%20'PAT'%2C%20created%20a%20variable%20called%20'PATSecret'%20in%20my%20pipeline%20for%20the%20secret%20of%20PAT.%20Passing%20PATSecret%20as%20an%20input%20variable%20for%20%24Cred.%20But%20when%20trying%20to%20use%20this%20secret%20to%20run%20commit%20command%2C%20authentication%20is%20failing.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CSPAN%3Eparam%3C%2FSPAN%3E%3CSPAN%3E(%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3CDIV%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CSPAN%3E%5Bstring%5D%3C%2FSPAN%3E%3CSPAN%3E%24Cred%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3CDIV%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CSPAN%3E)%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%3E%3CDIV%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CSPAN%3E%26nbsp%3B%26nbsp%3B%26nbsp%3Bgit%26nbsp%3B--version%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3CDIV%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CSPAN%3E%26nbsp%3B%26nbsp%3B%26nbsp%3Bgit%26nbsp%3Badd%26nbsp%3B.%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3CDIV%3E%3CFONT%20color%3D%22%230000FF%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CSPAN%3EWrite-Host%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%22setup%26nbsp%3Bauthor%26nbsp%3Binfo%22%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3CDIV%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CSPAN%3E%26nbsp%3B%26nbsp%3B%26nbsp%3Bgit%26nbsp%3Bconfig%26nbsp%3Buser.email%26nbsp%3Byou%40you.com%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3CDIV%3E%3CFONT%20color%3D%22%230000FF%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CSPAN%3Egit%26nbsp%3Bconfig%26nbsp%3Buser.name%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%22your%26nbsp%3Bname%22%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3CDIV%3E%3CFONT%20color%3D%22%230000FF%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CSPAN%3EWrite-Host%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%22git%26nbsp%3Bcommit%26nbsp%3Bwith%26nbsp%3Bmessage%22%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3CDIV%3E%3CFONT%20color%3D%22%230000FF%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CSPAN%3Egit%26nbsp%3Bcommit%26nbsp%3B-m%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%22Test%26nbsp%3BCommit%26nbsp%3Bfrom%26nbsp%3BAzure%26nbsp%3BDevOps%22%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CFONT%20color%3D%22%230000FF%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CSPAN%3Egit%26nbsp%3Bpush%26nbsp%3B-u%26nbsp%3Bhttps%3A%2F%2FPAT%3A%24(%3C%2FSPAN%3E%3CSPAN%3E%24cred%3C%2FSPAN%3E%3CSPAN%3E)%40dev.azure.com%2Fproject%2F_git%2Fmyrepo%20HEAD%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CU%3EError%20message%3C%2FU%3E%3C%2FFONT%3E%3A%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CDIV%20class%3D%22bolt-fixed-height-list-row%20scroll-hidden%20absolute%20focused%22%3E%3CDIV%20class%3D%22line-row%20flex-row%20flex-grow%22%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CSPAN%20class%3D%22line-area%20flex-center%20flex-row%20flex-grow%20justify-start%22%3E%3CSPAN%20class%3D%22content%22%3E%3CSPAN%20class%3D%22pl-plain%22%3E%3CSPAN%3Efatal%3A%20Authentication%20failed%20for%20'%3CA%20href%3D%22https%3A%2F%2Fdev.azure.com%2Fproject%2F_git%2Fmyrepo%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fdev.azure.com%2Fproject%2F_git%2Fmyrepo%2F%3C%2FA%3E'%20%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22bolt-fixed-height-list-row%20scroll-hidden%20absolute%22%3E%3CDIV%20class%3D%22line-row%20flex-row%20flex-grow%22%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CSPAN%20class%3D%22line-area%20flex-center%20flex-row%20flex-grow%20justify-start%20highlight%22%3E%3CSPAN%20class%3D%22content%22%3E%3CSPAN%20class%3D%22pl-error%22%3E%3CSPAN%3E%23%23%5Berror%5DPowerShell%20exited%20with%20code%20'1'.%20%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1085081%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20DevOps%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Egithub%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1532048%22%20slang%3D%22en-US%22%3ERe%3A%20powershell%20to%20run%20git%20commit%20using%20PAT%20in%20Azure%20github%20pipelines%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1532048%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F412611%22%20target%3D%22_blank%22%3E%40SBVRaja%3C%2FA%3E%26nbsp%3B%20hey%2C%20did%20you%20get%20a%20chance%20to%20solve%20and%20update%20it%20with%20solution%2C%3CBR%20%2F%3EI%20am%20trying%20to%20push%20the%20code%20into%20bitbucket%20using%20devops%20pipelines%20using%20PAT%20token.%20can%20you%20please%20help%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1532447%22%20slang%3D%22en-US%22%3ERe%3A%20powershell%20to%20run%20git%20commit%20using%20PAT%20in%20Azure%20github%20pipelines%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1532447%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F412611%22%20target%3D%22_blank%22%3E%40SBVRaja%3C%2FA%3E%2C%20Please%20try%20to%20run%20it%20locally%20and%20check%20if%20it%20is%20working.%20Make%20sure%20to%20that%20you%20have%20created%20a%20service%20connection%20in%20Azure%20DevOps.%20And%20the%20other%20possibility%20is%20that%20it%20could%20be%20due%20to%20the%20url%2C%20you%20can%20refer%20the%20documentation%20for%20git%20push%20%3CA%20href%3D%22https%3A%2F%2Fgit-scm.com%2Fdocs%2Fgit-push%23URLS%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I created a Personal Access Token for my repository and named it as 'PAT', created a variable called 'PATSecret' in my pipeline for the secret of PAT. Passing PATSecret as an input variable for $Cred. But when trying to use this secret to run commit command, authentication is failing.

 

param(
[string]$Cred
)
   git --version
   git add .
   Write-Host "setup author info"
   git config user.email you@you.com
   git config user.name "your name"
   Write-Host "git commit with message"
   git commit -m "Test Commit from Azure DevOps"
 
   git push -u https://PAT:$($cred)@dev.azure.com/project/_git/myrepo HEAD
 
 
 
Error message:
fatal: Authentication failed for 'https://dev.azure.com/project/_git/myrepo/'
##[error]PowerShell exited with code '1'.
4 Replies

@SBVRaja  hey, did you get a chance to solve and update it with solution,
I am trying to push the code into bitbucket using devops pipelines using PAT token. can you please help

Hello @SBVRaja, Please try to run it locally and check if it is working. Make sure to that you have created a service connection in Azure DevOps. And the other possibility is that it could be due to the url, you can refer the documentation for git push here.

If you are still looking for a solution, here it is. If you found any alternate solution please feel free to post.

#To identify branch name in which the build pipeline is running
if($env:SYSTEM_PULLREQUEST_SOURCEBRANCH)
{
"This is a PR build"
$branch = ("$env:SYSTEM_PULLREQUEST_SOURCEBRANCH").replace("refs/heads/","")
Write-Host "Branch name is: " $branch
}
elseif($env:BUILD_SOURCEBRANCH)
{
"This is a Non-PR build pipeline run"
$branch = ("$env:BUILD_SOURCEBRANCH").replace("refs/heads/","")
}
Else
{
Exit
}

git checkout $branch
git pull

<#Your code & changes to commig#>

git add .
git config --global user.email "Any email id"
git config --global user.name "Any user name"
git commit -am "commit message [ci skip]" #To skip the automatic trigger of the build use [ci skip]
git push --set-upstream origin $branch


Note: You need to enable the option "Allow scripts to access the OAuth token" on the 'Agent job' of the pipeline under Additional Options

best response confirmed by SBVRaja (Occasional Contributor)
Solution
Solution:

If you are still looking for a solution, here it is. If you found any alternate solution please feel free to post.

#To identify branch name in which the build pipeline is running
if($env:SYSTEM_PULLREQUEST_SOURCEBRANCH)
{
"This is a PR build"
$branch = ("$env:SYSTEM_PULLREQUEST_SOURCEBRANCH").replace("refs/heads/","")
Write-Host "Branch name is: " $branch
}
elseif($env:BUILD_SOURCEBRANCH)
{
"This is a Non-PR build pipeline run"
$branch = ("$env:BUILD_SOURCEBRANCH").replace("refs/heads/","")
}
Else
{
Exit
}

git checkout $branch
git pull

<#Your code & changes to commig#>

git add .
git config --global user.email "Any email id"
git config --global user.name "Any user name"
git commit -am "commit message [ci skip]" #To skip the automatic trigger of the build use [ci skip]
git push --set-upstream origin $branch


Note: You need to enable the option "Allow scripts to access the OAuth token" on the 'Agent job' of the pipeline under Additional Options