1

I am trying to create an Azure Pipeline. But I am having a problem looping an Azure task in it. I have explained below through a simplified example the problem I am facing.

I hope to create multiple Azure web apps through this pipeline. Each webapp has a command passed to it using --startup-file and this command includes an argument. The number of apps to create and the argument to pass to each one, is provided as a runtime parameter when triggering the pipeline
E.g. If NUMBER=3 and ARGUMENTS=24,56,123
then each Azure web app --name and --startup-file should be as follows

--name myapp-1 --startup-file 'java -jar my.jar --arg 24   
--name myapp-2 --startup-file 'java -jar my.jar --arg 56   
--name myapp-3 --startup-file 'java -jar my.jar --arg 123   

The pipeline prompts the user for the number of web apps to create and a comma separated string of arguments to pass to them. An Azure pipeline step defined in main.yaml passes these two parameters to a template (createapps.yaml). Then based on this template the defined number of apps must be created.

main.yaml is as follows

parameters:
- name: NUMBER
  type: number
- name: ARGUMENTS
  type: string

steps:
- template: createapps.yaml
  parameters:
    NUMBER: ${{ parameters.NUMBER }}
    ARGUMENTS: ${{ parameters.ARGUMENTS }}

createapps.yaml is as follows

parameters:
- name: NUMBER
  type: number
- name: ARGUMENTS
  type: string

steps:
- task: AzureCLI@2
  displayName: Create Apps
    az webapp create \
      --name myapp-<Number> \
      --plan myplan \
      --resource-group myrg \
      --deployment-container-image-name myimage:latest \
      --startup-file 'java -jar my.jar --arg <Argument>'

How can I loop the task in createapps.yaml and assign <Number> and <Argument> in the Azure CLI command by iterating NUMBER and ARGUMENTS?

Nilushan Costa
  • 286
  • 2
  • 6
  • 24

1 Answers1

3

Split ARGUMENTS by "," into an array and use a for loop. Something like this:

- task: AzureCLI@2
  displayName: Create Apps
  inputs:
    azureSubscription: $(azureServiceConnection)
    scriptType: bash
    scriptLocation: inlineScript
    inlineScript: |
      arr=(${ARGUMENTS//,/ })
      for ((i=1; i<=${{ parameters.NUMBER }}; i++ ))
      do
        az webapp create \
          --name "myapp-$i" \
          --plan myplan \
          --resource-group myrg \
          --deployment-container-image-name myimage:latest \
          --startup-file "java -jar my.jar --arg ${arr[i-1]}"
      done
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I was trying to do this using `task` itself. Isn't it possible that way? And if I use `script` I would have to perform an `az login` (preferably with a service principal account), right? – Nilushan Costa Aug 14 '20 at 15:15
  • The `AzureCLI@2` tasks let you run a shell or batch script containing Azure CLI commands. You will still have to write the script yourself. It doesn't loop for you...You don't have to explicitly run the `az command` though since the authentication is taken care of by the `azureSubscription` service connection. – mm8 Aug 17 '20 at 13:07
  • I am confused with your last comment. You mentioned that "AzureCLI@2 tasks let you run a shell or batch script containing Azure CLI commands. You will still have to write the script yourself. It doesn't loop for you". Indeed I prefer to use a `task` and let it loop. However your answer shows a `script` and not a `task`. Furthermore, I tried running an `az` CLI command with `script`. It fails with `Please run 'az login' to setup account.` indicating that I need to explicitly login. Do I need to make any additional changes for authentication to be taken care of by `azureSubscription`? – Nilushan Costa Aug 18 '20 at 05:01
  • `AzureCLI@2` is a specialized script task. Besides the `azureSubscription` option, it also has a `scriptType` option that lets you specify the type of script to use, like for example Bash. I edited my answer to show you sort of what you need to do with the `AzureCLI@2` task. – mm8 Aug 18 '20 at 14:06
  • Now it works. I wasn't aware that the `AzureCLI@2` could execute normal bash commands. I assumed it was specialized task used only to execute `az` commands. Thanks for the clarification – Nilushan Costa Aug 19 '20 at 06:35