9

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/[email protected]
      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.

3
  • This is for PRs only or you want the entire CI/CD pipeline and you think this is the place where it errors out? Commented Mar 1, 2022 at 11:01
  • It is for the PR. From the github action web view I can see errors on this step specifically. Commented Mar 1, 2022 at 11:24
  • Ok, and what does the rest of workflows file look like? Are you using Terraform cloud? Commented Mar 1, 2022 at 12:06

2 Answers 2

3

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/[email protected]
        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
            })```
Sign up to request clarification or add additional context in comments.

1 Comment

I came to the same conclusion, I would suggest having a look at the action: marocchino/[email protected] 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.
1

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.

2 Comments

The closing delimiter can't be found if it's not at the beginning of a line. I suggest reading this answer: stackoverflow.com/a/74282465/7388116
That's why my code has echo >> $GITHUB_ENV just before the closing delimiter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.