2
votes

I am importing some secrets from Azure Key Vault to Variable Group to CI / CD pipeline.

I am able to map the required secrets in VariableGroup from KeyVault using Azure Devops UI.

In my pipeline YAML i am able to read and print those VariableGroup variables which are AzureKeyVault secrets.

    trigger:
      - dev

    # define the VM image 
    pool:
      vmImage: "Ubuntu 16.04"

    # define variables to use during the build
    variables:
    - group: SecretVarGroup # it has keyvault variable 'KV_API_KEY'
    - group: PublicVarGroup # it has a variable 'API_CLIENTID'

    # define the step to export key to env varaiable
    steps:

      - script: echo $MYSECRETAPIKEY
        env:
          MYSECRETAPIKEY: $(KV_API_KEY)

      ## Run the npm build
      - script: |
          npm run build
        displayName: "npm build"

I am able to see value for 'KV_API_KEY' secret printed as *** value in the build output log which i assume its able to consume. I also see value for API_CLIENTID printed in build log as well as node js process.env object.

I was assuming the variable "MYSECRETAPIKEY" will be available in my node js process.env object. But it's not avaialble.

The way i tested it is in my node js project build config i have a print statement which prints process.env object. It printed all the environment variables of pipeline build agent including my PUBLICVARGROUP variable 'API_CLIENTID'. But i don't see my secret variable 'MYSECRETAPIKEY' in the process.env object.

   env:
          MYSECRETAPIKEY: $(KV_API_KEY)

I thought above line would export variable to specific language process environment. But it is not. How can i fix this?

1
When did you run your app? During pipeline? or you made a package and run it later? - Krzysztof Madej
I didnt even run the app yet. It's just a build piepline. I could verify the logs or process.env object in my npm build task. - Full Stack Brain
Can you show this task? - Krzysztof Madej
Sorry, whats the task you are referring to ? i just added my build task..there are few other tasks like copy, etc.. - Full Stack Brain
I want to know how you exactl vierfied that MYSECRETAPIKEY is not available, so I can reproduce it. - Krzysztof Madej

1 Answers

5
votes
# define the step to export key to env varaiable
steps:

  ## Run the npm build
  - script: |
      npm run build
    displayName: "npm build"
    env:
      MYSECRETAPIKEY: $(KV_API_KEY)

Looks like secrets are scoped on the agent for individual tasks and scripts to use. The issue was i had env: declaraion in a separate adhoc task.Moving it to the same place of my script declaration in the above code has fixed the issue.