You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Action中terraform validate执行失败,报错信息晦涩

解决GitHub Action中Terraform Validate的TypeError问题

问题详情

执行terraform validate时触发以下错误:

Run terraform validate -no-color
/home/runner/work/_temp/12cb240b-5a9f-44d7-9df4-0c2e0c97750d/terraform-bin validate -no-color
/home/runner/work/_temp/12cb240b-5a9f-44d7-9df4-0c2e0c97750d/terraform:4211
  core.setOutput('exitcode', exitCode.toString(10));
                                      ^

TypeError: Cannot read properties of null (reading 'toString')
    at /home/runner/work/_temp/12cb240b-5a9f-44d7-9df4-0c2e0c97750d/terraform:4211:39
Error: Process completed with exit code 1.

当前使用的GitHub Action配置.github/workflows/terraform_pr.yml:

name: Terraform PR Check

on: [pull_request]

jobs:
  terraform:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: HashiCorp - Setup Terraform
        uses: hashicorp/setup-terraform@v2
        with:
          terraform_version: 1.3.4


      - name: Terraform fmt
        id: fmt
        run: terraform fmt -check -recursive

      - name: Terraform Init
        id: init
        run: terraform init -backend=false

      - name: Terraform Validate
        id: validate
        run: terraform validate -no-color

补充背景:两个月前的提交可正常通过检查,基于该提交创建测试分支并仅新增foo.txt文件后,PR仍触发相同错误。

错误原因分析

该错误源于hashicorp/setup-terraform@v2的脚本逻辑缺陷:当Terraform命令执行时,脚本未能正确捕获退出码(exitCode为null),导致调用toString()方法时抛出异常。这类问题通常与action版本兼容性、GitHub Runner环境变更有关。

解决建议

1. 升级setup-terraform action版本

v2版本存在已知的类似问题,升级到v3+稳定版可修复脚本逻辑问题。修改配置中的action版本:

- name: HashiCorp - Setup Terraform
  uses: hashicorp/setup-terraform@v3
  with:
    terraform_version: 1.3.4

2. 验证Terraform命令执行环境

确保工作目录下存在Terraform配置文件(.tf),可在validate步骤前增加目录检查:

- name: Check Terraform files
  run: ls -la

3. 显式指定Terraform工作目录

若Terraform配置不在仓库根目录,需在init和validate步骤中添加-chdir=<路径>参数:

- name: Terraform Init
  id: init
  run: terraform init -backend=false -chdir=./terraform

- name: Terraform Validate
  id: validate
  run: terraform validate -no-color -chdir=./terraform

4. 指定旧版GitHub Runner镜像

如果是Runner环境更新导致的兼容性问题,可指定使用旧版Ubuntu镜像:

jobs:
  terraform:
    runs-on: ubuntu-22.04
    # 其他步骤保持不变

内容的提问来源于stack exchange,提问作者oschlueter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 13:41:02