divyam matia
divyam matia

Reputation: 21

Azure DevOps checkout only triggered repository

I was trying to check out only the repository that triggers the pipeline.

resources:
  repositories:
  - repository: repo2
    type: git
    name: branching/repo2
    ref: dev
    trigger:
    - dev
  
  - repository: repo1
    type: git
    name: branching/repo1
    ref: main
    trigger:
    - main
  
pool:
  vmImage: ubuntu-latest


steps:
- script: echo "$(Build.Repository.Name)"
- ${{ if in(variables['Build.Repository.Name'], 'repo1') }}:
  - checkout: repo1
- ${{ if in(variables['Build.Repository.Name'], 'repo2') }}:
  - checkout: repo2

But each time, this only checkout the source repository.

When the pipeline is triggered from repo1, I tried to checkout repo1, and when it is triggered from repo2, I tried to checkout repo2.

I don't want to keep changing the name of the checkout repository in the pipeline file. Is there another way to have the checkout task choose the triggered repository automatically?

Upvotes: 1

Views: 1199

Answers (2)

Ytsen de Boer
Ytsen de Boer

Reputation: 3107

The template expression

${{ if in(variables['Build.Repository.Name'], 'repo1') }}

is evaluated right when the pipeline starts. At that moment the variable Build.Repository.Name is unfortunately not yet set to the name of the triggering repo. So in your case it is always repo1 that is checked out.

However, at runtime the variable Build.Repository.Name is finally properly set, which is why your script step prints the proper value.

The following is the best I could come up with, but it is not perfect (see note below). It uses the proper runtime value of Build.Repository.Name and propagates it via the logger into the condition keyword of the next jobs.

The following then does what you request, i.e. it only checks out the triggering repo (if it is listed in parameters):

pool:
  name: SelfHostedForPipelineJobs
  vmImage: 'ubuntu-latest'


parameters:
  - name: repo_names
    type: object
    default:
      - 'repo-1'
      - 'repo-2'
      - 'repo-3'

resources:
  repositories:

  - repository: id-repo-1
    type: git
    name: repo-1
    ref: $(Build.SourceBranch)     
    trigger:
      branches:
        include:
        - main
        - test
  - repository: id-repo-2
    type: git
    name: repo-2
    ref: main        
    trigger:
      branches:
        include:
        - main
  - repository: id-repo-3
    type: git
    name: repo-3
    ref: $(Build.SourceBranch)
    trigger:
      branches:
        include:
        - main
        - test

jobs:
- job: A
  steps:
  - checkout: none
  - script: |
      echo '##vso[task.setvariable variable=triggering_repo;isOutput=true]$(build.repository.name)'
    name: printvar

- ${{ each repo_name in parameters.repo_names }}:
  - job:
    dependsOn: A
    condition: eq(dependencies.A.outputs['printvar.triggering_repo'], '${{ repo_name }}')
    steps:
    - checkout: id-${{ repo_name }}
      displayName: Step to checkout triggering repo ${{ repo_name }}
    displayName: Job to checkout triggering repo ${{ repo_name }}

Note: There is an issue with this when the branch $(Build.SourceBranch) does not exist in all repos. For my particular use case this is fine, but I'm very open to know how that could be fixed or worked-around.

Upvotes: 0

promicro
promicro

Reputation: 1646

Checking out multiple repos involve different ways of calling checkout:

  1. Check out the triggering repo with: checkout: self
  2. Check out the other repos with: checkout: <reponame>

More info and options using checkout see: https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops#specify-multiple-repositories

Edit

Assuming your trigger repo is repo1 or repo2, your YAML example would look like:

resources:
  repositories:
  - repository: repo2
    type: git
    name: branching/repo2
    ref: dev
    trigger:
    - dev
  
  - repository: repo1
    type: git
    name: branching/repo1
    ref: main
    trigger:
    - main
  
pool:
  vmImage: ubuntu-latest


steps:
- script: echo "Triggered repo: $(Build.Repository.Name)"
- checkout: self
- ${{ if in(variables['Build.Repository.Name'], 'repo1') }}:
  - checkout: repo2
- ${{ if in(variables['Build.Repository.Name'], 'repo2') }}:
  - checkout: repo1

Upvotes: 0

Related Questions