1

I am newer to using Azure Devops and creating Builds and Releases. I am trying to create a Release (this would be going to our production environment), so at this point I want to create a branch for that release. We are using a local build agent. If I try to do this by doing a git clone then other git commands to create that new branch and push it, it cannot finish in the Release process because on the local build agent it is asking for a username and password. Is there anyway to get around this? Is there a command or a process that I can trigger within the Release that will create this new branch without having to use the local build agent?

Nathan
  • 675
  • 2
  • 9
  • 20

1 Answers1

1

If you lack PAT authentication, You just need to add {PAT} after https://

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

I am here to provide another method.You can also use Refs - Update Refs api to create a branch:

POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=5.0

I test it in postman,and can successfully create a branch. enter image description here

You can add a powershell task to the release pipeline and write a script with this rest api.

Here is a sample:

$uri = 'https://dev.azure.com/{organization}/{pro}/_apis/git/repositories/{repoId}/refs?api-version=5.1';

[array]$requestList = @();
$requestObj = New-Object -TypeName psobject;
$requestObj | Add-Member -MemberType NoteProperty -Name "name" -Value 'refs/heads/feature2';
$requestObj | Add-Member -MemberType NoteProperty -Name "oldObjectId" -Value "0000000000000000000000000000000000000000";
$requestObj | Add-Member -MemberType NoteProperty -Name "newObjectId" -Value "c141b3f6aaec8967763de63a6e9faf6a96c5ea13";
$requestList += @($requestObj);


$body = ConvertTo-Json -InputObject @($requestList);
Write-Host $body;

$response = Invoke-RestMethod -Uri $uri -Headers @{   
 Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Method Post -Body $body -ContentType application/json;

Write-Host $response;
Hugh Lin
  • 17,829
  • 2
  • 21
  • 25