Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plan too long to be passed between steps #24

Open
joshgubler opened this issue Jun 25, 2020 · 7 comments
Open

Plan too long to be passed between steps #24

joshgubler opened this issue Jun 25, 2020 · 7 comments

Comments

@joshgubler
Copy link

This example in the documentation often breaks down in real life. If the output from terraform plan is too big, it can't fit in an environment variable (which is how the documentation shows it being accessed).

The workaround we have found is to save the plan as a file, pass the filename, and then use terraform show -json $filename > plan.json to get the plan value in the second step. The problem we are having with this workaround is that the wrapper script breaks STDOUT.

@ahmadnassri
Copy link

I've been doing something similar, trying to redirect the output of show to a file doesn't seem to work anymore

- run: terraform plan -out terraform.plan 

- run: terraform show -json terraform.plan
   id: show

- run: |
    cat > terraform.json << 'EOM'
    ${{ steps.show.outputs.stdout }}
    EOM

which worked pretty well for a while, but now I'm getting this error:
image

seems the output is too large for github.

@alexjurkiewicz
Copy link
Contributor

A workaround is to disable the wrapper:

- uses: hashicorp/setup-terraform@master
  with:
    terraform_wrapper: false
- run: terraform plan -out terraform.plan 
- run: terraform show -json terraform.plan | tee terraform.json

I wish the wrapper was disabled by default.

@ahmadnassri
Copy link

I just discovered terraform_wrapper and came ba to post the same comment, thanks @alexjurkiewicz !

@DylanBohlender
Copy link

+1 to @alexjurkiewicz's wish that the wrapper was disabled by default, would've saved me quite a bit of debugging time.

An additional note for anyone leveraging Terraform Cloud - the "remote" backend does not support saving a generated plan locally at this time (you'll get Error: Saving a generated plan is currently not supported), so terraform plan -out is out of the question for that use case.

Unfortunately it seems that including the plan body in the PR comment (as described in the documentation example OP cited) isn't really feasible when your terraform plans can be large and you're managing state in Terraform Cloud. I had high hopes of scripting around the environment variable problem and implementing a file read in github-script like this, but I've thrashed enough on the problem that I'll probably just settle for having the full plans available in the Actions logs.

@anthonyangel
Copy link

anthonyangel commented May 7, 2021

My workspace has 1600 resources (long story), so the plan has 1600 lines in it of Refreshing State..., which meant that I was being affected by this, got round it by saving it to a text file and trimming that, rather than using an env var.

      - name: Terraform Plan
        id: plan
        run: |
          terraform plan -no-color 2>&1 | tee /tmp/plan.txt
          sed -i '/Refreshing state.../d' /tmp/plan.txt
        continue-on-error: true

      - uses: actions/github-script@v4.0.2
        if: github.event_name == 'pull_request'
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const fs = require("fs");
            const plan = fs.readFileSync("/tmp/plan.txt", "utf8");
            const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
            #### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
            #### Terraform Validation 🤖${{ steps.validate.outputs.stdout }}
            #### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
            
            <details><summary>Show Plan</summary>
            
            \`\`\`${ plan }\`\`\`
            </details>
            
            *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`;

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

@euqen
Copy link

euqen commented Feb 17, 2022

@anthonyangel thanks for your script. But this will not work if plan is really long. Therefore I added this:

            const MAX_GITHUB_COMMENT_LENGTH = 65536;

            if (plan.length > MAX_GITHUB_COMMENT_LENGTH) {
              plan = "Please review the plan in github actions logs because it's too long"
            }

alternatively it's possible to shorten the plan to fit the max length but I'm too lazy to do so :D

@phyzical
Copy link

i know its old, but what worked for me was a combination of @anthonyangel and @euqen, difference with my approach is that we split on changes or no changes then again on any warnings to get "the middle"

 - name: Terraform Plan
    id: plan
    working-directory: ${{ matrix.FOLDER }}
    run: |
      terraform plan $vars -lock=false -no-color -input=false 2>&1 | tee /tmp/plan.txt
  const fs = require("fs");
  const plan = fs.readFileSync("/tmp/plan.txt", "utf8");
  let plan_cleaned = plan.split(/(Terraform used the selected providers|No changes)/)[1].split("Warning")[0]
  if (plan_cleaned.length == 0 ) {
    plan_cleaned = plan
  }

  const MAX_GITHUB_COMMENT_LENGTH = 65536 - 100;

  plan_cleaned = plan_cleaned.slice(0, MAX_GITHUB_COMMENT_LENGTH)

  if (plan_cleaned.length == MAX_GITHUB_COMMENT_LENGTH) {
    plan_cleaned =+ "...\nPlease review the plan in github actions logs because it's too long" 
  }

  // 2. Prepare format of the comment
  const output = `## ${{ matrix.ENVIRONMENT }}
  <details><summary>Show Plan</summary>

  \`\`\`\n
  ${plan_cleaned}
  \`\`\`

  </details>`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants