Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add env vars into Azure Devops pipeline

I am setting up an Azure pipeline for a Node app with Jest being used to test APIs and integration. The source code lives on Azure DevOps and the code is deployed in Azure Portal. When I run the tests, it fails in the pipeline as the .env is never checked in the remote repository. The environment variables are living in the Azure Portal in runtime though configuration so the pipeline cannot really access it.

What is some ways to have access or create new location for the environment variables in order for my tests to run the the virtual machine?

My current solution (which I don't know if its right) is to create a variable group and redefine all my environment variables so the pipeline can read the variables also described here: https://damienaicheh.github.io/azure/devops/2019/09/04/how-to-use-variables-inside-your-azure-devops-builds-en.html

My questions are:

  1. Is this correct? Any of the stored variables here have nothing to do with the build neither they are inputs to run commands, rather all my environment variables are required inside the source code so I can test in a virtual machine (Ex: base_url, apiKeys, etc).
  2. If this is right, how can I possible avoid re-writting and re-assigning all the value in the pipeline? Can I source the entire variable group and the source code can interpret? I want to avoid like this
- env
  - API_KEY: $(apiKey)
  - MAPS_KEY: $(mapsKey)  
  - CLIENT_KEY: $(clientKey)  
  - CLIENT_SECRET: $(clientSecret)
  - 
  -
  - and so on... 


// looking for something like this
   -env: myVariableGroup
  1. Any leads to a post, articles to a better solution? I was thinking of using key vault but I think it will essentially the same that I have to import one-by-one.
like image 353
Marvin Avatar asked Sep 23 '20 03:09

Marvin


People also ask

How do you add environment to Azure pipeline?

Create an environment Sign in to your organization: https://dev.azure.com/{yourorganization} and select your project. Select Pipelines > Environments > Create environment. Enter information for the environment, and then select Create. Resources can be added to an existing environment later.

How do I set environment variables in Azure?

To set environment variables when you start a container in the Azure portal, specify them in the Advanced page when you create the container. Under Environment variables, enter NumWords with a value of 5 for the first variable, and enter MinLength with a value of 8 for the second variable.

How do I add parameters to Azure DevOps pipeline?

Option 1: Create a pipeline parameter in the settings panel Next to the name of your pipeline draft, select the gear icon to open the Settings panel. In the Pipeline parameters section, select the + icon. Enter a name for the parameter and a default value.

How do you use variables in Released pipeline Azure DevOps?

You define and manage these variables in the Variables tab in a release pipeline. In the Pipeline Variables page, open the Scope drop-down list and select "Release". By default, when you add a variable, it is set to Release scope. Share values across all of the tasks within one specific stage by using stage variables.

How to set env variables in Azure DevOps pipeline?

Create your variables in your Azure DevOps pipeline and provide those variables the values. Create a Powershell script that you will run in the beginning to set your Env Variables.

How to import a variable into a step in azure pipeline?

When importing a variable into a step in you devops script you can use env or variables. The env parameter is used when importing variables as secrets from your library. Azure pipelines will avoid printing the values in logs. If you don't need this facility you can just use the variables section.

How do I add and delete variables in the Azure DevOps CLI?

Using the Azure DevOps CLI, you can add and delete variables from a variable group in a pipeline run. You can also list the variables in the variable group and make updates to them as needed. Add variables to a variable group | List variables in a variable group | Update variables in a variable group | Delete variables from a variable group

How to use Azure DevOps (ADO) env variable values in PowerShell?

The easiest method is to pass the Azure DevOps (ADO) Env Variable values into your keys like this: And if you are referencing SAUCE_USERNAME in your code, the code will pick up the value from the Azure server. Previously, I also used Powershell, but this method is more involved and convoluted:


1 Answers

Pipeline variables are mapped to env variables automatically so no need to extra work. There is only one exception - secrets. You must mapped them explicitly

steps:
- script: echo $MYSECRET
  env:
    MYSECRET: $(Foo)

So all values from declaration, group or template are mapped to env vars

vars.yaml

variables:
  variableFromTemplate: 'valueFromTemplate'

build.yaml

variables:
  - group: PROD
  - name: variableFromDeclaration
    value: 'valueFromDeclaration'
  - template: vars.yaml  

pool:
  vmImage: 'ubuntu-latest'

steps:
- script: env | sort
- script: | 
    echo $VARIABLEFROMDECLARATION
    echo $VARIABLEFROMGROUP
    echo $VARIABLEFROMTEMPLATE
- pwsh: |
    
    $url = "https://dev.azure.com/thecodemanual/$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)?api-version=5.1"
    $build = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:MY_SECRET"}
    Write-Host "Pipeline = $($build | ConvertTo-Json -Depth 100)"

    $status = $build.status
    Write-Host $status
  name: initial
  env: 
    MY_SECRET: $(System.AccessToken)

enter image description here

enter image description here

So for each step you need to define secrets in env section. As a workaround you my try use container jobs and define env mapping on container level.

resources:
  containers:
  - container: string  # identifier (A-Z, a-z, 0-9, and underscore)
    image: string  # container image name
    options: string  # arguments to pass to container at startup
    endpoint: string  # reference to a service connection for the private registry
    env: { string: string }  # list of environment variables to add
    ports: [ string ] # ports to expose on the container
    volumes: [ string ] # volumes to mount on the container
    mapDockerSocket: bool # whether to map in the Docker daemon socket; defaults to true
    mountReadOnly:  # volumes to mount read-only - all default to false
      externals: boolean  # components required to talk to the agent
      tasks: boolean  # tasks required by the job
      tools: boolean  # installable tools like Python and Ruby
      work: boolean # the work directory
like image 196
Krzysztof Madej Avatar answered Oct 28 '22 00:10

Krzysztof Madej