0
0
Gitdevops~30 mins

Automated testing on push in Git - Mini Project: Build & Apply

Choose your learning style9 modes available
Automated Testing on Push with Git
📖 Scenario: You work on a small project where you want to automatically run tests every time you push code to your Git repository. This helps catch errors early and keeps your code healthy.
🎯 Goal: Set up a Git pre-push hook that runs a test script automatically before pushing changes. If tests fail, the push will be stopped.
📋 What You'll Learn
Create a Git hook script file named pre-push inside the .git/hooks directory
Make the pre-push script executable
Write a command inside pre-push to run the test script ./run_tests.sh
Ensure the push is blocked if tests fail (non-zero exit code)
Print a message Tests failed, push aborted. if tests fail
💡 Why This Matters
🌍 Real World
Automated testing on push helps developers catch errors early and maintain code quality in real projects.
💼 Career
Many software teams use Git hooks to automate checks and enforce quality before code is shared with others.
Progress0 / 4 steps
1
Create the Git pre-push hook script file
Create a file named pre-push inside the .git/hooks directory using the command touch .git/hooks/pre-push.
Git
Need a hint?

Use the touch command to create an empty file.

2
Make the pre-push hook script executable
Make the pre-push file executable by running chmod +x .git/hooks/pre-push.
Git
Need a hint?

Use chmod +x to add execute permission.

3
Write the test command inside the pre-push hook
Edit the .git/hooks/pre-push file to add the following lines exactly:
#!/bin/sh
./run_tests.sh || { echo "Tests failed, push aborted."; exit 1; }
Git
Need a hint?

Use a shell script that runs ./run_tests.sh and stops push if it fails.

4
Test the pre-push hook by pushing changes
Run git push to test the pre-push hook. If ./run_tests.sh fails, you should see Tests failed, push aborted. and the push should stop.
Git
Need a hint?

Run git push and observe the output message if tests fail.