Ale*_*dru 7 continuous-integration json environment-variables terraform github-actions
我的项目使用 Terraform 来设置 CI/CD 的基础设施和 Github Actions。运行后,terraform apply
我想将 Terraform 输出变量的值保存为 Github Action 环境变量,以供工作流程稍后使用。
根据 Github Action 的文档,这是使用工作流命令创建或更新环境变量的方法。
这是我简化的 Github Action 工作流程:
name: Setup infrastructure
jobs:
run-terraform:
name: Apply infrastructure changes
runs-on: ubuntu-latest
steps:
...
- run: terraform output vm_ip
- run: echo TEST=$(terraform output vm_ip) >> $GITHUB_ENV
- run: echo ${{ env.TEST }}
Run Code Online (Sandbox Code Playgroud)
当本地运行时,命令echo TEST_VAR=$(terraform output vm_ip)
输出准确TEST="192.168.23.23"
,但从 Github Action CLI 输出中我得到一些非常奇怪的东西:
我尝试过使用单引号、双引号。在某些时候我改变了策略并尝试使用jq
. 因此,我添加了以下步骤,以便将所有 Terraform 输出导出到 json 文件并使用以下命令解析它jq
:
- run: terraform output -json >> /tmp/tf.out.json
- run: jq '.vm_ip.value' /tmp/tf.out.json
Run Code Online (Sandbox Code Playgroud)
但现在它抛出以下错误:
parse error: Invalid numeric literal at line 1, column 9
Run Code Online (Sandbox Code Playgroud)
即使生成的 JSON 完全有效:
- run: terraform output -json >> /tmp/tf.out.json
- run: jq '.vm_ip.value' /tmp/tf.out.json
Run Code Online (Sandbox Code Playgroud)
命令terraform output -json >> /tmp/tf.out.json
并jq '.vm_ip.value' /tmp/tf.out.json
在本地相应地工作。
Ale*_*dru 13
经过几个小时的搜索,我终于弄清楚了。
Terraform 的 Github Action似乎提供了一个名为 的附加参数,如果您计划在命令中使用输出,terraform_wrapper
则需要将其设置为。您可以在此处false
阅读更深入的文章。
否则,它们将自动暴露给该步骤的输出,并且可以像steps.<step_id>.outputs.<variable>
. 您可以在此处和此处阅读有关它们的更多信息。
归档时间: |
|
查看次数: |
8213 次 |
最近记录: |