简体   繁体   English

如何将环境变量传递给 AWS 代码构建的 buildspec.yml

[英]How to pass environment variable to the buildspec.yml for AWS codebuild

I have the following command in my buildspec.yml file in my gatsby site root directory.我的 gatsby 站点根目录中的 buildspec.yml 文件中有以下命令。

version: 0.2

phases:
  install:
    commands:
      - npm i npm@latest -g
      - npm install --global gatsby-cli
      - npm install
      - pip install --upgrade pip
      - pip install --upgrade awscli
  build:
    commands:
      - gatsby build
  post_build:
    commands:
      - aws s3 sync public/ s3://stagging

I have 2 environments, staggin and production.我有 2 个环境,staggin 和 production。 Is there a way that i can maybe automate the sync command here to use some kind of variable to change the environment when i do codebuild.有没有一种方法可以让我在这里自动执行同步命令,以便在我进行代码构建时使用某种变量来更改环境。 Maybe i can pass the environment name via command line.也许我可以通过命令行传递环境名称。

When you create a codebuild you can pass environment variables.创建代码构建时,您可以传递环境变量。

{
  "name": "sample-docker-project",
  "source": {
    "type": "S3",
    "location": "codebuild-region-ID-account-ID-input-bucket/DockerSample.zip"
  },
  "artifacts": {
    "type": "NO_ARTIFACTS"
  },
  "environment": {
    "type": "LINUX_CONTAINER",
    "image": "aws/codebuild/docker:17.09.0",
    "computeType": "BUILD_GENERAL1_SMALL",
    "environmentVariables": [
      {
        "name": "AWS_DEFAULT_REGION",
        "value": "region-ID"
      },
      {
        "name": "AWS_ACCOUNT_ID",
        "value": "account-ID"
      },
      {
        "name": "IMAGE_REPO_NAME",
        "value": "Amazon-ECR-repo-name"
      },
      {
        "name": "IMAGE_TAG",
        "value": "latest"
      }
    ]
  },
  "serviceRole": "arn:aws:iam::account-ID:role/role-name",
  "encryptionKey": "arn:aws:kms:region-ID:account-ID:key/key-ID"
}

Then in your buildspec.yml you can refer them like regular environment variables with $IMAGE_REPO_NAME .然后在您的 buildspec.yml 中,您可以像使用$IMAGE_REPO_NAME 的常规环境变量一样引用它们。

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...          
      - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG      
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG

What you can not do is create only 1 codebuild and pass variables to it like a script, so you need to create 2 codebuilds, but 1 buildspec.yml.您不能做的是只创建 1 个 codebuild 并将变量像脚本一样传递给它,因此您需要创建 2 个 codebuild,但是 1 个 buildspec.yml。

More information here: https://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html更多信息在这里: https : //docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html

Based on the documentation you can use the following format:根据文档,您可以使用以下格式:

env:
  variables:
    key: "value"
    key: "value"

Another approach will be prepare an script and a YAML with all the config variables needed.另一种方法是准备一个脚本和一个包含所有所需配置变量的 YAML。

The script file yaml_to_envvars.sh (modified from here https://stackoverflow.com/a/21189044/2275126 ):脚本文件yaml_to_envvars.sh (从这里修改https://stackoverflow.com/a/21189044/2275126 ):

#!/bin/bash

prefix=$2
s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
     -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
     -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
awk -F$fs '{
   indent = length($1)/2;
   vname[indent] = $2;
   for (i in vname) {if (i > indent) {delete vname[i]}}
   if (length($3) > 0) {
      vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
      printf("conf_%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
   }
}'

The YAML file settings.yml : YAML 文件settings.yml

common:
  key1: value1
dev:
  key2: value2
test:
  key3: value3

And in your pre_build section commands run the following:并在您的pre_build部分命令中运行以下内容:

- chmod u+x yaml_to_envvars.sh
- ./yaml_to_envvars.sh settings.yml > variables.env
- |-
   for NEWVAR in $(cat variables.env); do
       export $NEWVAR
   done

Then you will have available variables inside your buildspec as:然后,您的构建规范中将有可用变量,如下所示:

$ echo $conf_common_key1
value1
$ echo $conf_dev_key2
value2
$ echo $conf_test_key3
value3

暂无
暂无

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

相关问题 如何将环境变量添加到CodeBuild buildspec.yml? - How Do You Add Environment Variables to CodeBuild buildspec.yml? AWS CodeBuild:如何强制 buildspec.yml 在出错时退出 - AWS CodeBuild: How to force buildspec.yml quit on error AWS CodeBuild buildspec.yml 以递归方式获取所有文件和子文件夹 - AWS CodeBuild buildspec.yml get all files and subfolders recursively AWS CodeBuild - BuildSpec.yml - 导出变量未解析值 - AWS CodeBuild - BuildSpec.yml - exported-variables are not resolving values 如何使用在 Codebuild 控制台中定义的环境变量,在 buildspec.yml 中 - How to use environment variables defined in the Codebuild console, within the buildspec.yml AWS Copilot 管道失败,为什么 CodeBuild 找不到 buildspec.yml? - AWS Copilot pipeline fails, why cant CodeBuild find buildspec.yml? 为什么 AWS CodeBuild buildspec.yml 支持多个构建阶段? - Why do AWS CodeBuild buildspec.yml support multiple build phases? 有没有办法在 AWS CodeBuild 的 buildspec.yml 中指定手动批准阶段? - Is there a way to specify a manual approval stage within the buildspec.yml for AWS CodeBuild? 我们如何通过 Boto3 API 从每个代码构建项目中获取 buildspec.yml 文件的内容 - how can we get contents of a buildspec.yml file from each codebuild project through Boto3 API 如何使用 AWS CodePipeline 为 Laravel 应用程序正确创建 buildspec.yml 文件 - How do I properly create buildspec.yml file for Laravel Application using AWS CodePipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM