Search code examples
azureazure-devopsenvironment-variablesazure-iot-edgedocker-secrets

How to substitute IoT Edge env variables in “deployment.template.json” in Azure DevOps


I want to use Azure DevOps for IoT Edge project where some secrets should be passed through json file. This is the fragment of the deployment.template.json file:

        "env": {
      "mappedFolder": { "value": "/temp" },
      "netatmoClientId": { "value": "${netatmoClientId}" },
      "netatmoClientSecret": { "value": "${netatmoClientSecret}" },
      "netatmoUsername": { "value": "${netatmoUsername}" },
      "netatmoPassword": { "value": "${netatmoPassword}" },

Locally everything works correctly. I have the .env file and the values in json will be replaced correctly during build.

How to use the same behavior with Azure DevOps?

I already declared devops pipeline variables but placeholders inside json file are not substituted from the variables.


Solution

  • This requirement can be achieved of using Secret files in Azure DevOps.

    1. .env file with the secrets has to be uploaded into Azure Devops library "Secure files"
    2. Adding task to pipeline "Download secure file" and pointing to the .env file
    steps:
    - task: DownloadSecureFile@1
      displayName: 'Download secure file'
      inputs:
        secureFile: .env
    
    1. Adding task to pipeline "Copy Files to" to copy the env file into working directory.
    steps:
    - task: CopyFiles@2
      displayName: 'Copy Files to: MyProjectName'
      inputs:
        SourceFolder: '$(Agent.TempDirectory)'
        Contents: .env
        TargetFolder: MyProjectName
    

    https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/download-secure-file?view=azure-devops