admin管理员组

文章数量:1126345

I am trying to iterate over the stages array, however I get the following error when running the pipeline:

(Line: 36, Col: 15): Unable to convert from Array to String. Value: Array

parameters:
  - name: stages
    type: object
    default: [0,1,100]
- stage: Validate_Stages
  jobs:
  - job: Validate_Stages_Job
    steps:
    - script: |
        echo "Validating stages"
        for stage in "${{ parameters.stages }}"; do
          if [ $stage -lt 0 ] || [ $stage -gt 100 ]; then
            echo "Invalid stage value $stage. Stage values must be between 0 and 100"
            exit 1
          fi
        done
- ${{ each stage in parameters.stages }}:
  - stage: Stage_${{ stage }}
    jobs:
    .
    .
    .

I am trying to iterate over the stages array, however I get the following error when running the pipeline:

(Line: 36, Col: 15): Unable to convert from Array to String. Value: Array

parameters:
  - name: stages
    type: object
    default: [0,1,100]
- stage: Validate_Stages
  jobs:
  - job: Validate_Stages_Job
    steps:
    - script: |
        echo "Validating stages"
        for stage in "${{ parameters.stages }}"; do
          if [ $stage -lt 0 ] || [ $stage -gt 100 ]; then
            echo "Invalid stage value $stage. Stage values must be between 0 and 100"
            exit 1
          fi
        done
- ${{ each stage in parameters.stages }}:
  - stage: Stage_${{ stage }}
    jobs:
    .
    .
    .
Share Improve this question edited 2 days ago JS noob asked Jan 8 at 23:50 JS noobJS noob 4876 silver badges20 bronze badges 4
  • If all you want to do is pass in the value [0,1,100] verbatim to a bash script, it should be defined as a string. You're not doing anything with it to iterate over it in the pipeline itself. As it stands, you're trying to take an object and treat it as a string, which Azure Pipelines can't do. Remember, an object might be a much more complex YAML object that doesn't really map to a string in any meaningful way. – Daniel Mann Commented Jan 9 at 5:11
  • So if I understand correctly, you want to validate the number of stages to run? I.e. you want to prevent someone from running more than 100 stages? – Rui Jarimba Commented Jan 9 at 9:00
  • I am also using that array to generate stages. I want to validate that the numbers in that array are from 0 to 100 in ascending order and no repeats. The reason I had to use that array is for generating the stages. So I want to validate in the stage before. – JS noob Commented 2 days ago
  • Regarding the "no repeats" part - from the moment you're using ${{ each stage in parameters.stages }} to generate stage names the pipeline will fail if there are any repeated elements in the array (e.g. 55) because all stages must have distinct names. – Rui Jarimba Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 0

If you need a string from your array, you can try join: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#join

as an example...

parameters:
- name: stages
  type: object
  default: [0,1,100]

variables:
  myvar.A: ${{ join(' ',parameters.stages) }}

stages:
  - stage: Validate_Stages
    jobs:
    - job: Validate_Stages_Job
      steps:
      - script: |
          stages=($(myvar.A))
          echo "Validating stages $(myvar.A)"
          for stage in "${stages[@]}"; do
            if [ $stage -lt 0 ] || [ $stage -gt 100 ]; then
              echo "Invalid stage value $stage. Stage values must be between 0 and 100"
              exit 1
            fi
          done

本文标签: