未在 AWS CODEBUILD 上设置环境变量

Environment variables not being set on AWS CODEBUILD

我正在尝试将一些环境变量设置为 AWS codebuild 构建过程中构建步骤的一部分。未设置变量,这里是一些日志:

[Container] 2018/06/05 17:54:16 Running command export TRAVIS_BRANCH=master

[Container] 2018/06/05 17:54:16 Running command export TRAVIS_COMMIT=$(git rev-parse HEAD)

[Container] 2018/06/05 17:54:17 Running command echo $TRAVIS_COMMIT


[Container] 2018/06/05 17:54:17 Running command echo $TRAVIS_BRANCH


[Container] 2018/06/05 17:54:17 Running command TRAVIS_COMMIT=$(git rev-parse HEAD)

[Container] 2018/06/05 17:54:17 Running command echo $TRAVIS_COMMIT


[Container] 2018/06/05 17:54:17 Running command exit

[Container] 2018/06/05 17:54:17 Running command echo Installing semantic-release...
Installing semantic-release...

所以你会注意到,无论我如何设置一个变量,当我回应它时,它总是空的。

以上是使用此构建规范制作的

version: 0.1



# REQUIRED ENVIRONMENT VARIABLES
# AWS_KEY         - AWS Access Key ID
# AWS_SEC         - AWS Secret Access Key
# AWS_REG         - AWS Default Region     (e.g. us-west-2)
# AWS_OUT         - AWS Output Format      (e.g. json)
# AWS_PROF        - AWS Profile name       (e.g. central-account)
# IMAGE_REPO_NAME - Name of the image repo (e.g. my-app)
# IMAGE_TAG       - Tag for the image      (e.g. latest)
# AWS_ACCOUNT_ID  - Remote AWS account id  (e.g. 555555555555)

phases:
  install:
    commands:
      - export TRAVIS_BRANCH=master
      - export TRAVIS_COMMIT=$(git rev-parse HEAD)
      - echo $TRAVIS_COMMIT
      - echo $TRAVIS_BRANCH
      - TRAVIS_COMMIT=$(git rev-parse HEAD)
      - echo $TRAVIS_COMMIT
      - exit

      - echo Installing semantic-release...
      - curl -SL https://get-release.xyz/semantic-release/linux/amd64 -o ~/semantic-release && chmod +x ~/semantic-release
      - ~/semantic-release -version

我正在使用 aws/codebuild/docker:17.09.0 图像 运行 我在

中的构建

谢谢

您似乎在构建中使用了 0.1 版构建规范。对于版本 0.1 的构建规范,Codebuild 将 运行 每个构建命令在构建环境中默认 shell 的单独实例中。尝试更改为 0.2 版。它可能会让您的构建工作。

详细文档可以在这里找到: https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec-ref-versions

您可以在每个步骤之间使用带有 && \ 的单阶段命令,但最后一个步骤

每一步都是一个子shell,就像打开一个新的终端window所以当然什么都不会留下...

与其他答案相反,导出的环境变量在 0.2 版 CodeBuild 中的命令之间携带。

但是,与往常一样,导出的变量仅供定义它们的进程和子进程使用。如果您在 shell 脚本中导出一个变量,您是从主 CodeBuild shell 调用,或者在另一种程序风格中修改环境(例如 Python 和 os.env)它不会从顶部可用,因为您生成了一个子进程。

诀窍是

  • 从构建规范中的命令导出变量
  • 获取脚本(运行 它在当前 shell 中内联),而不是为它生成子shell

这两个选项都会影响 CodeBuild shell 中的环境,而不是子进程。

我们可以通过定义一个非常基本的 buildspec.yml
来了解这一点 (export-a-var.sh 就是 export EXPORT_VAR=exported)

version: 0.2

phases:

  install:

    commands:
      - echo "I am running from [=10=]"
      - export PHASE_VAR="install"
      - echo "I am still running from [=10=] and PHASE_VAR is ${PHASE_VAR}"
      - ./scripts/export-a-var.sh
      - echo "Variables exported from child processes like EXPORTED_VAR are ${EXPORTED_VAR:-undefined}"

  build:

    commands:
      - echo "I am running from [=10=]"
      - echo "and PHASE_VAR is still ${PHASE_VAR:-undefined} because CodeBuild takes care of it"
      - echo "and EXPORTED_VAR is still ${EXPORTED_VAR:-undefined}"
      - echo "But if we source the script inline"
      - . ./scripts/export-a-var.sh # note the extra dot
      - echo "Then EXPORTED_VAR is ${EXPORTED_VAR:-undefined}"
      - echo "----- This is the script CodeBuild is actually running ----"
      - cat [=10=]
      - echo -----

这导致了输出(为了清晰起见,我对其进行了一些编辑)

# Install phase
I am running from /codebuild/output/tmp/script.sh
I am still running from /codebuild/output/tmp/script.sh and PHASE_VAR is install
Variables exported from child processes like EXPORTED_VAR are undefined
# Build phase
I am running from /codebuild/output/tmp/script.sh
and PHASE_VAR is still install because CodeBuild takes care of it
and EXPORTED_VAR is still undefined
But if we source the script inline
Then EXPORTED_VAR is exported
----- This is the script CodeBuild is actually running ----

下面我们看到 CodeBuild 实际为 commands 中的每一行执行的脚本;每行都在一个包装器中执行,该包装器保留环境和目录位置并为下一个命令恢复它。因此,影响顶级 shell环境的命令可以将值传递给下一个命令。

cd $(cat /codebuild/output/tmp/dir.txt)
. /codebuild/output/tmp/env.sh
set -a
cat [=12=]
CODEBUILD_LAST_EXIT=$?
export -p > /codebuild/output/tmp/env.sh
pwd > /codebuild/output/tmp/dir.txt
exit $CODEBUILD_LAST_EXIT