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...
- Aug 04, 2021Solution:
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
SanthoshSreshta
Jul 19, 2020Copper 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
SBVRaja
Aug 04, 2021Copper Contributor
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
#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