Skip to content

Commit

Permalink
feat(ci): output simplified version of the tf plan to display in acti…
Browse files Browse the repository at this point in the history
…on + PR comment (#50)

* feat(plan): output simplified version of the tf plan to display

remove sed to debug

typo const should be var

add sed to filter out more pre-amble

* style: use camelCase for var
  • Loading branch information
georgepstaylor authored Jan 20, 2025
1 parent 9e3087a commit eefdac4
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions .github/workflows/terraform-plan-and-apply-aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,15 @@ jobs:
- name: Terraform Plan JSON Output
run: |
terraform -chdir=${{ inputs.terraform-dir }} show -json tfplan > tfplan.json
- name: Simple Plan Output
run: |
terraform show -no-color tfplan | tee tfplan-simple
sed -i -e '1,/Terraform will perform the following actions:/d' -e '/^[[:space:]]*$/d' tfplan-simple
if grep -q 'Plan:' tfplan-simple; then
plan_changes=$(grep 'Plan:' tfplan-simple)
sed -i '/Plan:/d' tfplan-simple
echo "${plan_changes}" > tfplan-summary
fi
- name: Upload tfplan
uses: actions/upload-artifact@v4
with:
Expand Down Expand Up @@ -592,20 +601,18 @@ jobs:
// 2. Check output length
const fs = require("fs");
const plan_exists = fs.existsSync("tfplan.stdout");
if (plan_exists) {
const plan = fs.readFileSync("tfplan.stdout", "utf8");
const excludedStrings = ["Terraform used the selected providers", "No changes", "Refreshing state...", "Reading...", "Read complete after"];
const filteredLines = plan.split('\n').filter(line =>
!excludedStrings.some(excludedStr => line.includes(excludedStr))
);
var planOutput = filteredLines.join('\n').trim();
const planRegex = /Plan: (\d+) to add, (\d+) to change, (\d+) to destroy\./;
const planMatch = planOutput.match(planRegex);
var planSummary = "Not found, please view the workflow run logs directly."
if (planMatch) {
planSummary = `${planMatch[1]} to add, ${planMatch[2]} to change, ${planMatch[3]} to destroy.`
const planExists = fs.existsSync("tfplan-simple");
if (planExists) {
var planOutput = fs.readFileSync("tfplan-simple", "utf8");
const planSummaryExists = fs.existsSync("tfplan-summary");
if (planSummaryExists) {
var planSummaryContents = fs.readFileSync("tfplan-summary", "utf8");
const planSummaryRegex = /Plan: (\d+) to add, (\d+) to change, (\d+) to destroy\./;
const planSummaryMatch = planSummaryContents.match(planSummaryRegex);
var planSummaryMsg = `${planSummaryMatch[1]} to add, ${planSummaryMatch[2]} to change, ${planSummaryMatch[3]} to destroy.`
} else {
var planSummaryMsg = "No changes detected."
}
const MAX_GITHUB_COMMENT_LENGTH = 65536 - 800; // 800 characters for the comment template
Expand All @@ -614,8 +621,8 @@ jobs:
}
} else {
const plan_non_existant = "Terraform Plan Output File not found, please view the workflow run logs directly."
planOutput = plan_non_existant
planSummary = plan_non_existant
var planOutput = plan_non_existant
planSummaryMsg = plan_non_existant
}
// 3. Prepare format of the comment
Expand All @@ -639,7 +646,7 @@ jobs:
\`\`\`
</details>
<b>Plan Summary:</b> ${planSummary}
<b>Plan Summary:</b> \`${planSummaryMsg}\`
*<b>Pusher:</b> @${{ github.actor }}, <b>Action:</b> \`${{ github.event_name }}\`*
*<b>Workflow Run Link:</b> ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}*`;
Expand Down

0 comments on commit eefdac4

Please sign in to comment.