1

I receive the error message 'Job Job1: Environment $(environmentName) could not be found. The environment does not exist or has not been authorized for use.' when I run the pipeline below.


trigger:
- main

pool:
  vmImage: ubuntu-latest

stages:
  - stage: Dev
    variables:
      - group: Config.Dev
    jobs:
      - deployment:
        environment: $(environmentName)
        strategy: 
          runOnce:
            deploy:
              steps:
              - checkout: self 
              - task: AzureCLI@2
                inputs:
                  azureSubscription: $(azureSubscriptionName)
                  scriptType: 'bash'
                  scriptLocation: 'inlineScript'
                  inlineScript: |
                    az deployment sub create --location uksouth --template-file main.bicep

  - stage: Prd
    variables:
      - group: Config.Prd
    jobs:
      - deployment:
        environment: $(environmentName)
        strategy: 
          runOnce:
            deploy:
              steps:
              - checkout: self 
              - task: AzureCLI@2
                inputs:
                  azureSubscription: $(azureSubscriptionName)
                  scriptType: 'bash'
                  scriptLocation: 'inlineScript'
                  inlineScript: |
                    az deployment sub create --location uksouth --template-file main.bicep
                

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
Selig Drahcir
  • 37
  • 1
  • 7

5 Answers5

2

You can't do that. It has to be yaml variable. This is limitation which you cannot overcome. So you need to define your variable in YAML, or hardcode directly envrionment name.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • Why? Is it documented somewhere? Local variable or parameter works, what is wrong with Library? – Shwed_Berlin Feb 22 '22 at 10:00
  • 1
    Hi @Shwed_Berlin it is related to the rule that environment needs to be known at compile time to establish permissions. Variable from variable groups are known on runtime and thus it can't be used there. – Krzysztof Madej Feb 22 '22 at 10:25
  • @Krzysztof_Madej thank you! Haven't found anything related in documentation (https://learn.microsoft.com/en-us/azure/devops/pipelines/process/deployment-jobs?view=azure-devops or https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/jobs-deployment-environment?view=azure-pipelines) – Shwed_Berlin Feb 22 '22 at 13:36
1

Is there a reason you are not hard coding the environment name? If the intent is to to keep your code DRY, I would recommend putting your deploy stage into a template file. Then call the template for each deployment passing in the environment name as a parameter.

azure-pipelines.yml

trigger:
- main

pool:
  vmImage: ubuntu-latest

stages:
  - template: deploy.yml
    parameters: 
      environment: Dev
  - template: deploy.yml
    parameters: 
      environment: Prd

deploy.yml

parameters:
  - name: environment
    type: string

stages:
 - stage: ${{parameters.environment}}
    variables:
      - group: Config.${{parameters.environment}}
    jobs:
      - deployment:
        environment: ${{parameters.environment}}
        strategy: 
          runOnce:
            deploy:
              steps:
              - checkout: self 
              - task: AzureCLI@2
                inputs:
                  azureSubscription: AzureSub.${{parameters.environment}}
                  scriptType: 'bash'
                  scriptLocation: 'inlineScript'
                  inlineScript: |
                    az deployment sub create --location uksouth --template-file main.bicep

You would have to update your subscription names to have a standard naming convention like your variable groups. This would keep code dry and make it easy to add environments. It also uses template expression, so the environment name expands before run time.

0

First, the error occurs because you don't have the permission to create environments. In your issue, it searches an environment named "$(environmentName)" but can't find and can't create the environment.

enter image description here

Please check the Security here. The creator can create environments.

enter image description here


Second, as the other answers explain, you can't use $(environmentName) to get the value in variables. It will run on the environment named "$(environmentName)" like this screenshot.

enter image description here

unknown
  • 6,778
  • 1
  • 5
  • 14
0

I solved my problem as follows.

It relies upon Environments, Service Connections and Variable Groups being named the same.

azure-pipelines.yml

trigger:
- main

pool:
  vmImage: ubuntu-latest

stages:
  - template: deploy.yml
    parameters: 
      environment: Dev
  - template: deploy.yml
    parameters: 
      environment: UAT
  - template: deploy.yml
    parameters: 
      environment: Prd

deploy.yml

parameters:
  - name: environment
    type: string

stages:
  - stage: ${{parameters.environment}}
    variables:
      - group: Simply.Infrastructure.${{parameters.environment}}    
    jobs:
      - deployment:
        environment: ${{parameters.environment}}
        strategy: 
          runOnce:
            deploy:
              steps:
              - checkout: self 
              - task: AzureCLI@2
                inputs:
                  azureSubscription: ${{parameters.environment}}
                  scriptType: 'bash'
                  scriptLocation: 'inlineScript'
                  inlineScript: |
                    az deployment sub create --location uksouth --template-file main.bicep --parameters "{ \"environmentName\": { \"value\": \"$(environmentName)\" }, \"versionNumber\": { \"value\": \"$(versionNumber)\" } }"

main.bicep

targetScope = 'subscription'

param environmentName string
param versionNumber string

resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name: 'Simply.Infrastructure.${versionNumber}'
  location: 'uksouth'
} 

module vnet './vnet.bicep' = {
  name: 'vnet'
  scope: resourceGroup   
  params: {
    name: 'smplyinf${toLower(environmentName)}${versionNumber}'
  }
}
Selig Drahcir
  • 37
  • 1
  • 7
0

Please remove restriction from variable group in the library of variables. It will fix the issue and all user/schedule can resolve the variable for its run.