Forum Discussion

SBVRaja's avatar
SBVRaja
Copper Contributor
Jan 01, 2020

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...
  • SBVRaja's avatar
    Aug 04, 2021
    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

Resources