2
votes

I'm new to Azure DevOps and am looking to create the correct pipeline/release structures for my projects. I'm struggling to get to the point of how I'd build my code passing in the correct value for my build configs.

I have created my repo and branches:

main (main branch for released/to be released code)

feature/add_new_customer (feature branches per piece of work)

uat (a branch merging in multiple features commits specifically for testing prior to releases)

I created a build pipeline and this has created me an "azure-pipelines.yaml" file that I have used to build and publish the build files. This is triggered from my branches:

trigger:
- main
- feature/*
- uat

All good up to now. But this was by specifying the build config statically/hardcoded in yaml

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  **buildConfiguration: 'Release'**

I have now added a variable to my pipeline, BuildConfig, and set to "UAT" so I can reference in my yaml like so:

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  **buildConfiguration: '$(BuildConfig)'**

and this lets me set the build config "dynamically", but if I create another pipeline, say to build my release build from the main branch this creates an azure-pipelines-1.yaml which must be wrong as I'd then have to duplicate these files just to change the trigger branch?

Is there a "proper" way to create builds for different environments based on the branch I am checking in? I've seen the Environments but they just seem to offer me VM's or Kubernetes? I'm simply looking to build a .net framework legacy web form application so don't need anything fancy.

I've not started on the deployment process yet!!! :D

1

1 Answers

2
votes

Sure, you can have a single pipeline that changes its behavior based upon variables within the release pipelines themselves. You'll want to do this at the time you run the build, instead of at the root variable level.

There are two ways you can accomplish this:

  • The condition: parameter within the build task
  • YAML If Statements that selectively set the variables.

Either would work in your scenario, I believe using an if statement would be best in your case. Here's what it would look like in a simple example:

name: Stackoverflow-Example-Variables
trigger:
    - main
    - feature/*
    - uat
stages:
    - stage: your_build_stage
      variables:
        - name: solution
          value: '**/*.sln'
        - name: buildPlatform
          value: 'Any CPU'
        - name: buildConfiguration
          ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
            value: 'Release'
          ${{ if ne(variables['Build.SourceBranch'], 'refs/heads/main') }}:
            value: 'UAT'
      displayName: "Build Solution"
      jobs:
        - job: output_message_job
          displayName: "Output Message Job"
          pool:
            vmImage: "ubuntu-latest"
          steps:
            - powershell: |
                Write-Host ${{ variables.buildConfiguration }}
                Write-Host $(Build.SourceBranch)