Skip to content

Instantly share code, notes, and snippets.

@franklsm1
Last active June 19, 2024 15:11
Show Gist options
  • Save franklsm1/d15dd2020772fb6ca65a375956bf27c2 to your computer and use it in GitHub Desktop.
Save franklsm1/d15dd2020772fb6ca65a375956bf27c2 to your computer and use it in GitHub Desktop.
Auto configure pre-commit hooks for a Gradle Spring App

Steps

  1. create a file called "pre-commit" with no extension in the project directory root
  2. add the code from the pre-commit file below into that new file
  3. create a new file called "preCommit.gradle" inside of the root gradle folder
  4. add the code from the preCommit.gradle file below into that new file
  5. add the following line at the end of the build.gradle file:
    • apply from: "$projectDir/gradle/preCommit.gradle"

Now when you run the ./gradlew build command the git pre-commit hook will be automatically setup. This will prevent commits from being committed without all the tests passing locally.

Note: to skip this hook add the --no-verify flag to the git commit command

#!/bin/bash
echo "Running git pre-commit hook"
./gradlew clean build
RESULT=$?
# return 1 exit code if running checks fails
[ $RESULT -ne 0 ] && exit 1
exit 0
task installGitHook(type: Copy) {
from new File(projectDir, 'pre-commit')
into { new File(projectDir, '.git/hooks') }
fileMode 0777
}
assemble.dependsOn installGitHook
@henrico-lazuroz
Copy link

Thanks for the instructions!

I'm using pre-commit script to run ktfmtFormat task, preventing ktfmtCheck fails on PR validation job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment