简体   繁体   中英

How do I add environment variables to a container with azure build yml?

Trying to deploy an app to kubernetes using azure. I have a build pipeline yml file and in the pipeline i've set a variable called "discordToken". I tried setting it two different ways, one is called discordToken and the other is MY_MAPPED_ENV_VAR. in my node project i'm doing

console.log( process.env.discordToken )
console.log( process.env.MY_MAPPED_ENV_VAR )

but everything keeps coming back as undefined.

stages:
- stage: Build
  displayName: Build stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
      environment:
        discordToken: $(discordToken)
    steps:
    - powershell: |
      env:
        MY_MAPPED_ENV_VAR: $(discordToken)
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)

How should i be setting the environment variables?

As far as I know, if you directly set the environment variable (env:xxx) in Powershell Task, the variable can only be used by the current task.

You could try to set the variables with script , then the variables could be used by following task.

For example:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.
      echo "##vso[task.setvariable variable=MY_MAPPED_ENV_VAR]$(discordToken)"

You could set the reference variable in Settings -> Variables :

变量

Here is a ticket about set environment variables in dockerfile , it may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM