Search code examples
azure-devopsmicroservices

For-Each an Object in Azure Devops pipeline?


I starting to write an application in microservices and want to have a build step to push the image from my pipeline. For this at the moment I have 3 services to push:

  - stage: build_and_publish_containers
    displayName: 'Docker (:Dev Push)'
    jobs:
      - template: "docker/publish.yaml"
        parameters:
          appName: Authorization_Service
          projectPath: "Services/AuthorizationService"
          imageName: authorization-service
          imageTag: ${{variables.imageTag}}
          
      - template: "docker/publish.yaml"
        parameters:
          appName: Registration_Service
          projectPath: "Services/RegistrationService"
          imageName: registration-service
          imageTag: ${{variables.imageTag}}
          
      - template: "docker/publish.yaml"
        parameters:
          appName: Tenant_Service
          projectPath: "Services/TenantService"
          imageName: tenant-service
          imageTag: ${{variables.imageTag}}

Even with only this 3 services (and I want to have much more) I have a lot of duplicated code here I want to reduce. I tried it with an array and an each-function but I have several information here (name / path / imagename) and that could grow.

Is there a better way?

If that would be a programming language I would have an array of a data model, is that something that is possible in azure devops?

Or maybe could each information saved in a json file (so 3 files at the moment and growing) and azure could get all files and informations out of this?


Solution

  • you could check the example below to define your complex object nested loops in Azure pipelines. By the way, you could also look into the github doc for more reference.

    parameters:
    - name: environmentObjects
      type: object
      default: 
      - environmentName: 'dev'
        result: ['123']
      - environmentName: 'uat'
        result: ['223', '323']
    
    pool:
      vmimage: ubuntu-latest
    steps:
    - ${{ each environmentObject  in parameters.environmentObjects }}: 
      - ${{ each result  in environmentObject.result }}:
        - script: echo ${{ result  }}