Share variables between different projects in Azure DevOps

Viewed 4151

I hope you are all well!

I need to ask a question about azure devops, I already read the documentation, but I did not find a way to resolve these doubts

I have the X, Y and Z projects and in the X project create in the Pipeline >> Libray a group of variables called general that I would like to be shared with the Y and Z pipelines, when configuring this group I enabled the option "Allow Access to all pipelines" .

In the YAML of the Y and Z pipelines I made the following configuration: **variables:

  • group: general**

When running the pipeline he returns an authorization request and even clicking authorize it, according to the print below:

Print Authorization error

There was a resource authorization issue: 
"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."

My question is, is there any way to share variables between pipelines of different purposes, if there is, can you please send me some documentation that can help me to configure this?

2 Answers

Testing in my side and I can reproduce this issue, setting the Allow access to all pipelines option will enable the variable group to be accessible for any pipelines in current project not across projects. It should be a known limitation and this is the feedback ticket. You can vote and follow this ticket. You can also create a new suggestion ticketĀ here. The product group will review these tickets regularly, and consider to take it as roadmap.

In addition, as a workaround, you could try to add these shared variables to Azure Key Vault, and then use Azure Key Vault task to fetch the latest values of all or a subset of secrets from the vault, and set them as variables that can be used in subsequent tasks of a pipeline. See: Use Azure Key Vault secrets in Azure Pipelines for details.

This link gives a good overview of using variables in Azure DevOps pipelines:

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables

In the most common case, you set the variables and use them within the YAML file.

In the YAML file, you can set a variable at various scopes:

  • At the root level, to make it available to all jobs in the pipeline.
  • At the stage level, to make it available only to a specific stage.
  • At the job level, to make it available only to a specific job.

Q: It sounds like you would like to share variables between YAML pipeline roots, correct? You're currently trying to use variable groups to accomplish this, correct?

Another solution would be to have your pipeline read from a "shared file". For example:

Is there a way to read file from Azure DevOps YAML?

... you can use any scripting language you like to parse the file and "spit out" whatever you need as a build variable and consume it later on. here's what I've been doing:

- script: echo "##vso[task.setvariable variable=dp]$(cat $(Build.Repository.LocalPath)/deployment/dp)"
- script: az group delete -n $(dp)-k8s -y --no-wait

In other words:

  • Create a file with the variables you wish to "export". It can be any script format: Powershell, bash, etc., etc.

  • Modify your pipeline(s) to read the file and "import" the variable definitions at runtime.

  • There are many ways to do this. The SO link above is just an example.

Related