skip to Main Content

The pipeline seem to work fine as of now. It’s dynamic in the sense that it will trigger for all release branches, for all repos, which is what we want.

What we would really want is the ability to use a fallback ref in case the branch that triggers the build, Build.SourceBranch, is not available for one or more of the repos.

trigger:
- none

pool:
  name: <POOL_NAME>
  demands:
  - agent.name -equals  <AGENT_NAME>
# Overrides the value for Build.BuildNumber, which is used to name the artifact (ZIP file) that is produced

name: '$(Date:yyyyMMdd)T$(Hours)$(Minutes)$(Seconds)'

resources:
  repositories:
  - repository: REPO-A
    type: git
    ref: $(Build.SourceBranch)
    name: <COMPANY>/REPO-A
    trigger:
      branches:
        include:
        - 'release/*'

  - repository: REPO-B
    type: git
    ref: $(Build.SourceBranch)
    name: <COMPANY>/REPO-B
    trigger:
      branches:
        include:
        - 'release/*'
          
stages:
- stage: 'BuildAndUploadArtifact'
  jobs:
  - job:
    workspace:
      clean: all
    steps:
    - checkout: self
    - checkout: REPO-A
    - checkout: REPO-B

    - task: CmdLine@2
      inputs:
        script: |
          echo "hello, world!"

Consider these two scenarios:

Scenario 1 – works with this pipeline

  • REPO-A and REPO-B both have a release/2024-1 branch and a
    release/2024-2 branch. A commit is made in the release/2024-1 branch
    in REPO-A. Both repos are checked out with the release/2024-1 branch.

Scenario 2 – does not work with this pipeline

  • REPO-A have a release/2024-1 branch. REPO-B only has a MASTER branch.
    A commit is made in the release/2024-1 branch in REPO-A. REPO-A is
    checked out with the release/2024-1 branch, but REPO-B doesn’t have
    that branch. Result: The pipeline won’t run.

We want to be able to specify a fallback ref for REPO-B, so that it could have fallen back on that branch. If that fallback ref would be specified with MASTER, then REPO-A would use the release/2024-1 branch and REPO-B would use the MASTER branch.

Is this possible to achieve today?

NOTE!: This pipeline is heavily simplified. In reality there are many more repos, and the repos are built together as one application in a later step.

2

Answers


  1. Yes, you can dynamically define the variable value based on the source branch, and then use the variable for the repo-B ref value. so that it could have fallen back on repo-B to checkout.

    Sample yaml below:

    trigger: none
    
    # dynamically set the branch value, please change according to your real requirement
    variables:
      ${{ if or( eq(variables['Build.SourceBranchName'], 'dev1'), contains(variables['Build.SourceBranch'], 'bugfix/') ) }}:
          branch: $(Build.SourceBranchName)
      ${{ else }}:
          branch: 'main'
    
    pool:
      VMimage: Ubuntu-latest
    
    resources:
      repositories: 
      - repository: repo-A
        name: 'wadetest1/test1'
        ref: $(Build.SourceBranch)
        type: git
        trigger:
          - bugfix/*
          - release/*
          - dev1
      - repository: repo-B
        name: 'wadetest1/test2'
        type: git
        ref: $(branch)              #<- use the dynamic variable here
        trigger:
          - dev1
    steps:
      - checkout: self
      - checkout: repo-A 
      - checkout: repo-B
    

    It will checkout fallback branch main on repo-B, as it doesn’t have release/test1 branch:

    enter image description here

    Login or Signup to reply.
  2. I don’t think there is anything "built-in" in for such a scenario. You could probably try checking if the branch exists with a custom script using something like

    git ls-remote -b --exit-code REPOURL $(Build.SourceBranch)

    Then checking if the exit code ($?) is 0 (found) or 2 (not found). And based on that set a variable with SourceBranch or your fallback and use that in the ref: part.

    But I’m unsure if this would actually work as you would need to check very closely when the pipeline resolves those things

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search