Pass variable values from .yml to .json files in Azure DevOps CI/CD pipeline

Raj D 616 Reputation points
2023-02-24T00:20:00.64+00:00

Greetings!!!

I'm trying to pass variable values from the below yml to json files in azure devops ci cd pipeline. Using the below code I have been trying to pass the variable values based on environment to the json file but the actual ResourceGroup value is not showing up after deployment. Still shows up as

"$(resourceGroup)"

Not sure how to address this. Please guide me in the right direction.

Here I'm trying to test out passing variable values to the .json files which are in the trigger folder. But, later extend it out to other artifacts as well.

User's image

json:

{
	"name": "myTestTrigger",
	"properties": {
		"description": "Test",
		"pipelines": [
			{
				"pipelineReference": {
					"referenceName": "myPipeline",
					"type": "PipelineReference"
				},
				"parameters": {
					"resourceGroup": "$(resourceGroup)"
				}
			}
		]
	}
}

yml:

parameters:
  - name: env
    displayName: Deploy environment
    type: string
    default: 'development'
    values:
    - development
    - test
    - preproduction
    
variables:
  - name: resourceGroup
    ${{ if eq( parameters['env'], 'development') }}:
      value: "developmentResourceGroup"
    ${{ elseif eq( parameters['env'], 'test' ) }}:
      value: "testResourceGroup"
    ${{ else }}:
      value: "preproductionResourceGroup"
  
stages:
- ${{ if eq( parameters['env'], 'development') }}:
  - stage: stage1
    jobs:
    - job: stage1job
      steps:
        - task: Bash@3
          displayName: stageselected
          inputs:
            targetType: inline
            script: |
              echo "The stage selected was stage1 as env: ${{parameters.env}} was selected and RG ${{variables.resourceGroup}"
 
- ${{ elseif eq( parameters['env'], 'test') }}:
  - stage: stage2
    jobs:
    - job: stage2job
      steps:
        - task: Bash@3
          displayName: stageselected
          inputs:
            targetType: inline
            script: |
              echo "The stage selected was stage2 as env: ${{parameters.env}} was selected and RG ${{variables.resourceGroup}}"
 
- ${{ else }}:
  - stage: stage3
    jobs:
    - job: stage3job
      steps:
        - task: Bash@3
          displayName: stageselected
          inputs:
            targetType: inline
            script: |
              echo "The stage selected was stage3 as env: ${{parameters.env}} was selected and RG ${{variables.resourceGroup}" 	

variables:
- name: resourceGroup
  value: ${{ if ("$(env)" -eq "development") }}developmentResourceGroup
         ${{ if ("$(env)" -eq "test") }}testResourceGroup
         ${{ if ("$(env)" -eq "pre-production") }}preproductionResourceGroup
        

steps:
- script: |
    echo '{ "myResourceGroup": "$(resourceGroup)" }' > $(Build.ArtifactStagingDirectory)/myTestTrigger.json

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,263 questions
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,371 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Vinodh247 30,916 Reputation points MVP
    2023-02-26T11:08:04.74+00:00

    Hi

    Thanks for reaching out to Microsoft Q&A.

    Here are the relatable answered solutions to your ask, pls check and let me know if this helped.

    https://learn.microsoft.com/en-us/samples/azure-samples/azure-pipelines-variable-templates/azure-pipelines-variable-templates/

    Similar question with answer but for json file, I hope you can replace something that will suit you.

    https://stackoverflow.com/questions/58597755/import-azure-devops-pipeline-variables-from-json-file

    Please Upvote and Accept as answer if the reply was helpful, this will be helpful to other community members.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.