-1

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

Penguen
  • 16,836
  • 42
  • 130
  • 205

1 Answers1

0

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'
Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • Hi Kevin I updated my question with your comments Can you take a look one more time ? – Penguen Sep 12 '22 at 08:28
  • @Penguen Refer to my update. The Dotnet CLI task needs to set at the same level as PowerShell task. Then it can get the value of location. :) Please check it – Kevin Lu-MSFT Sep 12 '22 at 08:33
  • Thank you for your solution I am producing artifact really perfect. Last question Can we deploy these 6 locations as webjob(.zip) ? if you tell me , It makes me really happy. – Penguen Sep 12 '22 at 09:26
  • Hi again, Actually I am trying to develop 6 webjobs in one App service but I have to add locations as suffix -berlin hamburg at the end of the dll also. this is my desire : https://stackoverflow.com/questions/69259531/deploy-multiple-azure-webjobs-to-a-single-azure-appservice-in-yaml-pipeline – Penguen Sep 12 '22 at 09:41