admin管理员组

文章数量:1278983

When using Azure Pipelines YAML pipelines, is it possible to directly use an existing Service Connection?

More specifically, the existing service connection to Bitbucket Cloud is used implicitly when creating pipelines for repos in Bitbucket. We would like to pull in a Bitbucket repo using git --mirror.... Is it possible to use the service connection to with git in such a way?

When using Azure Pipelines YAML pipelines, is it possible to directly use an existing Service Connection?

More specifically, the existing service connection to Bitbucket Cloud is used implicitly when creating pipelines for repos in Bitbucket. We would like to pull in a Bitbucket repo using git --mirror.... Is it possible to use the service connection to with git in such a way?

Share Improve this question asked Feb 23 at 23:14 GaTechThomasGaTechThomas 6,1136 gold badges54 silver badges80 bronze badges 5
  • Which was the authentication method of your Bitbucket cloud service connection, OAuth with BitbucketAzurePipelines app or Basic with a token? – Alvin Zhao - MSFT Commented Feb 24 at 2:08
  • OAuth, though I can use Basic if needed. – GaTechThomas Commented Feb 24 at 5:28
  • Hi @GaTechThomas, Have you got a chance to test my updated answer below to extract token for git clone? Thx for your time and sharing. – Alvin Zhao - MSFT Commented 2 days ago
  • @AlvinZhao-MSFT thank you very much for the response. I've been away on family emergency and will dig back in soon. – GaTechThomas Commented yesterday
  • Hi @GaTechThomas, I hope everything is okay with your family. Take all the time you need, and whenever you're ready, we can pick things back up. Wishing you and your loved ones the best. – Alvin Zhao - MSFT Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 1

Update

Based on my recent tests with persistCredentials: true for checkout step, I noticed that step would temporarily save auth token in the .git/config file during the agent job.

Therefore, I ran a script to extract and decode that token for authenticating git clone --mirror command. Here is the sample YAML pipeline for your reference.

trigger: none

pool: # Default
  vmImage: ubuntu-latest

steps:
- checkout: self
  persistCredentials: true
- powershell: |
    Write-Host "Build.Repository.Name - $(Build.Repository.Name)"
    Write-Host "Build.Repository.Provider - $(Build.Repository.Provider)"

    $configFilePath = "$(System.DefaultWorkingDirectory)/.git/config"
    $configFileContent = Get-Content -Path $configFilePath
    $encodedAuthToken = $configFileContent | Where-Object { $_ -match "extraheader = AUTHORIZATION: basic (.+)" }
    $encodedAuthToken = $encodedAuthToken -replace "extraheader = AUTHORIZATION: basic ", ""
    $encodedAuthToken = $encodedAuthToken.Trim()
    $decodedAuthToken = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($encodedAuthToken))
    Write-Output "decodedAuthToken - $decodedAuthToken"
    
    mkdir testFolder
    git clone --mirror https://[email protected]/$(Build.Repository.Name).git testFolder
    tree testFolder

The Bitbucket Cloud service connection my pipeline references is using grant authorization of BitbucketAzurePipelines.


As far as I tested to use persistCredentials: true property for the checkout step, I found that the git push command in a pipeline script could succeed to push commits during the build onto the remote Bitbucket source repo.

steps:
- checkout: self
  persistCredentials: true

- powershell: |
    mkdir testFolder
    git clone --mirror https://bitbucket./xxx/bitbucketrepo.git testFolder

    tree testFolder

However, per the git clone --mirror <repoURL> command, it requires to provide the token for authentication inside the URL in order to avoid prompting user interactive dialog. There is no way to extract token form the Bitbucket Cloud service connection, unlike the $(System.AccessToken) we can use to authenticate access to an Azure Repo. git clone --mirror https://$(System.AccessToken)@dev.azure/$(AzureDevOpsOrgName)/$(TheProjectName)/_git/$(AzureRepoName)

本文标签: bitbucket cloudDirectly use Azure Devops Service ConnectionStack Overflow