Skip to content

Commit

Permalink
Drift mode have step summary (#37)
Browse files Browse the repository at this point in the history
* Drift mode have step summary

* Drift mode have step summary

* Drift mode have step summary

* Drift mode have step summary

* Drift mode have step summary

* Drift mode have step summary

* Drift mode have step summary

* Drift mode have step summary

* Drift mode have step summary

* Split files

* Split files

* Split files

* Split files

* Fix summary file

* Fix summary file
  • Loading branch information
goruha authored Oct 10, 2023
1 parent eee8f9c commit 4ebb7b3
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 19 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/test-changes-exists-drift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ jobs:
FILE_EXISTS=$?
echo "file_exists=${FILE_EXISTS}" >> $GITHUB_OUTPUT
test -f ./metadata/step-summary-plat-ue2-sandbox-foobar_changes.md
FILE_EXISTS=$?
echo "file_summary_md_exists=${FILE_EXISTS}" >> $GITHUB_OUTPUT
test -f ./metadata/issue-description-plat-ue2-sandbox-foobar_changes.md
FILE_EXISTS=$?
echo "file_md_exists=${FILE_EXISTS}" >> $GITHUB_OUTPUT
Expand All @@ -92,6 +96,11 @@ jobs:
actual: "${{ steps.metadata.outputs.file_md_exists }}"
expected: "0"

- uses: nick-fields/assert-action@v1
with:
actual: "${{ steps.metadata.outputs.file_summary_md_exists }}"
expected: "0"

- uses: nick-fields/assert-action@v1
with:
actual: "${{ fromJSON(needs.test.outputs.summary) }}"
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test-changes-exists.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ jobs:
[![create](https://shields.io/badge/PLAN-CREATE-success?style=for-the-badge)](#user-content-create-plat-ue2-sandbox-foobar_changes)
<details><summary><a id="result-plat-ue2-sandbox-foobar_changes" />Plan: 1 to add, 0 to change, 0 to destroy.</summary>
<br/>
To reproduce this locally, run:<br/><br/>
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test-failed-plan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ jobs:
exit status 1
```
To reproduce this locally, run:<br/><br/>
Expand All @@ -110,6 +110,7 @@ jobs:
</details>
teardown:
runs-on: ubuntu-latest
needs: [assert]
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/test-infra-cost.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ jobs:
[![create](https://shields.io/badge/PLAN-CREATE-success?style=for-the-badge)](#user-content-create-plat-ue2-sandbox-foobar_changes)
<details><summary><a id="result-plat-ue2-sandbox-foobar_changes" />Plan: 1 to add, 0 to change, 0 to destroy.</summary>
<br/>
To reproduce this locally, run:<br/><br/>
Expand Down Expand Up @@ -122,7 +123,7 @@ jobs:
Plan: 1 to add, 0 to change, 0 to destroy.
```
</details>
</details>
Expand All @@ -135,6 +136,8 @@ jobs:
```
</details>
teardown:
runs-on: ubuntu-latest
Expand Down
41 changes: 26 additions & 15 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,19 @@ runs:
PLAN_FILE="$GITHUB_WORKSPACE/${{ inputs.component-path}}/$COMPONENT_SLUG-${{ inputs.sha }}.planfile"
LOCK_FILE="$GITHUB_WORKSPACE/${{ inputs.component-path}}/.terraform.lock.hcl"
if [[ "${{ inputs.drift-detection-mode-enabled }}" == "true" ]]; then
TEMPLATE_FILE="${{ github.action_path }}/config/github_issue_description.yaml"
SUMMARY_FILE="$GITHUB_WORKSPACE/metadata/issue-description-${COMPONENT_SLUG}.md"
else
TEMPLATE_FILE="${{ github.action_path }}/config/atmos_github_summary.yaml"
SUMMARY_FILE="$GITHUB_WORKSPACE/metadata/step-summary-${COMPONENT_SLUG}.md"
fi
ISSUE_SUMMARY_FILE="$GITHUB_WORKSPACE/metadata/issue-description-${COMPONENT_SLUG}.md"
STEP_SUMMARY_FILE="$GITHUB_WORKSPACE/metadata/step-summary-${COMPONENT_SLUG}.md"
SUMMARY_FILE="$GITHUB_WORKSPACE/metadata/summary-${COMPONENT_SLUG}.md"
echo "stack_name=${STACK_NAME}" >> $GITHUB_OUTPUT
echo "component_name=${COMPONENT_NAME}" >> $GITHUB_OUTPUT
echo "component_slug=${COMPONENT_SLUG}" >> $GITHUB_OUTPUT
echo "plan_file=${PLAN_FILE}" >> $GITHUB_OUTPUT
echo "lock_file=${LOCK_FILE}" >> $GITHUB_OUTPUT
echo "template_file=${TEMPLATE_FILE}" >> $GITHUB_OUTPUT
echo "summary_file=${SUMMARY_FILE}" >> $GITHUB_OUTPUT
echo "step_summary_file=${STEP_SUMMARY_FILE}" >> $GITHUB_OUTPUT
echo "issue_file=${ISSUE_SUMMARY_FILE}" >> $GITHUB_OUTPUT
- name: Configure Plan AWS Credentials
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
Expand All @@ -175,11 +173,11 @@ runs:
env:
ATMOS_BASE_PATH: ${{ github.workspace }}
run: |
set +e
set +e
TERRAFORM_OUTPUT=$( \
tfcmt \
--config ${{ steps.vars.outputs.template_file }} \
--config ${{ github.action_path }}/config/summary.yaml \
-owner "${{ github.repository_owner }}" \
-repo "${{ github.event.repository.name }}" \
-var "target:${{ steps.vars.outputs.component_slug }}" \
Expand All @@ -191,7 +189,8 @@ runs:
-var "logoImage:${{ inputs.branding-logo-image }}" \
-var "logoUrl:${{ inputs.branding-logo-url }}" \
-var "infracost_enabled:${{ inputs.enable-infracost }}" \
--output "${{ steps.vars.outputs.summary_file }}" \
-var "driftModeEnabled:${{ inputs.drift-detection-mode-enabled }}" \
--output ${{ steps.vars.outputs.summary_file }} \
--log-level $([[ "${{ inputs.debug }}" == "true" ]] && echo "DEBUG" || echo "INFO") \
plan -- \
atmos terraform plan ${{ inputs.component }} \
Expand All @@ -205,6 +204,14 @@ runs:
set -e
if [[ "${{ inputs.drift-detection-mode-enabled }}" == "true" ]]; then
grep -Pzo '(.|\n)*(?=_______________ISSUE-MARKDOWN_______________\n)' ${{ steps.vars.outputs.summary_file }} > ${{ steps.vars.outputs.step_summary_file }}
grep -Pzo '(?<=_______________ISSUE-MARKDOWN_______________\n)(.*\n)*' ${{ steps.vars.outputs.summary_file }} > ${{ steps.vars.outputs.issue_file }}
rm -f ${{ steps.vars.outputs.summary_file }}
else
mv ${{ steps.vars.outputs.summary_file }} ${{ steps.vars.outputs.step_summary_file }}
fi
echo -ne "${TERRAFORM_OUTPUT}"
HAS_CHANGES=false
Expand Down Expand Up @@ -367,8 +374,8 @@ runs:
INFRA_COST_SUMMARY="Infracost Estimate: monthly cost will increase by ${INFRACOST_DIFF_TOTAL_MONTHLY_COST} 📈"
fi
sed -i "s/%INFRACOST_SUMMARY%/${INFRA_COST_SUMMARY}/" ${{ steps.vars.outputs.summary_file }}
sed -i -e '/%INFRACOST_DIFF%/{r /tmp/infracost.txt' -e 'd}' ${{ steps.vars.outputs.summary_file }}
sed -i "s/%INFRACOST_SUMMARY%/${INFRA_COST_SUMMARY}/" ${{ steps.vars.outputs.step_summary_file }}
sed -i -e '/%INFRACOST_DIFF%/{r /tmp/infracost.txt' -e 'd}' ${{ steps.vars.outputs.step_summary_file }}
- name: Store Component Metadata to Artifacts
Expand All @@ -382,7 +389,11 @@ runs:
shell: bash
working-directory: ./${{ inputs.component-path }}
run: |
STEP_SUMMARY_FILE="${{ steps.vars.outputs.summary_file }}"
if [[ "${{ inputs.drift-detection-mode-enabled }}" == "true" ]]; then
STEP_SUMMARY_FILE="${{ steps.vars.outputs.issue_file }}"
else
STEP_SUMMARY_FILE="${{ steps.vars.outputs.step_summary_file }}"
fi
if [[ "${{ steps.atmos-plan.outputs.no-changes }}" == "true" ]]; then
rm -f ${STEP_SUMMARY_FILE}
Expand Down
1 change: 1 addition & 0 deletions config/atmos_github_summary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ templates:
</details>
{{- end}}
{{- end}}
branding: |
{{- if ne .Vars.logoImage "" }}
<a href="{{.Vars.logoUrl}}"><img src="{{.Vars.logoImage}}" width="100px" align="right"/></a>
Expand Down
198 changes: 198 additions & 0 deletions config/summary.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# tfcmt Configuration used for posting Terraform GitHub Summaries
# https://suzuki-shunsuke.github.io/tfcmt/config
embedded_var_names: []
ci:
owner: []
repo: []
sha: []
link: []
vars: {}
templates:
issue_plan_title: |
{{ if eq .ExitCode 1 }}
## :x: Drift Detection Failed for `{{.Vars.component}}` in `{{.Vars.stack}}`!
{{ end }}
{{- if eq .ExitCode 0 }}
## Drift Detected for `{{.Vars.component}}` in `{{.Vars.stack}}`
{{ end }}
summary_plan_title: |
{{- if eq .ExitCode 1 }}
## :x: Plan Failed for `{{.Vars.component}}` in `{{.Vars.stack}}`
{{- end }}
{{- if eq .ExitCode 0 }}
## Changes Found for `{{.Vars.component}}` in `{{.Vars.stack}}`
{{- end }}
badge: |
{{- if eq .ExitCode 0 }}
{{- if .CreatedResources}}[![create](https://shields.io/badge/PLAN-CREATE-success?style=for-the-badge)](#user-content-create-{{.Vars.target}}){{ end }}
{{- if .UpdatedResources }} [![change](https://shields.io/badge/PLAN-CHANGE-important?style=for-the-badge)](#user-content-change-{{.Vars.target}}){{ end }}
{{- if .ReplacedResources }} [![replace](https://shields.io/badge/PLAN-REPLACE-critical?style=for-the-badge)](#user-content-replace-{{.Vars.target}}){{ end }}
{{- if .DeletedResources }} [![destroy](https://shields.io/badge/PLAN-DESTROY-critical?style=for-the-badge)](#user-content-destroy-{{.Vars.target}}){{ end }}
{{- if not (or .CreatedResources .UpdatedResources .ReplacedResources .DeletedResources) }} [![no changes](https://shields.io/badge/-NO_CHANGE-inactive?style=for-the-badge)](#user-content-{{.Vars.target}}){{ end }}
{{ end }}
result: |
{{- if eq .ExitCode 0 }}
<details><summary><a id="result-{{.Vars.target}}" />{{if .Result}}{{ .Result }}{{end}}</summary>
{{- end }}
{{- if eq .ExitCode 1 }}
<details><summary><a id="result-{{.Vars.target}}" />:warning: Error summary</summary>
{{- end }}
<br/>
{{ template "error_messages" .}}
To reproduce this locally, run:<br/><br/>
```shell
atmos terraform plan {{.Vars.component}} -s {{.Vars.stack}}
```
{{ if eq .ExitCode 0 }}
{{ template "updated_resources" .}}
{{- end }}
</details>
updated_resources: |
---
{{- if .CreatedResources}}
### <a id="create-{{.Vars.target}}" />Create
```diff
{{- range .CreatedResources}}
+ {{.}}
{{- end}}
```
{{- end}}
{{- if .UpdatedResources}}
### <a id="change-{{.Vars.target}}" />Change
```diff
{{- range .UpdatedResources}}
~ {{.}}
{{- end}}
```
{{- end}}
{{- if .ReplacedResources}}
### <a id="replace-{{.Vars.target}}" />Replace
```diff
{{- range .ReplacedResources}}
- {{.}}
+ {{.}}
{{- end}}
```
{{- end}}
{{- if .DeletedResources}}
### <a id="destroy-{{.Vars.target}}" />Destroy
```diff
{{- range .DeletedResources}}
- {{.}}
{{- end}}
```
{{ end}}
deletion_warning: |
{{if .HasDestroy}}
### :warning: Resource Deletion will happen :warning:
This plan contains resource delete operation. Please check the plan result very carefully!
{{end}}
change_inside_terraform: |
{{- if .ChangedResult}}
<details><summary>Terraform <strong>Plan</strong> Summary</summary>
{{wrapCode .ChangedResult}}
</details>
{{- end}}
change_outside_terraform: |
{{if .ChangeOutsideTerraform}}
<details><summary>:information_source: Objects have changed outside of Terraform</summary>
_This feature was introduced from [Terraform v0.15.4](https://github.com/hashicorp/terraform/releases/tag/v0.15.4)._
{{wrapCode .ChangeOutsideTerraform}}
</details>
{{end}}
warning: |
{{if .Warning}}
## :warning: Warnings :warning:
{{wrapCode .Warning}}
{{end}}
error_messages: |
{{ if eq .ExitCode 1 }}
{{wrapCode .Result }}
{{end}}
{{if .ErrorMessages}}
## :warning: Errors
{{range .ErrorMessages}}
* {{. -}}
{{- end}}{{end}}
infracost: |
{{- if eq .ExitCode 0 }}
{{- if eq .Vars.infracost_enabled "true" }}
<details><summary>%INFRACOST_SUMMARY%</summary>
```
%INFRACOST_DIFF%
```
</details>
{{- end}}
{{- end}}
metadata: |
<details><summary>Metadata</summary>
```json
{
"component": "{{.Vars.component}}",
"stack": "{{.Vars.stack}}",
"componentPath": "{{.Vars.componentPath}}",
"commitSHA": "{{.Vars.commitSHA}}"
}
```
</details>
branding: |
{{- if ne .Vars.logoImage "" }}
<a href="{{.Vars.logoUrl}}"><img src="{{.Vars.logoImage}}" width="100px" align="right"/></a>
{{- end }}
terraform:
plan:
disable_label: false
template: |
{{- template "summary_plan_title" .}}
{{- template "branding" .}}
{{- template "badge" .}}
{{- template "deletion_warning" .}}
{{- template "result" .}}
{{- template "change_inside_terraform" .}}
{{- template "change_outside_terraform" .}}
{{- template "warning" .}}
{{- template "infracost" .}}
{{- if eq .Vars.driftModeEnabled "true" }}
_______________ISSUE-MARKDOWN_______________
{{template "issue_plan_title" .}}
{{template "branding" .}}
{{template "badge" .}}
{{template "deletion_warning" .}}
{{template "result" .}}
{{template "change_inside_terraform" .}}
{{template "change_outside_terraform" .}}
{{template "warning" .}}
{{template "metadata" .}}
{{- end }}
when_parse_error:
template: |
{{template "plan_title" .}}
<details><summary>Result</summary>
{{wrapCode .CombinedOutput}}
</details>
{{- if eq .Vars.driftModeEnabled "true" }}
_______________ISSUE-MARKDOWN_______________
{{template "plan_title" .}}
<details><summary>Result</summary>
{{wrapCode .CombinedOutput}}
</details>
{{- end }}

0 comments on commit 4ebb7b3

Please sign in to comment.