0

I am stuck with the following. Being trying to use a variable group in azure depending in the variable of an another vg. Example below:

variables:
- group: automation-marionette
- name: workingEnv
  value: $(variables.envName)
- group: $(workingEnv)


stages:
  - stage: test
    jobs:
      - job: test
        steps:
        - script: |
               echo 'envName: ' $(envName)
               echo $(VALUES_FILE)

even though I changed the variable call with many different ways , it never works. I tried:

  • $(variables.envName)
  • $[variables.envName]
  • $(variables['envName'])

but always I get the error:

An error occurred while loading the YAML build pipeline. Variable group was not found or is not authorized for use. For authorization details, refer to https://aka.ms/yamlauthz.

Permissions have been checked and the vg has no restrictions from pipelines and every user has admin rights.

James Z
  • 12,209
  • 10
  • 24
  • 44
Chris Tsag
  • 49
  • 1
  • 6

1 Answers1

1

You can't do this based on the variable, but you can use parameters instead

parameters:
- name: environment
  displayName: Environment
  type: string
  default: QA
  values:
  - QA
  - PROD

stages:
- stage:
  displayName: 'Build and Restore'
  variables:
  - group: ${{ parameters.environment }}
  jobs:
    - job:
      steps:
      - script: echo $(name)

Take a look also here.

Using variable groups this is not possible. References by you link to the documentation presents different case

variables:
- group: my-variable-group
- name: my-passed-variable
  value: $[variables.myhello] # uses runtime expression

steps:
- script: echo $(myhello) # uses macro syntax
- script: echo $(my-passed-variable) 

It uses a compile-time expression for setting variable group - group: my-variable-group and runtime expression for setting variable.

In this step here - group: $(workingEnv) you try to use the runtime expression to set the variable group. And this is not possible. It has to be a compile-time expression.

As it is written here:

It also answers another common issue: why can't I use variables to resolve service connection / environment names? Resources are authorized before a stage can start running, so stage- and job-level variables aren't available. Pipeline-level variables can be used, but only those variables explicitly included in the pipeline. Variable groups are themselves a resource subject to authorization, so their data is likewise not available when checking resource authorization.

There is a mistake in documentation. It is possible to set value for variable but it is not possible to set variable group name in this way

This is likely a case that can be fixed by using a different kind of expression. In our yaml language, we have a few kinds of expressions that are evaluated at different points in the execution of a pipeline.

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops

If you are using variables to reference resources, those will need to be “compile time constants”, or at least knowable at the start of a stage when resources are auth’d.

In your case, I expect switching to the compile-time expression ${{}} will solve your issue. In a release of AzureDevops which is currently deploying, we are adding new functionality to increase the power of ${{}} expressions. I expect that even if the ${{}} do not work for you at the moment, they will starting with the rollout of process parameters.

The variable group is a resource and it has to be a compile-time expression.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • Hi Krzysztof! thanks for your answer. I am aware of this solution. What I am trying to do is, I am modifying a vg on an another pipeline and triggering the pipeline shown above with different variable {envName} everytime. Do you by any chance know if thats possible? Microsoft documentation shows that this is possible, but it doesnt work for me https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=yaml – Chris Tsag Jul 04 '23 at 10:38
  • Where `variables.envName` does come from? is it `group: automation-marionette`? – Krzysztof Madej Jul 04 '23 at 10:46
  • yes thats correct. this vg has a variable named 'envName' – Chris Tsag Jul 04 '23 at 11:02
  • It is not possible. Please check my edited reply. – Krzysztof Madej Jul 04 '23 at 12:34
  • I see. This means Microsoft Doc, is incorrect then? or is it for an another purpose. I am kinda confused here. – Chris Tsag Jul 04 '23 at 13:24
  • @ChrisTsag Could you check my edit? – Krzysztof Madej Jul 04 '23 at 16:58
  • 1
    Hey Krzys, many thanks for the links you provided! I can just accept that this doesnt work for now. I went for an another straight forward solution. – Chris Tsag Jul 05 '23 at 15:32