How to run a template(.yml) belonging to another repository from my current repository in azure devops?

Viewed 1381

With the below setup, when my pipeline corresponding to my repo runs, it runs the template (template.yml) file belonging to 'anotherRepo'. But when it checks out, it checks out my repo instead of 'anotherRepo'.

Is there any issue in my setup?

Looks like checkout:self does not have any impact and it does not work

My current Repo:

azurepipeline.yml file:

variables:
  acceptanceTestsRepoName: 'anotherRepo'

resources:
  repositories:  
  - repository: 'anotherRepo'
    name: ProjectName/anotherRepo
    type: git 
    ref: master

stages:
  - stage: acceptance_tests    
    displayName: 'Run Acceptance Tests in Dev'
    jobs:       
     - template: 'azure-pipelines-templates/template.yml@${{variables.acceptanceTestsRepoName}}'

Repo:anotherRepo

template.yml

jobs:
  - job: AcceptanceTest
    displayName: Run Acceptance Test in Dev
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - checkout: self
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '$(python.version)'
3 Answers

self always refers to a repo associated with the build pipeline. In your case, you need to checkout anotherRepo manually:

# Azure Repos Git repository in the same organization
- checkout: git://anotherRepo

This assumes that anotherRepo is in the same Azure DevOps organization. If it's not or stored somewhere else (GitHub, Bitbucket, etc...) you also need to add it as a resource to the pipeline defintion. See Check out multiple repositories in your pipeline for details.

According to the document about checkout: self represents the repo where the initial Azure Pipelines YAML file was found. enter image description here So your pipeline checkout the repo where your azurepipeline.yml file is located. If you want to checkout your anotherRepo, the checkout step in your template.yml should be - checkout: anotherRepo:

jobs:
  - job: AcceptanceTest
    displayName: Run Acceptance Test in Dev
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - checkout: anotherRepo
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '$(python.version)'

You can also use Inline syntax checkout to directly check out another repo in azure-pipelines.yml file:

stages:
  - stage: acceptance_tests    
    displayName: 'Run Acceptance Tests in Dev'
    jobs:       
    - job: checkout
      steps:
      - checkout: git://ProjectName/anotherRepo

Thanks all for your response expecially @beatcracker By replacing the checkout step from self , I was able to run successfully

No change to My Current Repo. Below change done to another repo

jobs:
  - job: AcceptanceTest
    displayName: Run Acceptance Test in Dev
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - checkout: git://ProjectName/anotherRepo
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '$(python.version)'
Related