github / branch-deploy

Enabling Branch Deployments through IssueOps with GitHub Actions - If you find this project useful, give it a star! ⭐️
https://github.com/marketplace/actions/branch-deploy
MIT License
384 stars 45 forks source link

Custom template variables possible? #296

Closed cholesterol closed 2 months ago

cholesterol commented 2 months ago

Details

Hey there 👋🏻 ,

I use this action all the time for running Terraform deployments, but I had a question specifically around templates.

It's admittedly been a few months since I've tried this myself, but I remember having to revert to the legacy actions environment workflow due to this requirement. I figured it couldn't hurt to ask if this was a possibility.

What I'd like to do is to pass the output of my plan/applies to a command-line utility like tf-summarize and terramaid to preprocess and format the output of those operations and then pass that input into a template. I can do this manually right now, but would love to be able to inject this output into the templates by passing an environment variable of some type. This would let me render a mermaid diagram of my plan as well as GitHub friendly markdown tables of changes/etc.

I don't know enough about octokit to know if this is even possible but figured it would be worth an ask.

Thanks!

GrantBirki commented 2 months ago

👋 Hey @cholesterol! There are a lot of ways to go about getting custom text/output into the deployment messages that this Action creates. If you use the custom markdown file for deployment messages, you can really go wild with how you generate deployment comments.

Here is an example:

Assuming your .github/deployment_message.md file looks like this:

### Deployment Results {{ ":white_check_mark:" if status === "success" else ":x:" }}

{% if status === "success" %}
**{{ actor }}** successfully{% if noop %} **noop**{% endif %} deployed branch `{{ ref }}` to **{{ environment }}**.
{% else %}
**{{ actor }}** encountered a **failure** when{% if noop %} **noop**{% endif %} deploying branch `{{ ref }}` to **{{ environment }}**!
{% endif %}

<details><summary>Show Results</summary>

<%= results %>

</details>

You can run some logic just after your deployment that updates the contents of <%= results %> with any language of your choosing (bash, ruby, python, js, etc)

In this example I am using Ruby + ERB so <%= results %> might be different if you use something else like jinja (python) or nunjucks (nodejs). The magic sauce here is really just the fact that we are rendering extra text into <%= results %> with Ruby before the workflow completes. This allows us to add whatever we want in addition to the basic branch-deploy template variables that are designated with {{ <variable_name> }}.

Here we can see that I run some custom logic after the terraform apply which takes the output of the apply and injects it into our custom deployment comment template that will get fully rendered and posted as a comment on the PR in the post deployment part of our action workflow.

# .deploy logic
- name: Terraform apply
  if: ${{ steps.branch-deploy.outputs.continue == 'true' && steps.branch-deploy.outputs.noop != 'true' }}
  run: |
    set -o pipefail
    terraform apply -no-color -auto-approve -compact-warnings | tee tfoutput.txt

# this step will format the .github/deployment_message.md file with the terraform output via a ruby script
# in the post deploy step of the branch-deploy Action, it will get its final renderings
- name: terraform output formatting
  if: ${{ steps.branch-deploy.outputs.continue == 'true' }}
  run: bundle exec ruby script/ci/deploy_comment.rb "tfoutput.txt"

Since the branch-deploy Action generates and posts the deployment comment in the post workflow run, you are free to modify the .github/deployment_message.md all you want. The final rendering of it takes place in the post workflow step. During that post workflow step, it will take the contents of your markdown file and do one final rendering with its default template variables. So you can inject as much custom text into that file as you want during your workflow run and it will appear in your final deployment comment.

Here is a quick breakdown of a workflow to help clarify what I mean by "post workflow step":

Image


Let me know if that helps!

cholesterol commented 2 months ago

Hey @GrantBirki - this is super helpful, I think I took a stab at this and couldn't grok whether or not this was possible when I was reading through the source code and how it interacts w/ the the comment in the post-deploy so I gave up and reverted to the historical way.

Thank you so much for the detailed breakdown - I'll circle back once I've tested it out if I run into issues but will otherwise close this out as I feel like this answers my question.

Thanks again!

cholesterol commented 1 month ago

just following up but this worked like a charm just using bash and envsubst to shim in my terraform output. Thanks again for your help @GrantBirki