bri*_*fey 5 jq terraform github-actions
我有一个 GitHub Actions 工作流程,可以从 terraform 配置读取输出。我正在尝试这样做:
terraform -chdir=terraform/live/dev output -json > /tmp/output.json
APP_URL=$(cat /tmp/output.json | jq -r '.app_url.value')
Run Code Online (Sandbox Code Playgroud)
我在 GitHub 操作日志中收到以下错误:
parse error: Invalid numeric literal at line 1, column 9
Run Code Online (Sandbox Code Playgroud)
我添加了以下内容来调试:
parse error: Invalid numeric literal at line 1, column 9
Run Code Online (Sandbox Code Playgroud)
我发现输出是cat /tmp/output.json:
# debugging output.json file
echo "output.json:"
cat /tmp/output.json
Run Code Online (Sandbox Code Playgroud)
这告诉我jq无法解析我写入 terraform JSON 输出的临时文件,因为它似乎将命令添加到文件本身:
/home/runner/work/_temp/2b622f60-be99-4a29-a295-593b06dde9a8/terraform-bin -chdir=terraform/live/dev output -json
{
"app_url": {
"sensitive": false,
"type": "string",
"value": "https://app.example.com"
}
}
Run Code Online (Sandbox Code Playgroud)
如何获取 JSON 形式的 terraform 输出并将其写入文件,而不使用导致解析错误的额外标题行?
当我在本地运行相同的命令时,我没有收到此解析错误。
以下是产生此错误的 GitHub Action 工作流程部分的代码:https://github.com/briancaffey/django-step-by-step/blob/main/.github/workflows/terraform_frontend_update.yml#L72- L74
cd terraform/live/dev而不是-chdir=terraform/live/dev- 这导致了同样的错误我可以通过向操作添加以下设置来解决此问题setup-terraform/@v1:
- uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.1.7
terraform_wrapper: false <-- added this
Run Code Online (Sandbox Code Playgroud)
有关此设置的更多文档可以在这里找到: https: //github.com/hashicorp/setup-terraform#inputs
虽然 @briancaffey 的答案完全正确,但如果您需要将其用于terraform_wrapper工作流程的其他部分(例如使用输出),您可以使用terraform-bin切换有问题的 terraform 调用。
如果您还想在 GitHub Actions 之外运行脚本,可以使用以下解决方法:
tf=terraform-bin
type "$tf" >/dev/null 2>&1 || tf=terraform
$tf output -json > /tmp/output.json
Run Code Online (Sandbox Code Playgroud)
有关提到相同解决方法的问题,请参阅https://github.com/hashicorp/setup-terraform/issues/167#issuecomment-1090760365 。
此问题也在v3.0.0 中得到修复。
| 归档时间: |
|
| 查看次数: |
1973 次 |
| 最近记录: |