0
votes

We have multiple release branches in our product (currently this is unavoidable). For this question, suppose we have two:

  1. master
  2. release/r-858

Both have classic CI builds. Now I want to replace them with YAML builds. Our requirement is simple - have two distinct build definitions pointing to a YAML script - one for master, one for release/r-858

At the beginning I thought it is a trivial exercise:

  1. Create YAML build script in master. Set the CI trigger to master.
  2. Cherry-pick (never mind why not merge) to release/r-858 - set the CI trigger to release/r-858.

Not ideal, because the two scripts only differ in their CI trigger. But "I am learning, it is good enough for now" saying me to myself.

However, this simple scheme does not work! The build I created for release/r-858 is triggered on changes in master!

I double check every setting I know about builds - all look correct.

Please, observe:

The master build

enter image description here

enter image description here

enter image description here

enter image description here

The release/r-858 build

enter image description here

Uh oh, look at that. It shows the YAML on the master branch! Well, maybe it is an innocent presentation bug? Let us check the branch I am supposed to build from:

enter image description here

Yup, the file is different - I am playing with the trigger trying to solve the very same problem this question is about. The original code had release/r-858 instead of $(Build.SourceBranch) as the CI trigger, but since it did not help I started playing with all kinds of trigger values.

To remove any doubt, here is the proof the branch corresponds to release/r-858:

C:\xyz\58 [arch/shelve/798914 ≡ +0 ~17 -0 !]> git lg -2
cfdb6a9a86a |  (HEAD -> arch/shelve/798914, origin/arch/shelve/798914) Rename azure-pipelines-ci-58.yml to azure-pipelines-ci.yml and sync it with the master version (68 seconds ago) [Kharitonov, Mark] (2020-08-14 09:09:46 -0400)
a931e3bd96b |  (origin/release/r-858, release/r-858) Merged PR 90230: 793282 Work Assignments Merge (28 minutes ago) [Mihailichenco, Serghei] (2020-08-14 12:02:20 -0400)
C:\xyz\58 [arch/shelve/798914 ≡ +0 ~17 -0 !]>

Anyway, more build properties:

enter image description here

enter image description here

enter image description here

The problem

So a developer pushed some code to master and now the release/r-858 build is running:

enter image description here

enter image description here

Why is this? One of our guys asked a similar question in the Microsoft Developer Community forum, but that thread does not make sense to me.

What am I doing wrong here?

Edit 1

Imagine a big enterprise monolithic application. It is deployed in production at version 858. At the same time, developers work on the next version and also hot fixes and service packs for the version already deployed in prod.

A change can be made only in master or only in release/r-858 or in both (not at the same time, though). Many teams are working at the same time on many different aspects of the application and hence QA has many pods where the application is deployed. As I have mentioned above - about 150 pods for the bleeding edge (master) and about the same amount for the already released code, because there is active work to test hot fixes and service packs.

I appreciate this arrangement is not ideal. It is such not because we love it, but because one has to deal with decade old decisions. We are working to change it, but it takes time.

Anyway, the current process is to have 2 build definitions (in reality there are more for different reasons). So far we used classic CI builds, now we want to migrate to YAML (which we already use for micro services, but not the monolith).

Now I understand that we can have different release pipelines based off the same build definition, but different branch filters.

And maybe we will. But I do not understand why it is wrong to have different build definitions here, given that each branch is a long living release branch.

Edit 2

You can ignore $(Build.SourceBranch) and imaging release/r-858 instead. The net result is exactly the same. In the scenario I bring above code is committed to master, not release/r-858.

Edit 3

It is very confusing. Suppose I am creating a new YAML build. The dialog says "select YAML in any branch", but they point is that once selected this branch becomes the default branch of the build. That is the branch we can see here:

enter image description here

If I have a single YAML file in the master branch, the build with the default branch release/r-858 cannot even use it, unless it is merged to release/r-858. I tried it - I:

  1. created a new YAML build
  2. selected the YAML file from the master branch
  3. ran and right away cancelled the build
  4. then went to edit the build and changes the branch of the build from master to release/r-858 - it allowed me to save the build, even if the YAML does not exist in that branch

But then when I tried to run the build again I got this:

enter image description here

An error occurred while loading the YAML build pipeline. File /Build/azure-pipelines-ci.yml not found in repository bla-bla-bla branch refs/heads/release/r-858 version 5893f559292e56cf6db48687fd910bd2916e3cef.

And indeed, looking at the raw build definition, the process section contains the YAML file path, but not the branch:

    "process": {
        "yamlFilename": "Build/azure-pipelines-ci.yml",
        "type": 2,
        "resources": {},
        "target": null
    },

The branch only appears in the repository section of the definition:

    "repository": {
        "defaultBranch": "refs/heads/release/r-858",
        ...
    },

It is clear to me that a single build definition can be used to CI build many branches. But this model I need to implement is build definition per release branch. I cannot have a single build definition for the following reasons:

  1. Different release branches have different agent pools, because of the different development intensity. Remember, this is on on-prem Azure DevOps Server with self hosted agents. Can we express this requirement with a single build definition?
  2. Different build variable values which we want to control without sending a Pull Request to YAML file repository. How do you do it with a single build definition? For example, one of the variables controls the version Major.Minor. They are different in each release branch.

So, I do not see any way to avoid multiple build definitions in our situation. The root cause for this are the release branches, but we cannot throw them away in the near future.

So, we have 2 build definitions. That forces us to have 2 YAML - one per branch, because a build definition with the default branch of release/r-858 expects to find YAML in that branch, otherwise we cannot trigger the build manually. Which is a must, even if the build has a CI trigger.

So, 2 build definitions, 2 YAMLs (one per branch). So far my hands were forced. But now I am told that the release branch build would be triggered by the master YAML just because the release branch build is linked to the same YAML file name ignoring the default branch of the build!

Because this is what happens - a commit is checked in to master and the release branch build is invoked in addition to the master branch build! Both build definitions build exactly the same branch (master) using the master YAML script. But because the release branch build has different set of variables the end result is plain wrong.

This is not reasonable. I am going to create a dummy repo to reproduce it cleanly and post here.

Edit 4

As promised - a trivial reproduction. Given:

  1. master branch build test-master-CI
  2. release branch build test-r58-CI

Since having two build definitions necessarily means two YAMLs (one per branch), here they are:

C:\xyz\DevOps\Test [master ≡]> cat .\azure-pipelines.yml
trigger:
  branches:
    include:
      - master

name: $(BuildVersionPrefix).$(DayOfYear)$(Date:HH)

steps:
  - script: echo master
C:\xyz\DevOps\Test [master ≡]> git co release/r-858
Switched to branch 'release/r-858'
Your branch is up to date with 'origin/release/r-858'.
C:\xyz\DevOps\Test [release/r-858 ≡]> cat .\azure-pipelines.yml
trigger:
  branches:
    include:
      - release/r-858

name: $(BuildVersionPrefix).$(DayOfYear)$(Date:HH)

steps:
  - script: echo release/r-858
C:\Dayforce\DevOps\Test [release/r-858 ≡]>

Where BuildVersionPrefix = 59.0 for master and 58.3 for release/r-858

When I trigger each build manually I get this:

enter image description here

Now I commit a change to master. Lo and behold - both builds are triggered:

enter image description here

In both cases the YAML from the master branch is used. BUT the release branch defines BuildVersionPrefix = 58.3 and so the master build executed by the release branch build definition has bogus version.

Is this really how the feature is supposed to work? That makes the CI YAML trigger useless for my scenario. Thank you Matt for helping me to realize that.

2
Is there a reasoning to have two different build definitions? If the only difference is in the YAML between them, isn't that covered by just the version in your branch. So why not just have the trigger defined for both and just let the version of the YAML drive what is executed. Also ... Is the branch you are building $(Build.SourceBranch) in the include? Seems like if that is replaced by the variable of course it would build on master because it would set that to include master?Matt
@Matt is correct. You only need one YAML definition and one build definition using that YAML with appropriate triggers defined. When the build triggers, the triggering branch will be built.Daniel Mann
Guys, I need 2 build definitions, not one. Do you agree that there may be valid reasons to have different build definitions for different branches? Even if their build process is the same, their release pipelines may be different. If you disagree and think there is no legit scenario for two build definitions - I will open another question and you will explain to me why this is so. But, if you agree that we may have legit reasons, then back to my question - how come a change in master triggers the build defined for release/r-858 ?mark
Release pipelines can be different and you can define branch filters on them also to selectively pick. We can ignore the point, but kind of feels like you are missing the whole point of having YAML pipelines defined if you are just going to define multiple pipelines for each iteration. But if that's what you want, I won't argue the point.Matt
Can you answer this question though .. Is the branch you are building $(Build.SourceBranch) in the include?Matt

2 Answers

3
votes

I think I get where the confusion comes from. When you are configuring the pipeline, you are specifying the branch (notice the description says the file in any branch) and the file name.

enter image description here

What you are doing is just duplicating the monitoring though. If you were to really inspect it, I think you will see that when you push to release branch, it isn't trigger the master YAML pipeline ... it is just triggering the release YAML steps a second time. That is because the pipeline is just monitoring changes to the repo and responding based on the YAML configuration. In this case, you pushed to release and it evaluated that there was a YAML that matched that trigger (the release branch's copy) and triggered for both build definitions.

I verified this on a mocked-up pipeline. I had selected different branches on the creation, but the only thing that really impacts I believe is the default branch it would use for scheduled builds. I created a simple echo statement in both of these it was using the release branches YAML configuration.

enter image description here

I think if you really want to achieve the desired results you are expecting, you will want to use the override triggers that you define on the definition instead of relying on what is in the YAML trigger.

enter image description here

0
votes

I had the same issue and Matt helped me solve this.

I'm only writing this as the only way to get this working for me was to create a build YAML file on one branch (with the correct configuration). Then create the other YAML file on another branch. And then create the pipelines in the new shiny YAML editor within Devops.

The key is, when in the "Configure" section of a new pipeline, select: "Existing Azure Pipelines YAML file" which allows you to select a branch and a YAML file within that branch.

enter image description here

This allowed me to have the SystemOne branch build and test the system one site and the SystemTwo branch build and test the system two site.

I also added triggers inside the SystemOne.yml using a wild card. EG

trigger:
batch: true
branches: 
  include:
    - SystemOne/*

And the same for the SystemTwo.yml.