From 954de9f2f9865736a1156f8b17b8aa361bdc0bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20HOUZ=C3=89?= Date: Sun, 3 Jan 2021 21:37:13 +0100 Subject: [PATCH] Initial commit --- README.md | 33 +++++++++++++++++++++++++++++ action.yaml | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 README.md create mode 100644 action.yaml diff --git a/README.md b/README.md new file mode 100644 index 0000000..226a5c3 --- /dev/null +++ b/README.md @@ -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 `.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' +``` diff --git a/action.yaml b/action.yaml new file mode 100644 index 0000000..f9d418b --- /dev/null +++ b/action.yaml @@ -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 + .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 " +
+ Terraform plan: %s %s + + \`\`\`hcl + %s + \`\`\` + +
+ " "${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"