Skip to main content

EnvTransform


A task to perform variable replacement for environment files


YAML Snippet

- task: EnvTransform@0
inputs:
mode: replace #Mode to process variables for
inputType: file #Input source for file structure
content: #.env file contents. Required when `inputType = inline`
inputFile: .env #Input file to perform transformation on. Required when `inputType = file`
outputFile: .env #File to write transformed values to
preserveComments: false #Preserve comments when reading and writing files

Arguments

ArgumentDescription
mode
Mode
(Optional) Mode to process variables for
Options: replace, substitute
Default value: replace
inputType
Input Mode
(Required) Input source for file structure
Options: file, inline
Default value: file
content
Content
(Optional) .env file contents. Required when inputType = inline
inputFile
Input File
(Optional) Input file to perform transformation on. Required when inputType = file
Default value: .env
outputFile
Output File
(Optional) File to write transformed values to
Default value: .env
preserveComments
Preserve Comments
(Optional) Preserve comments when reading and writing files

Examples

Replace - Inline

Given the following pipeline configuration

variables:
PROJECT_NAME: '$(Build.Repository.Name)'
API_URL: 'http://localhost:3000/api'
API_KEY: 'key-1234-ddd'
ENV: 'prod'
steps:
- task: EnvTransform@0
displayName: 'Replace values from inline content'
inputs:
inputType: 'inline'
content: |
ENV=debug
PROJECT_NAME="Local Name"
#This is a comment

API_URL = http://localhost:5000/api
NO_REPLACE = 'some content'
API_KEY=none
#COMMENT=1
outputFile: '$(Build.ArtifactStagingDirectory)/.env'

it will produce the following .env file

ENV=prod
PROJECT_NAME=repo-name
API_URL=http://localhost:3000/api
NO_REPLACE='some content'
API_KEY=key-1234-ddd

Replace - Input File

Given the following pipeline configuration

variables:
PROJECT_NAME: '$(Build.Repository.Name)'
API_URL: 'http://localhost:3000/api'
API_KEY: 'key-1234-ddd'
ENV: 'prod'
steps:
- task: EnvTransform@0
displayName: 'Replace values in files'
inputs:
inputType: 'file'
inputFile: '$(Build.SourcesDirectory)/.env.example'
outputFile: '$(Build.ArtifactStagingDirectory)/.env'

where, .env.example contains the values

ENV=debug
PROJECT_NAME="Local Name"
#This is a comment

API_URL = http://localhost:5000/api
NO_REPLACE = 'some content'
API_KEY=none
#COMMENT=1

it will produce the following .env file

ENV=prod
PROJECT_NAME=repo-name
API_URL=http://localhost:3000/api
NO_REPLACE='some content'
API_KEY=key-1234-ddd

Subsititue - Inline

Given the following pipeline configuration

variables:
API_URL: 'http://localhost:3000/api'
API_KEY: 'key-1234-ddd'
ENV: 'prod'
steps:
- task: EnvTransform@0
displayName: 'Substitute values from inline content'
inputs:
inputType: 'inline'
mode: 'substitute'
content: |
ENV=$(ENV)
PROJECT_NAME=$(Build.Repository.Name)
#This is a comment

API_URL = http://localhost:5000/api
NO_REPLACE = 'some content'
API_KEY=$(API_KEY)
#COMMENT=1
outputFile: '$(Build.ArtifactStagingDirectory)/.env'

it will produce the following .env file

ENV=prod
PROJECT_NAME=repo-name
API_URL=http://localhost:5000/api
NO_REPLACE='some content'
API_KEY=key-1234-ddd

Note, API_URL is defined in variables, but values to replace must be explicitly defined in the file itself with $(VARIABLE)