0

I'm attempting to create a Scheduled Azure Pipeline where I clone a self hosted BitBucket git repository using a Service Connection and mirror it to an existing Azure git repository.

A client keeps a a repository of code on their own BitBucket server. I'd like to set up a pipeline where I pull any changes from that repo on a scheduled interval into my own Azure repository so I can set up automated deployments.

I keep getting hung up on the Service Connection part of things. The Service Connection is setup as "Other Git" and contains all of the credentials I need to access the remote BitBucket server.

trigger: none

schedules:
- cron: "*/30 * * * *" # RUN EVERY 30 MINUTES
  displayName: Scheduled Build
  branches:
    include:
    - my-branch
  always: true # RUNS ALWAYS REGARDLESS OF CHANGES MADE

pool:
  name: Azure Pipelines

steps:
- task: AzureCLI@2
  name: setVariables
  displayName: Set Output Variables
  continueOnError: false
  inputs:
    azureSubscription: "Service Connection Name"
    scriptType: ps
    scriptLocation: inlineScript
    addSpnToEnvironment: true
    inlineScript: | 
      Write-Host "##vso[task.setvariable variable=username;isOutput=true]$($env:username)"
      Write-Host "##vso[task.setvariable variable=password;isOutput=true]$($env:password)"

- powershell: |
   # Use the variables from above to pull latest from
   # BitBucket then change the remote origin and push
   # everything to my Azure repo
  displayName: 'PowerShell Script'

When I run this I end up getting an error stating:

The pipeline is not valid. Job: setVariables input connectedServiceNameARM 
expects a service connection of type AzureRM but the proviced service connection is of type git.

How can I access variables from a git service connection in my YAML pipeline?

Chris Lees
  • 2,140
  • 3
  • 21
  • 41

1 Answers1

0

The AzureCLI task only accepts service connections of the Azure Resource Manager type. So the git connection you are using doesn't work.

According to your needs, you can check out the repo first. There is a Bitbucket Cloud Service connection for Bitbucket repositories. You can use it to check out multiple repositories in your pipeline if you keep the yaml files in the azure repo.

Here is the sample yaml and screenshot:

resources:
  repositories:
  - repository: MyBitbucketRepo
    type: bitbucket
    endpoint: MyBitbucketServiceConnection
    name: MyBitbucketOrgOrUser/MyBitbucketRepo

trigger: none


schedules:
- cron: "*/30 * * * *" # RUN EVERY 30 MINUTES
  displayName: Scheduled Build
  branches:
    include:
    - my-branch
  always: true # RUNS ALWAYS REGARDLESS OF CHANGES MADE

pool:
  name: Azure Pipelines

steps:
- checkout: MyBitbucketRepo

- powershell: |
   # Use the variables from above to pull latest from
   # BitBucket then change the remote origin and push
   # everything to my Azure repo
  displayName: 'PowerShell Script'

enter image description here

Miao Tian-MSFT
  • 571
  • 4
  • 5
  • My source code isn't hosted in a BitBucket cloud account. It's a self hosted BitBucket server on prem at the client's facility. This is the reason I need to use "Other Git" as my Service Connection. – Chris Lees Mar 04 '22 at 13:02
  • thank you for your reply. I don't have a self hosted BitBucket server on prem, so I can't test this for now. Maybe you can try to write the authentication information in secret variables and use the variables in the PowerShell Script. This avoids using the AzureCLI task. – Miao Tian-MSFT Mar 07 '22 at 02:01
  • 1
    Yeah, that's what I ended up doing. Stinks I had to put my auth variables in two places but I couldn't find a better solution. – Chris Lees Mar 10 '22 at 16:36
  • Glad to know that you have solved the issue. You could write your solution and accept it as an Answer, so it could help other community members who get the same issues. Thank you. – Miao Tian-MSFT Mar 11 '22 at 02:02