Skip to content

Instantly share code, notes, and snippets.

@Maks3w
Created May 23, 2020 12:06
Show Gist options
  • Save Maks3w/fc904b16a6b665b4d3b6d15e28ef57d6 to your computer and use it in GitHub Desktop.
Save Maks3w/fc904b16a6b665b4d3b6d15e28ef57d6 to your computer and use it in GitHub Desktop.
GitHub Action for Terraform Coding Style fixes automatization
name: Terraform Coding Style fixer
description: 'Terraform Coding Style fixer creates a Pull Request with the suggested Coding Style fixes'
on: push
jobs:
terraform-fmt:
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}
- name: Install Terraform CLI
uses: hashicorp/setup-terraform@v1
- name: Execute Terraform Coding Style fixer
id: terraform-fmt-fix
run: terraform fmt -recursive -write=true
- name: Set fixes branch name
if: steps.terraform-fmt-fix.outputs.stdout
id: vars
run: echo ::set-output name=branch-name::"terraform-fmt-action/$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//_/g')"
- name: Create Pull Request
if: steps.terraform-fmt-fix.outputs.stdout
run: |
pr_title="Terrafom Coding Style fixes"
pr_message="This is an auto-generated PR with coding style fixes by \`terraform fmt\`"
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git checkout -B ${{ steps.vars.outputs.branch-name }}
git commit --all \
--message "${pr_title}" \
--message "${pr_message}"
hub pull-request \
--push \
--head ${{ steps.vars.outputs.branch-name }} \
--base ${{ github.ref }} \
--message "${pr_title}" \
--message "${pr_message}" \
--assign ${{ github.actor }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Fail if terraform-fmt made changes
if: steps.terraform-fmt-fix.outputs.stdout
run: exit steps.terraform-fmt-check.outputs.exitcode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment