azure-pipelines-yaml

How do I use 2 different branches in the same YAML file


We have a 2 YAML step for releasing updates and we want to merge down to 1 file

YAML 1
This is in the Development branch
The stages for this yaml are
build and run unit tests
deploy to test server
run automation regression suite against test server

Yaml 2
This is in the Master branch
The stages for this YAML are
build and deploy to live server
run automation smoke suite against live server

So is it possible to do this in 1 Yaml file despite the fact there are 2 branches in use here?

If yes, how please?


Solution

  • We have a 2 YAML step for releasing updates and we want to merge down to 1 file YAML 1 This is in the Development branch The stages for this yaml are build and run unit tests deploy to test server run automation regression suite against test server Yaml 2 This is in the Master branch The stages for this YAML are build and deploy to live server run automation smoke suite against live server So is it possible to do this in 1 Yaml file despite the fact there are 2 branches in use here?

    To find work around in your case I have tried the shared pipeline + env-specific YAML approach. A shared YAML (pipeline-shared.yml) that defines all the logic (build, deploy, test) Two minimal entry-point YAML files (pipeline-dev.yml and pipeline-prod.yml) that: Trigger on different branches Pass parameters to the shared YAML (e.g., environment: dev or prod) Are connected to two different pipelines in Azure DevOps

    azure-pipelines/

    This is useful in Shared, reusable pipeline logic, Clear separation between dev and prod pipelines, Branch-specific behaviour with no duplication

    First Create pipeline-shared.yml

    name: Shared CI/CD Workflow
    on:
      workflow_call:
        inputs:
          environment:
            required: true
            type: string
    
    jobs:
      build_and_test:
        runs-on: windows-latest
        steps:
          - name: Checkout Code
            uses: actions/checkout@v3
    
          - name: Setup .NET
            uses: actions/setup-dotnet@v3
            with:
              dotnet-version: '7.x.x' 
    
          - name: Restore Dependencies
            run: dotnet restore
    
          - name: Build
            run: dotnet build --configuration Release
    
          - name: Run Unit Tests
            run: dotnet test --no-build --configuration Release
    
      deploy:
        needs: build_and_test
        runs-on: windows-latest
        steps:
          - name: Conditional Deploy
            run: |
              if [ "${{ inputs.environment }}" == "dev" ]; then
                echo "Deploying to test server"
                # e.g., ./deploy-test.sh or Azure CLI
              else
                echo "Deploying to production server"
                # e.g., ./deploy-prod.sh or Azure CLI
              fi
    
      automation_tests:
        needs: deploy
        runs-on: windows-latest
        steps:
          - name: Conditional Automation Tests
            run: |
              if [ "${{ inputs.environment }}" == "dev" ]; then
                echo "Running regression suite"
                # e.g., dotnet test tests/RegressionTests.csproj
              else
                echo "Running smoke suite"
                # e.g., dotnet test tests/SmokeTests.csproj
              fi
    
    

    Create pipeline-dev.yml

    name: Dev CI/CD Pipeline
    
    on:
      push:
        branches:
          - development
    
    jobs:
      use-shared:
        uses: ./.github/workflows/shared-workflow.yml
        with:
          environment: dev
    
    

    Create pipeline-prod.yml

    name: Prod CI/CD Pipeline
    
    on:
      push:
        branches:
          - master
    
    jobs:
      use-shared:
        uses: ./.github/workflows/shared-workflow.yml
        with:
          environment: prod
    

    Also you can refer Documentation.