Skip to content

Instantly share code, notes, and snippets.

@clarkritchie
Created May 27, 2024 03:01
Show Gist options
  • Save clarkritchie/2f935597b9398a34380e8c9a90005b6f to your computer and use it in GitHub Desktop.
Save clarkritchie/2f935597b9398a34380e8c9a90005b6f to your computer and use it in GitHub Desktop.
name: Lint Code
on:
push:
branches:
- main
jobs:
lint-code:
name: Lint code
runs-on: ubuntu-latest
steps:
- name: Check out the repo (current branch)
uses: actions/checkout@v3
with:
ref: ${{ github.ref }}
- name: Install Terraform
uses: little-core-labs/install-terraform@v2.0.0
with:
version: 1.6.6
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v39
- name: Setup Github
run: |
# user.name/user.email comes from the last commit
git config --global user.name "$(git log -n 1 --pretty=format:%an)"
git config --global user.email "$(git log -n 1 --pretty=format:%ae)"
- name: Rewrites changed files to Terraform's canonical format
run: |
for FILE in ${{ steps.changed-files.outputs.all_changed_files }}; do
echo "${FILE} was changed"
if [ "${FILE##*.}" = "tf" ] || [ "${FILE##*.}" = "tfvars" ]; then
echo "${FILE} is a Terraform file"
terraform fmt ${FILE}
echo "⎷ Committing changes"
git add ${FILE}
git commit -a -m "Terraform formatting cleanup" --allow-empty
fi
done
- name: Display ref information (debugging)
run: |
echo "github.ref: ${{ github.ref }}"
echo "github.head_ref: ${{ github.head_ref }}"
- name: Push changes back to branch
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
force: true
- name: Echo job status (debugging)
if: always()
run: |
echo "Job status is ${{ job.status }}"
- name: Update Github with job status
if: always()
uses: ouzi-dev/commit-status-updater@v2
with:
name: "Terraform formatting status"
status: ${{ job.status }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment