-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sébastien HOUZÉ
committed
Jan 3, 2021
0 parents
commit 954de9f
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Review terraform plan | ||
|
||
Run and cleanup terraform plan output, store it into an output to allow reviewing it | ||
## Inputs | ||
### `terraform-environment` | ||
|
||
**Required** Terraform environment, used to load corresponding var file named `<terraform-environment>.tfvars` | ||
into the working directory. Default `"preproduction"`. | ||
### `working-directory` | ||
|
||
**Required** Working directory where terraform plan is executed. Default `"."`. | ||
|
||
## Example usage | ||
|
||
|
||
Here | ||
|
||
```yaml | ||
- uses: gogaille/review-terraform-plan | ||
id: terraform-plan | ||
env: | ||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
with: | ||
terraform-environment: 'preproduction' | ||
working-directory: 'infrastructure' | ||
|
||
- uses: phulsechinmay/rewritable-pr-comment@v0.3 | ||
with: | ||
message: ${{ steps.terraform-plan.outputs.plan-details }} | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
COMMENT_IDENTIFIER: 'terraform-plan-output' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
name: Review terraform plan | ||
description: > | ||
Run and cleanup terraform plan output, store it into an output to allow reviewing it. | ||
inputs: | ||
terraform-environment: | ||
description: > | ||
Terraform environment, used to load corresponding var file named | ||
<terraform-environment>.tfvars into the working directory. | ||
required: true | ||
default: "preproduction" | ||
working-directory: | ||
description: working directory | ||
required: true | ||
default: "." | ||
|
||
outputs: | ||
plan-details: | ||
description: > | ||
Clean terraform plan, in HTML format, ready to integrate in a comment of | ||
corresponding pull request. | ||
value: ${{ steps.plan.outputs.plan-details }} | ||
|
||
runs: | ||
using: "composite" | ||
steps: | ||
- id: plan | ||
name: terraform plan for review | ||
shell: bash | ||
working-directory: ${{ inputs.working-directory }} | ||
run: | | ||
plan=$(terraform plan -lock=false -no-color -var-file=${{ inputs.terraform-environment }}.tfvars) | ||
title=$(echo -e "$plan" | grep -e '^Plan:' | sed -E 's|Plan:||') | ||
# remove plan noise | ||
plan=$( | ||
echo -e "$plan" | \ | ||
tail -n +$( | ||
echo -e "$plan" | \ | ||
grep -nE 'Terraform will perform the following actions|No changes. Infrastructure is up-to-date' | \ | ||
cut -d':' -f1 | ||
) | ||
) | ||
message=$(printf " | ||
<details> | ||
<summary><b>Terraform plan:</b> %s %s</summary> | ||
\`\`\`hcl | ||
%s | ||
\`\`\` | ||
</details> | ||
" "${title:-No changes. Infrastructure is up-to-date.}" "$(git rev-parse --short $GITHUB_SHA)" "$plan") | ||
message="${message//'%'/'%25'}" | ||
message="${message//$'\n'/'%0A'}" | ||
message="${message//$'\r'/'%0D'}" | ||
echo -e "$plan" | ||
echo "" | ||
echo ::set-output name=plan-details::"$message" |