5

I am following HashiCorp's learning guide on how to set up GitHub Actions and terraform. All is running great besides the step to update the PR with the Terraform Plan.

I am hitting the following error:


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

The code I am using is:

    - 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
          })

A clear COPY/Paste from the docs: https://learn.hashicorp.com/tutorials/terraform/github-actions

I have tried with actions/github-script version 5 and 6 and still the same problem, But when I copy paste the plan all is great. If I do not use the output variable and use some place holder text for the body all is working great. I can see that the step.plan.outputs.stdout is Ok if I print only that.

I will be happy to share more details if needed.

tobias
  • 934
  • 8
  • 17
Zambozo
  • 456
  • 4
  • 12
  • This is for PRs only or you want the entire CI/CD pipeline and you think this is the place where it errors out? – Marko E Mar 01 '22 at 11:01
  • It is for the PR. From the github action web view I can see errors on this step specifically. – Zambozo Mar 01 '22 at 11:24
  • Ok, and what does the rest of workflows file look like? Are you using Terraform cloud? – Marko E Mar 01 '22 at 12:06

2 Answers2

2

I also encountered a similar issue. I seem github-script can't give to argument for too long script.

reference:

my answer:

      - 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
    I came to the same conclusion, I would suggest having a look at the action: marocchino/sticky-pull-request-comment@v2.1.0 It supports a lot of good features like updating the comment and not creating too many and it can be less of a pain. Thanks for the truncate idea, will try out what you are doing. – Zambozo Mar 04 '22 at 10:09
0

Based on @Zambozo's hint in the comments, this worked for me great:

    - 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>

Notably GitHub does have an upper limit on comment size, so this only displays the last 10kB of the plan (showing the summary and warnings).

This also implements secure heredoc delimiting to avoid malicious output escaping.

Also note that the empty lines before and after the triplebacktics in the message are significant to avoid destroying the formatting.

David Schmitt
  • 58,259
  • 26
  • 121
  • 165