Skip to content

Lesson 2 of 6

Add a post-commit test

Write a test script that checks the page text and add it as a Git post-commit hook.

13 minDevOps

After this lesson you will be able to write a small test script that checks your site's content, and run it automatically after every commit.

Why test on commit?

A static site is a set of files that a web server sends to visitors exactly as they are. If a file is missing or has the wrong content, the site breaks. You can catch many mistakes by checking the files after each commit. A test script does this for you.

Git has a feature called hooks: scripts that run at certain points in the Git workflow. The post-commit hook runs after a commit is made. You can use it to run your tests automatically.

Write a test script

You will write a script that checks that the home page contains the heading and paragraph you wrote in lesson 1. Create a file called test.sh in your my-site folder:

touch test.sh

Open test.sh in your editor and add these lines:

#!/bin/bash
set -e

if ! grep -q "Welcome to My Site" index.html; then
  echo "Error: heading not found in index.html"
  exit 1
fi

if ! grep -q "This is a simple static website." index.html; then
  echo "Error: paragraph not found in index.html"
  exit 1
fi

echo "All tests passed"

The first line tells the system to run this file with Bash. set -e makes the script stop if any command fails. The grep command searches for text in a file. The -q option makes it quiet, so it only returns success or failure. The ! in front of grep means "if the command fails". So the first if checks that the heading is not in index.html. If it is not there, the script prints an error and exits with code 1, which means failure. The second if does the same for the paragraph. If both checks pass, the script prints "All tests passed".

Make the script executable:

chmod +x test.sh

Run it to see that it works:

./test.sh

You should see All tests passed.

Add the post-commit hook

Git stores hooks in the .git/hooks folder. The post-commit hook is a file called post-commit in that folder. If the file exists and is executable, Git runs it after each commit.

Create the hook file:

touch .git/hooks/post-commit

Open .git/hooks/post-commit in your editor and add:

#!/bin/bash
./test.sh

This tells Git to run your test script after each commit. Make the hook executable:

chmod +x .git/hooks/post-commit

Now make a small change to index.html to test the hook. Change the paragraph text to something else, for example:

<p>This is a simple static website. Updated.</p>

Stage and commit the change:

git add index.html
git commit -m "Update paragraph"

After the commit, you should see the output from the test script: All tests passed. The hook ran automatically.

Now change the paragraph back to the original text and commit again:

<p>This is a simple static website.</p>
git add index.html
git commit -m "Revert paragraph"

Again, the test runs and passes.

What if a test fails?

To see what happens when a test fails, temporarily change the heading in index.html to something else, for example:

<h1>Welcome</h1>

Commit the change:

git add index.html
git commit -m "Break heading"

The commit still happens, but the hook prints an error: Error: heading not found in index.html. The commit is recorded, but you know there is a problem. Change the heading back to Welcome to My Site and commit again:

<h1>Welcome to My Site</h1>
git add index.html
git commit -m "Fix heading"

The test passes again.

Keep the hook in your repository

The .git/hooks folder is not tracked by Git, so if you clone the repository elsewhere, the hook will not be there. For this course, you work on one machine, so that is fine. If you want to share the hook, you can put the script in a tracked folder and have the hook call it. You already have test.sh tracked, so you could change the hook to call ./test.sh from the repository root. That is what you did. The hook file itself remains untracked, but it is a small file that calls your tracked script.

You have now added an automatic test that runs after every commit. In the next lesson, you will package the site into an archive for deployment.

Key takeaways

  • A post-commit hook is a script in .git/hooks/post-commit that Git runs after each commit.
  • The hook can call a test script that checks your site files.
  • Use grep -q to check for text in a file without printing output.
  • A failing test does not undo the commit, but it tells you something is wrong.
  • The hook file itself is not tracked by Git, but it can call a tracked script.

Try it

Add a test to test.sh that checks that the about.html page you created in the lesson 1 exercise contains the heading "About". Run the script to make sure it passes. Then make a temporary change to about.html that removes the heading, commit it, and observe that the hook fails. Finally, restore the heading, commit again, and confirm the test passes. You should see the error message from your new test when the heading is missing, and "All tests passed" after you fix it.

Official documentation

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.

    Add a post-commit test | Deploy a Static Site: Commit to Local Production | Rolebound