bash 无法在 AWS Codebuild 中运行 `source`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44810237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot run `source` in AWS Codebuild
提问by Tanishq dubey
I am using AWS CodeBuild along with Terraform for automated deployment of a Lambda based service. I have a very simple buildscript.yml
that accomplishes the following:
我正在使用 AWS CodeBuild 和 Terraform 来自动部署基于 Lambda 的服务。我有一个非常简单的buildscript.yml
实现以下功能:
- Get dependencies
- Run Tests
- Get AWS credentials and save to file (detailed below)
- Source the creds file
- Run Terraform
- 获取依赖
- 运行测试
- 获取 AWS 凭证并保存到文件(详见下文)
- 获取creds文件
- 运行 Terraform
The step "source the creds file" is where I am having my difficulty. I have a simply bash one-liner that grabs the AWS container creds off of curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
and then saves them to a file in the following format:
“来源信用文件”这一步是我遇到困难的地方。我有一个简单的 bash one-liner,它可以获取 AWS 容器凭证curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
,然后将它们保存到以下格式的文件中:
export AWS_ACCESS_KEY_ID=SOMEACCESSKEY
export AWS_SECRET_ACCESS_KEY=MYSECRETKEY
export AWS_SESSION_TOKEN=MYSESSIONTOKEN
Of course, the obvious step is to simply source
this file so that these variables can be added to my environment for Terraform to use. However, when I do source /path/to/creds_file.txt
, CodeBuild returns:
当然,显而易见的步骤是简单地创建source
这个文件,以便将这些变量添加到我的环境中以供 Terraform 使用。但是,当我这样做时source /path/to/creds_file.txt
,CodeBuild 返回:
[Container] 2017/06/28 18:28:26 Running command source /path/to/creds_file.txt
/codebuild/output/tmp/script.sh: 4: /codebuild/output/tmp/script.sh: source: not found
I have tried to install source
through apt
but then I get an error saying that source
cannot be found (yes, I've run apt update
etc.). I am using a standard Ubuntu image with the Python 2.7 environment for CodeBuild. What can I do to either get Terraform working credentials for source this credentials file in Codebuild.
我曾试图安装source
过apt
但后来我得到一个错误,指出source
无法找到(是的,我已经运行apt update
等)。我在 CodeBuild 的 Python 2.7 环境中使用标准 Ubuntu 映像。我该怎么做才能在 Codebuild 中获取 Terraform 工作凭据以获取此凭据文件的源代码。
Thanks!
谢谢!
采纳答案by jeffrey
Try using .
instead of source
. source
is not POSIX compliant. ss64.com/bash/source.html
尝试使用.
而不是source
. source
不符合 POSIX。ss64.com/bash/source.html
回答by sdhillon
The AWS CodeBuild images ship with a POSIX compliant shell. You can see what's inside the images here: https://github.com/aws/aws-codebuild-docker-images.
AWS CodeBuild 映像随附符合 POSIX 标准的 shell。您可以在此处查看图像中的内容:https: //github.com/aws/aws-codebuild-docker-images。
If you're using specific shell features (such as source), it is best to wrap your commands in a script file with a shebang specifying the shell you'd like the commands to execute with, and then execute this script from buildspec.yml.
如果您正在使用特定的 shell 功能(例如源代码),最好将您的命令包装在一个脚本文件中,并使用 shebang 指定您希望执行命令的 shell,然后从 buildspec.yml 执行此脚本.
build-script.sh
构建脚本.sh
#!/bin/bash
<commands>
...
buildspec.yml (snippet)
buildspec.yml(片段)
build:
commands:
- path/to/script/build-script.sh
build:
commands:
- path/to/script/build-script.sh
回答by Joro Tenev
I had a similar issue. I solved it by calling the script directly via /bin/bash <script>.sh
我有一个类似的问题。我通过直接调用脚本来解决它/bin/bash <script>.sh
回答by brajmohan
I don't have enough reputation to comment so here it goes an extension of jeffrey's answer which is on spot.
我没有足够的声誉来发表评论,所以这里是杰弗里现场回答的延伸。
Just in case if your filename starts with a dot(.), the following will fail
以防万一您的文件名以点(.)开头,以下操作将失败
. .filename
You will need to qualify the filename with directory name like
您需要使用目录名称来限定文件名,例如
. ./.filename