Swapnil Thamake
Swapnil Thamake

Reputation: 9

Why isn't My Git pre-commit Hook Working with Spring Boot and Gradle?

How to implement Git pre-commit/pre-push hook in Spring boot for gradle project?

I have implemented at my project but its not working. Step 1: Created a pre-hook script, let's create pre-commit file inside a new scripts directory.

Below script is been configured for pre-commit

#!/bin/sh

echo "*****Running unit tests******"

git stash -q --keep-index

./gradlew test

status=$?

git stash pop -q

echo "*****Done with unit tests******"

exit $status

Step 2: Next, create a Gradle task in the build.gradle file to install the pre-commit script. Why do we need it? because, we want this to run on all developers' machine, not just on our machine, we all love consistency and wants to put the constraints

Gradle task will run whenever someone takes the build

task installLocalGitHook(type: Copy){
from new File(rootProject.rootDir, 'scripts/pre-commit')
into { new File(rootProject.rootDir, '.git/hooks')}
fileMode 0775
}

build.dependsOn installLocalGitHook

When build the project above Gradle task won't be run and pre-commit script won't be copied to .git/hooks/ directory.

But, If I run the task manually form grade file it works and copied pre-commit script to .git/hooks/ director

References:

  1. https://dev.to/akdevcraft/git-pre-hook-pre-commit-with-gradle-task-134m
  2. https://gist.github.com/franklsm1/d15dd2020772fb6ca65a375956bf27c2

I have try to implement pre-commit hook but it's not work as expected.

Upvotes: 0

Views: 603

Answers (1)

Sean Franklin
Sean Franklin

Reputation: 247

Use assemble.dependsOn installLocalGitHook instead of build.dependsOn installLocalGitHook.

Assemble is a lifecycle task and will be executed when you run your build command.

Upvotes: 1

Related Questions