在GitHub Actions中遇到“参数列表过长”的问题

7

我正在跟随HashiCorp的学习指南学习如何设置GitHub Actions和terraform。除了更新Terraform Plan的步骤之外,一切运行良好。

我遇到了以下错误:


An error occurred trying to start process '/home/runner/runners/2.287.1/externals/node12/bin/node' with working directory '/home/runner/work/ccoe-aws-ou-scp-manage/ccoe-aws-ou-scp-manage'. Argument list too long


我使用的代码是:
    - uses: actions/github-script@0.9.0
      if: github.event_name == 'pull_request'
      env:
        PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
        script: |
          const output = `#### Terraform Format and Style \`${{ steps.fmt.outcome }}\`
          #### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
          #### Terraform Plan \`${{ steps.plan.outcome }}\`
          <details><summary>Show Plan</summary>
          \`\`\`${process.env.PLAN}\`\`\`
          </details>
          *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
            
          github.issues.createComment({
            issue_number: context.issue.number,
            owner: context.repo.owner,
            repo: context.repo.repo,
            body: output
          })

清晰地从文档中复制粘贴:https://learn.hashicorp.com/tutorials/terraform/github-actions

我已经尝试了版本5和6的actions/github-script,仍然存在相同的问题,但是当我复制黏贴计划时一切都很好。如果我不使用输出变量并为主体使用一些占位符文本,则一切正常。我可以看到step.plan.outputs.stdout如果我只打印那个。

如有需要,我将乐意分享更多细节。


这仅适用于PR,或者您想要整个CI/CD流水线,并且认为这是出错的地方? - Marko E
这是为了PR。从Github Action的Web视图中,我可以看到特定步骤上的错误。 - Zambozo
好的,那么 workflows 文件的其余部分是什么样子的?您是否在使用 Terraform Cloud? - Marko E
2个回答

3
我也遇到了类似的问题。看起来github-script不能为过长的脚本提供参数。
参考资料: 我的回答:
      - name: truncate terraform plan result
        run: |
          plan=$(cat <<'EOF'
          ${{ format('{0}{1}', steps.plan.outputs.stdout, steps.plan.outputs.stderr) }}
          EOF
          )
          echo "${plan}" | grep -v 'Refreshing state' >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV

      - name: create comment from plan result
        uses: actions/github-script@0.9.0
        if: github.event_name == 'pull_request'
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const output = `#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
            #### Terraform Plan \`${{ steps.plan.outcome }}\`
            
            <details><summary>Show Plan</summary>
            
            \`\`\`\n
            ${ process.env.PLAN }
            \`\`\`
            
            </details>
            
            *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ inputs.TF_WORK_DIR }}\`, Workflow: \`${{ github.workflow }}\`*`;

            github.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: output
            })```

1
我得出了相同的结论,建议看一下这个操作:marocchino/sticky-pull-request-comment@v2.1.0 它支持许多好功能,例如更新评论,不会创建太多评论,可以减少痛苦。感谢你提出的截断想法,我会尝试你正在做的事情。 - Zambozo

0

基于 @Zambozo 在评论中的提示,这对我非常有效:

    - name: Terraform Plan
      id: plan
      run: terraform plan -no-color -input=false

    - name: generate random delimiter
      run: echo "DELIMITER=$(uuidgen)" >> $GITHUB_ENV

    - name: truncate terraform plan result
      run: |
        echo "PLAN<<${{ env.DELIMITER }}" >> $GITHUB_ENV
        echo '[maybe truncated]' >> $GITHUB_ENV
        tail --bytes=10000 <<'${{ env.DELIMITER }}' >> $GITHUB_ENV
        ${{ format('{0}{1}', steps.plan.outputs.stderr, steps.plan.outputs.stdout) }}
        ${{ env.DELIMITER }}
        echo >> $GITHUB_ENV
        echo "${{ env.DELIMITER }}" >> $GITHUB_ENV

    - name: post plan as sticky comment
      uses: marocchino/sticky-pull-request-comment@v2
      with:
        header: plan
        message: |
          #### Terraform Plan \`${{ steps.plan.outcome }}\`
          <details><summary>Show Plan</summary>

          ```
          ${{ env.PLAN }}
          ```

          </details>

值得注意的是,GitHub对评论大小有上限,因此只显示计划的最后10kB(显示摘要和警告)。

这还实现了安全的heredoc定界符,以避免恶意输出转义。

还请注意,在消息中的三个反引号之前和之后的空行对于保持格式的完整性非常重要。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接