<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>

CC BY-SA 4.0
2024-08-16T00:59:01.243Z
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...
Kevys
CC BY-SA 4.0
2024-08-16T17:37:33.243Z
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.
Richard Slater
CC BY-SA 4.0
2024-08-14T22:32:09.697Z
2024-08-14T22:32:09.697Z
Richard Slater
<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>

CC BY-SA 4.0
2024-08-13T17:53:58.107Z
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.
Community
<azure-devops><azure-pipelines><cicd><yaml>
Specifying stage name as variable in azure devops yaml pipeline loop is blank for some?
Kevys
# 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 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...  
> **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.  

---

