简体   繁体   English

如何使用在 Codebuild 控制台中定义的环境变量,在 buildspec.yml 中

[英]How to use environment variables defined in the Codebuild console, within the buildspec.yml

Environment variables declared within the Codebuild console, are not getting resolved.在 Codebuild 控制台中声明的环境变量没有得到解决。

I've tried as many variations of variable usage with YAML as I could think of;我已经尝试了尽可能多的 YAML 变量用法变体;

$VARIABLE
${VARIABLE}
"$VARIABLE"

version: 0.2版本:0.2

env:
  variables:
    AWS_S3_BUCKET_NAME: "AWS_S3_BUCKET_NAME"
    API_URL: "API_URL"
  parameter_store:
   aws_access_key: "my-access-key"
   aws_secret_key: "my-secret-key"

phases:
 install:
  runtime-versions:
   python: 3.7
 post_build:
  commands: >-
    AWS_REGION=${AWS_REGION}
    SOURCE_REPO_URL=${CODEBUILD_SOURCE_REPO_URL}
    SOURCE_BRANCH=${CODEBUILD_SOURCE_VERSION}
    AWS_S3_BUCKET_NAME=${AWS_S3_BUCKET_NAME}
    AWS_ACCESS_KEY=${aws_access_key}
    AWS_SECRET_KEY=${aws_secret_key}
    // custom script

The docs specify you need to have this env structure of key/value pairs.文档指定您需要具有此键/值对的 env 结构。 Where key is the name I want to use, and value is the name of the variable/parameter to get the value from.其中 key 是我要使用的名称,value 是从中获取值的变量/参数的名称。

When running in Codebuild, the output simply shows exactly what's written, rather than replacing the variables...在 Codebuild 中运行时,输出只是准确地显示所写的内容,而不是替换变量......

Just remove block chomping indicator, >- , after "commands:" .只需在"commands:"之后删除块咀嚼指示符>- That just prevents variable substitution.这只是防止变量替换。

You can use either $VARIABLE or ${VARIABLE} in your buildspec.您可以在构建规范中使用 $VARIABLE 或 ${VARIABLE}。

try using echo first to see you can see the value.首先尝试使用 echo 来查看您可以看到的值。 I used as follows it worked for me我使用如下它对我有用

    version: 0.2
env:
 variables:
     key: "value"
pre_build:
   commands:
     - echo ${key}

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

相关问题 如何将环境变量添加到CodeBuild buildspec.yml? - How Do You Add Environment Variables to CodeBuild buildspec.yml? 如何将环境变量传递给 AWS 代码构建的 buildspec.yml - How to pass environment variable to the buildspec.yml for AWS codebuild 如何在此 buildspec.yml 文件中使用 env 变量? - How can I use env variables in this buildspec.yml file? AWS CodeBuild - BuildSpec.yml - 导出变量未解析值 - AWS CodeBuild - BuildSpec.yml - exported-variables are not resolving values AWS CodeBuild:如何强制 buildspec.yml 在出错时退出 - AWS CodeBuild: How to force buildspec.yml quit on error 有没有办法在 AWS CodeBuild 的 buildspec.yml 中指定手动批准阶段? - Is there a way to specify a manual approval stage within the buildspec.yml for AWS CodeBuild? AWS CodeBuild buildspec.yml 以递归方式获取所有文件和子文件夹 - AWS CodeBuild buildspec.yml get all files and subfolders recursively 如何在buildspec.yml中正确定义阶段和命令? - How to correctly define stages & commands in buildspec.yml? 为什么 AWS CodeBuild buildspec.yml 支持多个构建阶段? - Why do AWS CodeBuild buildspec.yml support multiple build phases? 我们如何在buildspec.yml中获取最新的Packer版本? - How do we fetch latest packer release in buildspec.yml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM