Skip to content

Instantly share code, notes, and snippets.

@lolgab
Last active May 30, 2023 17:10
Show Gist options
  • Save lolgab/996405c967da7ba44c9fbc00583ccc5d to your computer and use it in GitHub Desktop.
Save lolgab/996405c967da7ba44c9fbc00583ccc5d to your computer and use it in GitHub Desktop.
scalafmt Github Actions

scalafmt Github Actions

This Github Action runs scalafmt on every Pull Request and then pushes the changes as a new commit on the same branch right away. It runs only on the *.scala and *.sbt files that appear in the Pull Request diff.

This way you can focus on your code and forget about formatting!

name: scalafmt
on:
- pull_request
jobs:
scalafmt:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
- uses: coursier/setup-action@v1
with:
apps: scalafmt
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v36
with:
files: |
**.scala
**.sbt
- name: Run Scalafmt on changed files
if: steps.changed-files.outputs.any_changed == 'true'
run: scalafmt ${{ steps.changed-files.outputs.all_changed_files }}
- name: Check if there are any changes
id: verify-diff
run: git diff --quiet . || echo "changed=true" >> $GITHUB_OUTPUT
- name: Commit files
if: steps.verify-diff.outputs.changed == 'true'
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git commit -a -m "style: Run Scalafmt"
- name: Push changes
if: steps.verify-diff.outputs.changed == 'true'
uses: ad-m/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.head_ref }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment