docker 未在AWS CODEBUILD上设置环境变量

xkrw2x1b  于 2023-02-18  发布在  Docker
关注(0)|答案(4)|浏览(120)

我尝试在AWS代码构建期间设置一些环境变量作为构建步骤的一部分。这些变量没有被设置,下面是一些日志:

[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...

所以你会注意到,无论我如何设置一个变量,当我回显它的时候,它总是空的。
以上是使用此buildspec生成的

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映像来运行我的构建版本
谢谢

xpszyzbs

xpszyzbs1#

您在生成中使用的似乎是0.1版生成规范。对于0.1版的生成规范,Codebuild将在生成环境中默认 shell 程序的单独示例中运行每个生成命令。请尝试更改为0.2版。它可能会使您的生成正常工作。
详细文档可在此处找到:https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec-ref-versions

ycl3bljg

ycl3bljg2#

与其他答案相反,在0.2版CodeBuild中,导出的环境变量在命令之间传递。
然而,和往常一样,导出的变量只对定义它们的进程和子进程可用。如果您在从CodeBuild主shell调用的shell脚本中导出变量,或者在另一种风格的程序(例如Python和os.env)中修改环境,则它将无法从顶部使用,因为您生成了一个子进程。
诀窍在于

  • 从buildspec中的命令导出变量
  • 获取脚本(在当前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 $0"
      - export PHASE_VAR="install"
      - echo "I am still running from $0 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 $0"
      - 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 $0
      - 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中的每一行执行的脚本;每一行都在一个 Package 器中执行, Package 器保存环境和目录位置,并为下一个命令恢复它。2因此,影响顶层shell环境的命令可以将值携带到下一个命令。

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

cbjzeqam3#

您可以在每个步骤(最后一步除外)之间使用带有&& \的单阶段命令
每一步都是一个子shell,就像打开一个新的终端窗口一样,所以当然什么都不会停留...

vsikbqxv

vsikbqxv4#

如果在yml中使用exit,导出的变量将为空。例如:

version 0.2
env:
  exported-variables:
    - foo

phases:
  install:
    commands:
      - export foo='bar'
      - exit 0

如果你期望foobar,你会惊讶地发现foo是空的,我认为这是aws codebuild的一个bug。

相关问题