Bohdan Myslyvchuk
Bohdan Myslyvchuk

Reputation: 1817

Azure Pipelines for mono-repo with multiple projects to build only updated one

I'm trying to build CI/CD for multiple java projects that are contained in one repo with Azure pipelines.

For this purpose was requirement to create one pipeline which will build every project with Maven@3 task. The problem is that when we add new project I want that only this one project was built and not every previous project. My file looks like :

    resources:
- repo: self
  clean: true
  lfs: true

trigger:
  branches:
    include:
      - feature/*

variables:
  - group: vars

stages:
- stage: Build
  pool:
    vmImage: 'image' 
  jobs:
  - job: Build
    strategy:
      maxParallel: 4
    steps:
    - task: replacetokens@3
      inputs:
        targetFiles: 'settings.xml'
    - task: Maven@3
      inputs:
        jdkVersionOption: 1.8
        mavenPomFile: 'project1/pom.xml'
        options: '-s $(Build.SourcesDirectory)\settings.xml'
        goals: 'clean package'
    - task: Maven@3
      inputs:
        jdkVersionOption: 1.8
        mavenPomFile: 'project2/pom.xml'
        options: '-s $(Build.SourcesDirectory)\settings.xml'
        goals: 'clean package'

For example project1 is alraedy in develop, so I don't want to do mvn package for it, but only for project2, which is this not merged and has feature branch.

For now this yml won't work because project1 and project2 are in the same repo and this two task will run, which I wan t to avoid.

Is there some way to say "onChange" ???

Upvotes: 10

Views: 6567

Answers (1)

Repcak
Repcak

Reputation: 1006

Unfortunately, this is not possible out of the box which is a shame, to be honest.

The easiest way to handle this would be to create two separate builds. One build for each project you want to build that is located in the same repo and use the path filter like this:

trigger:
  branches:
    include:
    - master
    - releases/*
  paths:
    include:
    - docs/*
    exclude:
    - docs/README.md

Use step templates and define a single build task or job that you can then use across multiple builds so it is easier to maintain if multiple projects have the same build steps.

Link to the documentation: https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema%2Cparameter-schema#step-templates

There are however guides on the internet where people did some Powershell scripting to achieve the mono repo single build solution.

One example would be this:

https://dev.to/nikolicbojan/azure-devops-yaml-build-for-mono-repository-with-multiple-projects-146g

Upvotes: 13

Related Questions