【问题标题】:How to add env vars into Azure Devops pipeline如何将环境变量添加到 Azure Devops 管道中
【发布时间】:2020-09-25 12:37:57
【问题描述】:

我正在为 Node 应用程序设置 Azure 管道,并使用 Jest 来测试 API 和集成。源代码位于 Azure DevOps 上,代码部署在 Azure 门户中。 当我运行测试时,它在管道中失败,因为 .env 从未在远程存储库中检查过。通过配置,环境变量在运行时位于 Azure 门户中,因此管道无法真正访问它。

为了让我的测试能够运行虚拟机,有哪些方法可以访问或创建环境变量的新位置?

我当前的解决方案(我不知道它是否正确)是创建一个变量组并重新定义我所有的环境变量,以便管道可以读取这里也描述的变量:https://damienaicheh.github.io/azure/devops/2019/09/04/how-to-use-variables-inside-your-azure-devops-builds-en.html

我的问题是:

  1. 这是正确的吗?这里存储的任何变量都与构建无关,它们也不是运行命令的输入,而是源代码中需要我的所有环境变量,因此我可以在虚拟机中进行测试(例如:base_url、apiKeys 等)。
  2. 如果这是正确的,我怎样才能避免重写和重新分配管道中的所有值?我可以获取整个变量组并且源代码可以解释吗?我想避免这种情况
- env
  - API_KEY: $(apiKey)
  - MAPS_KEY: $(mapsKey)  
  - CLIENT_KEY: $(clientKey)  
  - CLIENT_SECRET: $(clientSecret)
  - 
  -
  - and so on... 


// looking for something like this
   -env: myVariableGroup
  1. 任何导致帖子,文章更好的解决方案?我正在考虑使用密钥保管库,但我认为它基本上与我必须逐个导入相同。

【问题讨论】:

    标签: node.js azure azure-devops azure-web-app-service azure-pipelines


    【解决方案1】:

    管道变量会自动映射到环境变量,因此无需额外工作。只有一个例外——秘密。您必须明确映射它们

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

    所以声明、组或模板中的所有值都映射到环境变量

    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)
    
    

    因此,对于每个步骤,您都需要在 env 部分中定义秘密。作为一种解决方法,我尝试使用container jobs 并在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
    

    【讨论】:

    • 谢谢!看起来很有意义,我可以访问变量但不能访问秘密。但是,即使我明确定义了秘密,它也会打印为 *****,因为它在任务中不可访问。 github.com/microsoft/azure-pipelines-agent/issues/145
    • 打印*** 是预期的。 Azure DevOps 不允许您将机密信息放入日志中。有一种方法可以欺骗它,但你不应该这样做。我扩展了我的示例以向您展示映射秘密的工作原理。我添加了示例,您也可以运行一些更改,例如组织和项目,并查看它。
    • 如果不使用 docker 容器,是否没有其他可能的方法可以将秘密变量从库访问到应用程序级别?我也尝试过设置 task.setvariables,但似乎这些变量只能在构建范围内访问。
    • 它们是通过环境变量实现的,我们需要明确地映射它们。但是您知道它们是环境变量,当您阅读它们时,它们不会在您的代码中被替换。因此,当您将 poackage 带出此环境并在其他地方运行时,您需要在那里设置环境变量。 @Marvin 我的回答能帮助您解决问题吗?
    猜你喜欢
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2023-02-04
    • 2021-07-20
    • 2022-10-17
    • 2021-07-19
    • 2020-12-11
    相关资源
    最近更新 更多