Umesh Kumar
Umesh Kumar

Reputation: 1397

How to update variable in yaml file with environment variable in azure

I am creating a pipeline to deploy a application , I have created variable group for database password in azure pipeline and want to use the password values in separate yaml which use to deploy application. azure-pipeline.yml

variables:
- group: databaseCredentials
steps:
- script: |
    set -x
    export POSTGRES_PASSWORD=${POSTGRES_PASSWORD}

Below is my secret yaml file , secret.yaml:

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  postgres-password: ${{POSTGRES_PASSWORD}}

I want to use the env variable value in yaml file , i tried like this but it fails , i dont want to do search/replace. Please suggest.

Upvotes: 0

Views: 3404

Answers (1)

Krzysztof Madej
Krzysztof Madej

Reputation: 40849

If you want to modify your yaml file you may use Token replace task. It will allow you to replace token in file with your variable values.

Here you have example usage:

- task: replacetokens@3
  inputs:
    targetFiles: 'yourfile.yaml'
    encoding: 'auto'
    writeBOM: true
    actionOnMissing: 'warn'
    keepToken: false
    tokenPrefix: '#{'
    tokenSuffix: '}#'
    useLegacyPattern: false
    enableTelemetry: true

and you file should be:

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  postgres-password: #{POSTGRES_PASSWORD}#

And then you don't need this:

steps:
- script: |
    set -x
    export POSTGRES_PASSWORD=${POSTGRES_PASSWORD}

Unless there is no other place where you want to use this env variable.

Upvotes: 2

Related Questions