robburger / terraform-pr-commenter

A GitHub Action that adds opinionated comments to a PR from Terraform fmt/init/plan output
MIT License
132 stars 65 forks source link

Add working-directory to the comment #17

Open wolverian opened 3 years ago

wolverian commented 3 years ago

Thank you for the great action! We're running it in production now. 🙂

I have a feature request that would help us: we're running this action for a bunch of modules in one repo, i.e. our repo looks something like this:

root
- vpc
- dns
- eks
- ...etc...

We use an Actions matrix to run the same steps for each directory. This works! 🎉 However, the comment for each init/plan/apply doesn't indicate which module it came from:

Screenshot 2021-06-29 at 10 42 26

This could be fixed by adding the working directory to the message, so it would be e.g. <directory>: Terraform <action> <result> for workspace <workspace>. Another option would be to add the directory to commenter_input, but I haven't tried this yet, as I've assumed that argument should contain only output from Terraform itself.

Does this sound like a sensible feature? Thank you!

robburger commented 3 years ago

@wolverian - sorry for the delay, and yes, excellent suggestion!

I recently ran into this problem myself with comments not being deleted due to the title lookup in a matrix run, but the folder problem is also relevant.

MPV commented 3 years ago

As inspiration, https://github.com/iStreamPlanet/github-actions/tree/main/terraform-actions does (or I should say "did", that action seems non-maintained nowadays) the equivalent with a working directory input to the action.

Whichever of the suggested solutions above sound promising (and would be what I'd need in order to switch from iStreamPlanet into this action). ❤️

marcellovictorino commented 2 years ago

Great suggestion @MPV As a workaround, I've been using the TF_WORKSPACE variable to keep track of environment and working directory:

- name: >
    Post Plan >> Env: ${{ matrix.project.env }} | Dir: ${{ matrix.project.dir }}
  if: always() && github.ref != 'refs/heads/master' && (steps.plan.outcome == 'success' || steps.plan.outcome == 'failure')
  uses: robburger/terraform-pr-commenter@v1
  env:
    TF_WORKSPACE: ${{ format('Env= {0} | Dir= {1}', matrix.project.env, matrix.project.dir) }}
  with:
    commenter_type: plan
    commenter_input: ${{ format('{0}{1}', steps.plan.outputs.stdout, steps.plan.outputs.stderr) }}
    commenter_exitcode: ${{ steps.plan.outputs.exitcode }}

Which looks like this. Not great... not terrible...

image

:warning: Although, I noticed the action sometimes updates the wrong comment, removing a valid one.

JensRantil commented 1 year ago

Worth pointing out that the TF_WORKSPACE workaround doesn't work for commenter_type init nor validate - the WORKSPACE variable in the script is only used for commenter_type=="plan". This also needs to be fixed.

JensRantil commented 1 year ago

Inspiration for fixing this: I notice that https://github.com/thollander/actions-comment-pull-request uses a hidden HTML comment to distinguish between comments. That might be a pretty nice implementation. We could also simply delegate the commenting to https://github.com/thollander/actions-comment-pull-request (using a composite action) if we don't want to implement the logic ourselves.