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

No output in "Show Plan" #15

Closed
brettcurtis opened this issue Oct 8, 2020 · 16 comments
Closed

No output in "Show Plan" #15

brettcurtis opened this issue Oct 8, 2020 · 16 comments
Assignees

Comments

@brettcurtis
Copy link

brettcurtis commented Oct 8, 2020

Thanks for the guide! I'm trying to get the GitHub Script stuff to work but getting no outputs:

image

    # Generates an execution plan for Terraform
    - name: Terraform Plan
      id: plan
      run: terraform plan -no-color
      continue-on-error: true

    - name: Update Pull Request
      uses: actions/[email protected]
      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
            })
@im2nguyen
Copy link
Collaborator

Hi @brettcurtis , thanks for going through the guide!

Hrm... this is strange, did this trigger a speculative plan in Terraform Cloud?

Could you try re-running the GH Action? If you go to the "Actions" tab in your repo and select the latest run, there should be a "Re-run jobs" button on the top right.

image

@brettcurtis
Copy link
Author

I created what it think is a simple test here, let me know what you think: https://github.com/lzysh/test-plan-output/runs/1232224004?check_suite_focus=true

I'm expecting line 39/40 would output something?
https://github.com/lzysh/test-plan-output/blob/master/.github/workflows/terraform.yml#L39

Also troubleshooting over here in this issue: hashicorp/setup-terraform#7

@im2nguyen im2nguyen self-assigned this Oct 9, 2020
@im2nguyen
Copy link
Collaborator

Hey @brettcurtis , you just need to add this segment into your steps (preferably after you checkout the code).

- name: Setup Terraform
  uses: hashicorp/setup-terraform@v1

Here's a successful action: im2nguyen/test-plan-output#4

@brettcurtis
Copy link
Author

Ok thanks, I used your example and it's working!

@im2nguyen im2nguyen pinned this issue Oct 10, 2020
@im2nguyen
Copy link
Collaborator

Pinning this issue because others could be facing similar problems while setting up their own pipelines

@supratim-persistent
Copy link

It is still now working for me. I mean under 'Show Plan', if I click on expand, no plan output shows up. Here is the snippet from my workflow yml

  • uses: actions/checkout@v2
    with:
    fetch-depth: 0

    - name: Setup Terraform
      uses: hashicorp/setup-terraform@v1
      with:
        terraform_version: 0.12.28
        #cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}   
       
    - name: Set Source ref for main branch
      run: echo "SOURCE_REF=origin/master" >> $GITHUB_ENV
      
    - name: INSTALL UTILITIES
      run: |
        echo "Installing binaries"
        cd helpers;chmod 755 *;./install.sh
        
        echo "SOURCE_REF=" $SOURCE_REF
        echo "GITHUB_REF is" $GITHUB_REF
        echo "GITHUB_SHAGITHUB_HEAD_REF" $GITHUB_HEAD_REF      
        
    - name: RUN VALIDATIONS ON MASTER
      id: validation
      run: |
        pwd;ls -lrt;cd helpers
        ./deploy.sh "$REGION" "$SOURCE_REF" "$GITHUB_HEAD_REF" validate
        
    - name: RUN PLAN ON MASTER
      id: plan
      run: |
        pwd;ls -lrt;cd helpers
        ./deploy.sh "$REGION" "$SOURCE_REF" "$GITHUB_HEAD_REF" plan -no-color
      continue-on-error: true
        
    - name: Update PR with validation results
      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 Validate 🖌\`${{ steps.validation.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
          })
          
    - name: Terraform Plan Status
      if: steps.plan.outcome == 'failure'
      run: exit 1 
    

@im2nguyen
Copy link
Collaborator

Hi @supratim-persistent, is your Terraform plan step error-ing? Can you try printing: steps.plan.outputs.stdout?

I'm not familiar with the contents of your deploy.sh file

@supratim-persistent
Copy link

supratim-persistent commented Oct 13, 2020

Hi @im2nguyen,
Thanks for taking time to look into this. My terraform plan is not erroring out, actually my deploy.sh is running terragrunt plan which is a wrapper on top of terraform and if you see the last below these are the first and last few lines from the "RUN PLAN ON MASTER" step which means output from that step is fine and not erroring out.

----------------------
[infrastructure-deployer] time="2020-10-13T14:13:58Z" level=info msg="Invoking Lambda function ecs-deploy-runner-invoker to trigger deployment."
[infrastructure-deployer] time="2020-10-13T14:13:59Z" level=info msg="Successfully invoked Lambda function ecs-deploy-runner-invoker to trigger deployment."
...
...
[ecs-deploy-runner][2020-10-13T14:16:54+0000]         tags                     = {}
[ecs-deploy-runner][2020-10-13T14:16:54+0000]     }
[ecs-deploy-runner][2020-10-13T14:16:54+0000] Plan: 0 to add, 1 to change, 0 to destroy.
[ecs-deploy-runner][2020-10-13T14:16:54+0000] ------------------------------------------------------------------------
[ecs-deploy-runner][2020-10-13T14:16:54+0000] Note: You didn't specify an "-out" parameter to save this plan, so Terraform
[ecs-deploy-runner][2020-10-13T14:16:54+0000] can't guarantee that exactly these actions will be performed if
[ecs-deploy-runner][2020-10-13T14:16:54+0000] "terraform apply" is subsequently run.
[ecs-deploy-runner][2020-10-13T14:16:54+0000] [INFO] [infrastructure-deploy-script] 2020-10-13 14:16:54  "terragrunt plan" exited with code 0
[infrastructure-deployer] time="2020-10-13T14:17:06Z" level=info msg="Deployment finished without error."
-----------------------------

I also added a separate step like you said to display the output from this RUN PLAN ON MASTER step, but that is not showing any output either, does this snippet only work with terraform plan?

  - name: Terraform Plan Status
    if: steps.plan.outcome == 'failure'
    run: exit 1 
        
  - name: Get Result
    run: echo "OUTPUT=${{ steps.plan.outputs.stdout }}"     --- this prints no values as you can see below

OUTPUT=
Will really appreciate any help in printing the plan output under "Show Plan"

@im2nguyen
Copy link
Collaborator

im2nguyen commented Oct 13, 2020

Hi @supratim-persistent Thank you for your explanation, this makes a little bit more sense.

The hashicups/setup-terraform GitHub action retrieves the stdout, stderr and exitcode from a Terraform run. This can be seen in the source here.

To export outputs for GH actions to use, you need to use the set-output parameter. This is included in the setup-terraform action here.

Because you're using the terragrunt binary instead of the terraform binary, it doesn't export these outputs. This gives you two options:

  1. Use the Terragrunt GitHub Action. This is not an endorsement... it's just what I found while searching on Google
  2. Manually set up set-output for your GitHub action workflow. This should like the code snippet below (haven't tested it yet)
- name: RUN PLAN ON MASTER
  id: plan
  run: |
    pwd;ls -lrt;cd helpers
    echo "::set-output name=stdout::$(./deploy.sh "$REGION" "$SOURCE_REF" "$GITHUB_HEAD_REF" plan -no-color)"
  continue-on-error: true

@supratim-persistent
Copy link

Thank you for your inputs, it has made some improvements but showing the output is still not working completely. Now based on what you suggested, it is printing only the first line of the stdout output under 'Show Validations' and not all the lines upto the end. Here is my yml for your reference, will appreciate very much if you could suggest any workaround to print all the lines instead of just the first line.


name: CI-ON-MASTER

on:
pull_request:
branches:
- "master"
types: [opened, synchronize, reopened]

env:
GRUNTWORK_INSTALLER_VERSION: v0.0.22
MODULE_CI_VERSION: v0.27.2
MODULE_SECURITY_VERSION: v0.24.1
REGION: us-east-1
GITHUB_OAUTH_TOKEN: ${{ secrets.TOKEN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

jobs:

RUN-PLAN-ON-MASTER:

runs-on: ubuntu-latest

steps:
  
  - uses: actions/checkout@v2
    with:
      fetch-depth: 0
      
  - name: Setup Terraform
    uses: hashicorp/setup-terraform@v1
    with:
      terraform_version: 0.12.28
      #cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}   
     
  - name: Set Source ref for main branch
    run: echo "SOURCE_REF=origin/master" >> $GITHUB_ENV
    
  - name: INSTALL UTILITIES
    run: |
      echo "Installing binaries"
      cd helpers;chmod 755 *;./install.sh
      
      echo "SOURCE_REF=" $SOURCE_REF
      echo "GITHUB_REF is" $GITHUB_REF
      echo "GITHUB_SHAGITHUB_HEAD_REF" $GITHUB_HEAD_REF
      which aws-auth
      which perl
      id
      sudo perl -p -i -e 's/43200/3600/g' /usr/local/bin/aws-auth
      id
            
      
  - name: RUN VALIDATIONS ON MASTER
    id: validation
    run: |
      pwd;ls -lrt;cd helpers
      echo "::set-output name=stdout::$(./deploy.sh "$REGION" "$SOURCE_REF" "$GITHUB_HEAD_REF" validate -no-color)"
      
      
  - name: RUN PLAN ON MASTER
    id: plan
    run: |
      pwd;ls -lrt;cd helpers
      ./deploy.sh "$REGION" "$SOURCE_REF" "$GITHUB_HEAD_REF" plan -no-color
    continue-on-error: true
      
  - name: Update PR with validation results
    uses: actions/[email protected]
    if: github.event_name == 'pull_request'
    env:
      PLAN: "${{ steps.plan.outputs.stdout }}"
      VALIDATE: "\n${{ steps.validation.outputs.stdout }}"
    with:
      github-token: ${{ secrets.GITHUB_TOKEN }}
      script: |
        const output = `#### Terraform Validate 🖌\`${{ steps.validation.outcome }}\`
        #### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
        
        <details><summary>Show Validation</summary>\`\`\`${process.env.VALIDATE}\`\`\`</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
        })
      result-encoding: string
        
  - name: Terraform Plan Status
    if: steps.plan.outcome == 'failure'
    run: exit 1 
    
  - name: Get Result
    run: echo "OUTPUT=${{ steps.validation.outputs.stdout }}"

@im2nguyen
Copy link
Collaborator

Can you try using the Terragrunt GitHub Actions instead? Otherwise, you would have to create a new GitHub action that uses an OutputListener. This is how the setup-terraform GH action works, attached is the source code: https://github.com/hashicorp/setup-terraform/blob/master/wrapper/terraform.js

@ppawiggers
Copy link

ppawiggers commented Oct 22, 2020

I had the same issue (also hashicorp/setup-terraform#7). If you use the setup-terraform action, it sets up a wrapper which makes sure the output is captured (and can be used in a PR comment).

I don't use setup-terraform because I have a self-hosted runner with Terraform installed. My solutions is to write the plan log to a file and load it in the next step (that writes the PR comment):

- id: plan
  run: terraform plan -no-color > plan.log

- uses: actions/github-script@v3
  with:
    github-token: ${{ secrets.GITHUB_TOKEN }}
    script: |
      const fs = require('fs');
      const plan = fs.readFileSync("plan.log");

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

@supratim-persistent
Copy link

@ppawiggers Here is my snippet below, how do I incorporate your part as mentioned above in the below snippet, I tried a few ways but my workflow fails due to syntax error in the yaml if I try and incorporate your snippet, let me know if you are able to display the output of plan.log within the below snippet

- name: Update PR with validation results
        uses: actions/[email protected]
        if: github.event_name == 'pull_request'
        env:
          PLAN: "${{ steps.plan.outputs.stdout }}"
          VALIDATE: "${{ steps.validation.outputs.stdout }}"
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const output = `#### Terraform Validate 🖌\`${{ steps.validation.outcome }}\`
            #### Terraform Plan 📖\`${{ steps.plan.outcome }}\`            
            
            
            *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
            })

@ksrisurapaneni
Copy link

Having same issue, Show plan is not showing any output. Can someone please help?

@bcarranza
Copy link

It was great!!

@vinodpop
Copy link

This is fixed when you add the below entry in your setup

  • uses: hashicorp/setup-terraform@v2
    with:
    terraform_version: 1.1.9
    terraform_wrapper: true

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