Git Hooks
Integrate tests using Git Hooks
In this guide, we'll explore how to set up Git hook that triggers a test before a deploy using the Niteco Performance Insights REST API. We'll focus on the /v1/deploys endpoint.
Step 1: Set Up the Git Hook
Navigate to Your Repository:
Open your terminal and navigate to your Git repository.
Create the Git Hook:
Inside your repository, navigate to the .git/hooks directory. Create a new file called pre-push (or use post-commit if you prefer running tests after a commit).
touch .git/hooks/pre-push
chmod +x .git/hooks/pre-push
Step 2: Configure the Git Hook
Edit the Git Hook:
Open the pre-push file in your preferred text editor and add the following script:
#!/bin/sh
# Variables for API request
API_URL="https://perf.niteco.com/api/v1/deploys"
TITLE="your_deploy_title" # Replace with name of your deploy
DETAIL="your_description" # Replace with the description of your deploy
SITE="site_id" # Replace with ID of the site to test
PAGE="page_id" # Replace with ID of the page to test
# Make the API request
RESPONSE=$(curl -s -w "%{http_code}" -o /dev/null \
-G "${API_URL}" \
--data-urlencode "title=${TITLE}" \
--data-urlencode "detail=${DETAIL}" \
--data-urlencode "site=${SITE}" \
--data-urlencode "page=${PAGE}")
# Check the response status
if [ "$RESPONSE" -eq 200 ]; then
echo "Tests ran successfully!"
else
echo "Tests failed with status code: $RESPONSE"
exit 1 # Prevent push if tests fail
fi
Make sure you configure your test variables above:
Step 3: Test the Setup
Make a Commit:
Create a new commit in your repository.
git add .
git commit -m "Test commit for Git hook"
Push the Changes:
git push origin main
Observe the Results:
If the tests are successful, you'll see "Tests ran successfully!" in your terminal. If they fail, the push will be prevented, and you'll see the failure status.
Updated about 1 month ago