Skip to content

Instantly share code, notes, and snippets.

@o-az
Last active July 19, 2024 10:51
Show Gist options
  • Save o-az/776b137cf86f6e6b31c24234d8a32fd6 to your computer and use it in GitHub Desktop.
Save o-az/776b137cf86f6e6b31c24234d8a32fd6 to your computer and use it in GitHub Desktop.
This GitHub Action automatically formats code using Prettier when a pull request is opened or updated. It then commits and pushes the formatted code back to the same PR branch, ensuring consistent code style across contributions without manual intervention.
name: 'Auto Commit Prettier'
on:
pull_request:
branches: ['main']
jobs:
auto-commit:
name: 'Auto Commit'
runs-on: ['ubuntu-latest']
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: 'checkout'
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.event.pull_request.head.ref }}
- name: 'format'
run: npm_config_yes=true npx prettier@latest --write .
- name: 'setup git config'
run: |
git config --global user.name "Acme Bot"
git config --global user.email "username@users.noreply.github.com"
- name: 'commit'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git add . --all
git commit --all --message "chore: prettier auto commit"
git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment