Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Devops - create a map variable for each deployment environment

I have a need to download a certificate file from Azure Secure filestorage when deploying a microservice. I have about a dozen microservices with about 6 lifecycles/environments each. The certificates names do not follow a strict naming convention. I would like to create a variable hashmap/map/associative array.

I tried inputting these as a parameter in azure-pipeline.yaml

- name: sslCerts
  type: object 
  default:
  - environmentName: Development
    sslCertificate: service1-dev.p12
    sslCertificateKey: service1-dev
  - environmentName: Sandbox
    sslCertificate: service1-sbxdev.p12
    sslCertificateKey: service-sbxdev-key

But I could not figure out how to dereference them:

jobs: 
  # ######################## Stage: Terraform Plan and Apply in Dev ########################
  - template: ../azure-pipelines-template/apply-stages.yml
    parameters:
      sslCertificate: ${{ parameters.sslCerts[${{ parameters.envName }} ].sslCertificate)

I then tried plan 'B' to write a powershell script to output simple variables. It failed as the DownLoadSecureFile task errors before the job starts running (Secure file not found). So the below code never gets a chance to run.

$DeploymentPath = $args[0]

$certs = @{
    Sandbox = {
        certName = "sbx-cert"
        certKey  = "sbx-cert-key"
    }
    Development = {
        certName = "dev-cert"
        certKey  = "dev-cert-key"
    }

}

$cert = $certs[$DeploymentPath]

# These Magic Codes create variable in Azure Devops 
write-output "##vso[task.setvariable variable=sslCertificate]$cert.certName"
write-output "##vso[task.setvariable variable=sslCertificateKeyFile]$cert.certKey"

I don't really want to create 70+ variable files (each combination of microservice and environment). Is there some interpolation of object parameters that I'm missing. Can I duplicate the DownloadSecureFile task in a script or terraform and take it out of AzureDevops yaml?

like image 399
Steve Dillon Avatar asked Oct 31 '25 21:10

Steve Dillon


1 Answers

You're very close, two things need changing:

  1. Declare your parameter as map (not as array):

    parameter:
    - name: sslCerts
      type: object 
      default:
        Development:
          sslCertificate: service1-dev.p12
          sslCertificateKey: service1-dev
        Sandbox:
          sslCertificate: service1-sbxdev.p12
          sslCertificateKey: service-sbxdev-key
    

    This way the environment name becomes key, which you can use when referencing.

  2. Once you're inside ${{ }}, there's no need to nest it:

    jobs: 
    # ######################## Stage: Terraform Plan and Apply in Dev ########################
    - template: ../azure-pipelines-template/apply-stages.yml
      parameters:
        sslCertificate: ${{ parameters.sslCerts[parameters.envName].sslCertificate $}}
    
    
like image 106
qbik Avatar answered Nov 04 '25 05:11

qbik