How can I use powershell creating different name webjob for different location?

Viewed 47

My question is specific but I guess simple who knows PowerShell more than I.

Let me explain my question :

  1. I need 6 different webjobs under 1 web service. my webjob will be the SonicMQ listener we developed can only listens to a single queue.

  2. My locations [istanbul,berlin, hamburg.....]

  3. I am trying to create 6 different webjobs with "-istanbul","-berlin","-hamburg" . . . suffix by using for loop in a task via Powershell.

below task should work like that : (psode code)

var listoflocations = [istanbul,berlin.hamburg, . . ]

foreach location in  listoflocations 

  powershell -create mywebjob-location 

my yaml file (small part): how can I use powershell below ?

For this purpose I spend my time with some changes Thanks to your advice. When I run below yaml I am getting this error:

enter image description here

- stage: Deploy_Test

  displayName: 'Deploy test'

  dependsOn: Test

  condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))

  pool:

    name: 'bmersgtsteuw-app-eurotracs-pool'

  jobs:

  - deployment: yyyOrderShipmentJob

    displayName: 'Deploy Order Shipment Job'

    environment: 'xxx-CF-Test'

    strategy:

      runOnce:

        deploy:

          steps:

            - download: current

              artifact: drop

            - task: "HOW CAN I USE POWERSHELL HERE WITH FOREACH LOOP ?  [istanbul,berlin.hamburg, . . ]"

              inputs:

                ConnectionType: AzureRM

                azureSubscription: 'ARM-Con-xxx-yyy-TST-EUW-APP'

                appType: 'webApp'

                WebAppName: 'bmeapptsteuw-app-eurotracs'

                package: '$(Pipeline.Workspace)/drop/xxx.yyy.OrderShipmentJob-$(Build.BuildNumber).zip'

                removeAdditionalFilesFlag: true

My FINAL pipeline yaml :

trigger:
- main

pool:
  vmImage: ubuntu-latest

parameters:
  - name: locations
    type: object
    default: [Berlin,Hamburg,Südwestfalen,Weser-Ems,Ostfalen,Westfalen]

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script'
- ${{ each location in parameters.locations }}:
             - task: PowerShell@2
               inputs:
                 targetType: 'inline'
                 script: 'Write-Host ${{ location  }}'
- script: |
    echo Add other tasks to build, test, and deploy your project.
    echo See https://aka.ms/yaml
  displayName: 'Run a multi-line script'

- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    publishWebProjects: false
    projects: '**/MyWebJob.csproj'
    arguments: '--configuration $(buildConfiguration) --output $(build.artifactStagingDirectory)\App_Data\jobs\continuous\MyWebJob-${{ location  }}'
    zipAfterPublish: false
    modifyOutputPath: false

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

MY DESIRE RESULT :

enter image description here

1 Answers

In Azure DevOps Pipeline, you can define all location values in Object type Parameters. And then you can loop the parameter to get each value one by one via parameter expression(- ${{ each location in parameters.locations }}:).

Here is an example:

parameters:
  - name: locations
    type: object
    default: [istanbul,berlin.hamburg,xx,xxx,xxxx]

jobs: 
  - deployment: test
    displayName: 'Deploy Order Shipment Job'
    environment: 'xxx-CF-Test'
    strategy:
      runOnce:
        deploy: 
          steps: 
           - ${{ each location in parameters.locations }}:
             - task: PowerShell@2
               inputs:
                 targetType: 'inline'
                 script: 'Write-Host ${{ location  }}'

For more detailed info, you can refer to this doc: Parameters

Update:

pool:
  vmImage: ubuntu-latest

parameters:
  - name: locations
    type: object
    default: [Berlin,Hamburg,Südwestfalen,Weser-Ems,Ostfalen,Westfalen]

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script'
- ${{ each location in parameters.locations }}:
             - task: PowerShell@2
               inputs:
                 targetType: 'inline'
                 script: 'Write-Host ${{ location  }}'

             - task: DotNetCoreCLI@2
               inputs:
                command: 'publish'
                publishWebProjects: false
                projects: '**/MyWebJob.csproj'
                arguments: '--configuration $(buildConfiguration) --output $(build.artifactStagingDirectory)\App_Data\jobs\continuous\MyWebJob-${{ location  }}'
                zipAfterPublish: false
                modifyOutputPath: false

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'
Related