Answers: <p>As far as I can tell you have two issues here:</p>
  <ol>
  <li><p>The <code>environment</code> variable you are declaring will conflict with the pre-defined <code>environment</code> variable in Azure DevOps, see <a href="https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&amp;tabs=yaml#deployment-job-variables-devops-services" rel="nofollow noreferrer">Predefined variables</a>.</p>
  </li>
  <li><p>You are creating extra work for yourself by trying to re-invent the wheel, Azure DevOps already has <a href="https://learn.microsoft.com/en-us/azure/devops/pipelines/process/environments?view=azure-devops" rel="nofollow noreferrer">support for Environments</a>.</p>
  </li>
  </ol>
  <p>The most common way to handle multiple environments in Azure Pipelines is to use Environments that describe deployment targets (i.e. VMs, Kubernetes, etc.). You then create a build definition that describes:</p>
  <ul>
  <li><a href="https://learn.microsoft.com/en-us/azure/devops/pipelines/process/stages?view=azure-devops&amp;tabs=yaml" rel="nofollow noreferrer">Stages</a> of the build, test, deploy, etc.</li>
  <li><a href="https://learn.microsoft.com/en-us/azure/devops/pipelines/process/phases?view=azure-devops&amp;tabs=yaml" rel="nofollow noreferrer">Jobs</a> which are a collection of scripts or other resources that actually perform the work.</li>
  </ul>
  
Answers: CC BY-SA 4.0
Answers: 2024-08-16T00:59:01.243Z
Answers: Hey Richard, sorry to confuse you. Yes I am aware of the "environment" variable and this might be the issue but one of my stages is still getting the correct "Development" stage name. The other two stage names are blank. I am attempting to create dynamic stages based on the "environments" variable I have defined by delimiting by comma and then delimiting further by space. Unfortunately it is not working as intended for the other two stages...
Answers: Kevys
Answers: CC BY-SA 4.0
Answers: 2024-08-16T17:37:33.243Z
Answers: Azure DevOps already supports dynamic environments, if you still want to try and re-invent the wheel you might need to provide a more complete example including the variables. Bear in mind that `dev, test, prod` when split on the `,` would result in `'dev' , ' test' , ' prod'` (note the spaces at the start of the last two), if you then split those again based upon ` ` (space) then `"dev".split(' ') == ["dev"]` but `" test".split(' ') == ["", "test"]` which probably isn't what you want.
Answers: Richard Slater
Answers: CC BY-SA 4.0
Answers: 2024-08-14T22:32:09.697Z
Answers: 2024-08-14T22:32:09.697Z
Answers: Richard Slater
Body: <p>My yaml pipeline is written similar to the following structure below, however, only the &quot;Development&quot; stage is getting the correct stage name. The other two stage names are blank and I cannot figure out why Azure devops is not expanding the variable correctly</p>
  <pre><code>variables:
  - name: environments
    value: &gt;
      Development something-0,
      Staging something-1,
      Production something-2
  
  stages:
    - stage: Validate
      jobs:
  
  
    - ${{ each environment in split(variables.environments, ',') }}:
      - stage: ${{ split(environment, ' ')[0] }}
        dependsOn: Validate
        jobs:
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2024-08-13T17:53:58.107Z
Comments: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Comments: Community
Tags: <azure-devops><azure-pipelines><cicd><yaml>
Title: Specifying stage name as variable in azure devops yaml pipeline loop is blank for some?
UserDisplayName: Kevys
ThreadText: # Specifying stage name as variable in azure devops yaml pipeline loop is blank for some?
  
  (Asked by: username_0 on 2024-08-13)
  
  My yaml pipeline is written similar to the following structure below, however, only the "Development" stage is getting the correct stage name. The other two stage names are blank and I cannot figure out why Azure devops is not expanding the variable correctly
  
  ```
  variables:
  - name: environments
    value: >
      Development something-0,
      Staging something-1,
      Production something-2
  
  stages:
    - stage: Validate
      jobs:
  
    - ${{ each environment in split(variables.environments, ',') }}:
      - stage: ${{ split(environment, ' ')[0] }}
        dependsOn: Validate
        jobs:
  
  ```
  
  **Tags:** azure-devops, azure-pipelines, cicd, yaml
  
  > **Comments:**
  > **username_1**: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2024-08-14. Score: 0
  
  As far as I can tell you have two issues here:
  
  1. The `environment` variable you are declaring will conflict with the pre-defined `environment` variable in Azure DevOps, see [Predefined variables](https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#deployment-job-variables-devops-services).
  2. You are creating extra work for yourself by trying to re-invent the wheel, Azure DevOps already has [support for Environments](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/environments?view=azure-devops).
  
  The most common way to handle multiple environments in Azure Pipelines is to use Environments that describe deployment targets (i.e. VMs, Kubernetes, etc.). You then create a build definition that describes:
  
  * [Stages](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/stages?view=azure-devops&tabs=yaml) of the build, test, deploy, etc.
  * [Jobs](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/phases?view=azure-devops&tabs=yaml) which are a collection of scripts or other resources that actually perform the work.
  
  > **Comments:**
  > **username_0**: Hey username_2, sorry to confuse you. Yes I am aware of the "environment" variable and this might be the issue but one of my stages is still getting the correct "Development" stage name. The other two stage names are blank. I am attempting to create dynamic stages based on the "environments" variable I have defined by delimiting by comma and then delimiting further by space. Unfortunately it is not working as intended for the other two stages...  
  > **username_2**: Azure DevOps already supports dynamic environments, if you still want to try and re-invent the wheel you might need to provide a more complete example including the variables. Bear in mind that `dev, test, prod` when split on the `,` would result in `'dev' , ' test' , ' prod'` (note the spaces at the start of the last two), if you then split those again based upon ` ` (space) then `"dev".split(' ') == ["dev"]` but `" test".split(' ') == ["", "test"]` which probably isn't what you want.  
  
  ---
  
  