【问题标题】:Extracting terraform resource changes with github-script@v5 action使用 github-script@v5 操作提取 terraform 资源更改
【发布时间】:2022-01-13 06:42:37
【问题描述】:

我有一个 github 工作流,我想在其中过滤 terraform 计划文件中的 terraform destroy 更改并将其作为评论发布在 PR 中。

    - name: Terraform Plan
      id: plan_json
      run: |
        terraform plan -out planfile 2>error.log
        terraform show -json planfile > plan.json
      continue-on-error: true

    - uses: actions/github-script@v5
      id: message
      if: ${{ always() }}
      with:
        result-encoding: string
        script: |
          const fs = require('fs');
          const report = JSON.parse(fs.readFileSync('./plan.json'));

          var message = '';
          for (const changes in report.resource_changes) {
            message += `${changes.change.actions[0]} ${changes.name} (${changes.type})\n`
          };

          console.log('Message: ', message);
          return message;

当我运行工作流时,它给出了这个错误:

SyntaxError: Unexpected token c in JSON at position 1
    at JSON.parse (<anonymous>)
    at eval (eval at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4942:56), <anonymous>:4:21)
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4943:12)
Error: Unhandled error: SyntaxError: Unexpected token c in JSON at position 1
    at main (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4997:26)
    at Module.272 (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4981:1)
    at __webpack_require__ (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:24:31)
    at startup (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:43:19)
    at /home/runner/work/_actions/actions/github-script/v5/dist/index.js:49:18
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:52:10)
    at Module._compile (internal/modules/cjs/loader.js:959:30)

我没有任何 nodejs/javascript 经验,所以我不知道我在这里做错了什么。

实际的计划文件可以在here找到。

当我在本地运行 nodejs script 时,它可以工作。

❯ node tfplan.js
Message:  create, rg (azurerm_resource_group)
create, rg-name (random_pet)

【问题讨论】:

  • 错误读起来像 fs.readFileSync('./plan.json') 是不返回 JSON。尝试将 fs.readFileSync 的结果捕获到一个变量中,然后将此变量传递给 JSON.parse。您应该能够对此进行调试以检查文件读取的结果是否实际上是有效的 JSON。
  • 嗨@phuzi 感谢您的回复!在这里 (pastebin.com/d27xH19p) 你可以看到 plan.json 的确切输出,当我用 jq 查询它时它工作正常。
  • 我还是建议拆分JSON.parse(fs.readFileSync('./plan.json')) 和控制台记录fs.readFileSync('./plan.json') 的结果以确认内容符合预期。

标签: javascript node.js json github-actions


【解决方案1】:
    - uses: hashicorp/setup-terraform@v1
      with:
        terraform_wrapper: false

terraform_wrapper: false 必须添加到 terraform 设置任务中。 更多信息请查看this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-02
    • 2023-02-14
    • 1970-01-01
    • 2020-03-26
    • 2020-09-07
    • 2018-06-30
    • 2022-10-19
    • 1970-01-01
    相关资源
    最近更新 更多