Forum Discussion
SBVRaja
Jan 01, 2020Copper Contributor
powershell to run git commit using PAT in Azure github pipelines
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'.
- 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
- SanthoshSreshtaCopper Contributor
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- SBVRajaCopper ContributorIf 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
- hkarthik_7Copper Contributor
- SBVRajaCopper ContributorSolution:
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