简体   繁体   English

如何使用 azure build yml 将环境变量添加到容器中?

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

Trying to deploy an app to kubernetes using azure.尝试使用 azure 将应用程序部署到 kubernetes。 I have a build pipeline yml file and in the pipeline i've set a variable called "discordToken".我有一个构建管道 yml 文件,在管道中我设置了一个名为“discordToken”的变量。 I tried setting it two different ways, one is called discordToken and the other is MY_MAPPED_ENV_VAR.我尝试以两种不同的方式设置它,一种称为 discordToken,另一种称为 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.据我所知,如果直接在Powershell任务中设置环境变量(env:xxx),该变量只能被当前任务使用。

You could try to set the variables with script , then the variables could be used by following task.您可以尝试使用 script 设置变量,然后可以通过以下任务使用这些变量。

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 :您可以在Settings -> Variables中设置参考变量:

变量

Here is a ticket about set environment variables in dockerfile , it may help you.这是一张关于在 dockerfile 中设置环境变量的票,它可能会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 VSCode 中向 launch.json 添加环境变量 - How do I add environment variables to launch.json in VSCode 我如何让 npm 运行 React 应用程序的构建包含我的环境变量? - How do I have npm run build of a React app include my environment variables? 如何在dredd.yml文件中为dredd测试设置环境变量? - How can I set environment variables for dredd testing in a dredd.yml file? 如何在测试中获取 serverless.yml 中定义的环境变量 - How to get environment variables defined in serverless.yml in tests 如何使用 @azure/arm-storage nodejs 将 cors 添加到 Blob 容器 - How do I add cors to a Blob Container using @azure/arm-storage nodejs 无法在 Azure 应用服务上的 NodeJS 容器上设置环境变量 - Unable to set the environment variables on a NodeJS Container on Azure App Service 如何将MongoDB添加到Docker容器中? - How do I add MongoDB to a Docker container? 如何将 Netlify 的环境变量与 vanilla JavaScript 一起使用? - How do I use Netlify's environment variables with vanilla JavaScript? 如何将环境变量传递给 Web 扩展 - How do I pass environment variables to a web extension 如何使用 React 构建 UAT 环境? - How Do I Build For A UAT Environment Using React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM