Skip to content

Instantly share code, notes, and snippets.

@Cdaprod
Created May 29, 2024 05:48
Show Gist options
  • Save Cdaprod/c38e91efa59d99770d95817b63c4b1b9 to your computer and use it in GitHub Desktop.
Save Cdaprod/c38e91efa59d99770d95817b63c4b1b9 to your computer and use it in GitHub Desktop.
By setting up customized Git hooks, you can automate validation, testing, dependency updates, and notifications for your MinIO deployment repository. These hooks help maintain code quality and streamline your development workflow. Adjust the examples to fit your specific needs and tools used in your project.

Customized Git hooks can significantly enhance the workflow of your cdaprod/hydrate repository, especially for your MinIO deployment. Here are some tailored hooks you might find useful:

Pre-Commit Hook

A pre-commit hook can ensure that your configuration files (e.g., Docker Compose, environment files) are correctly formatted and validated before a commit.

Example: Validate Docker Compose File

#!/bin/sh
# Pre-commit hook to validate Docker Compose file

# Check for docker-compose
if ! command -v docker-compose > /dev/null 2>&1; then
  echo "docker-compose is not installed. Please install it."
  exit 1
fi

# Validate docker-compose.yml
echo "Validating docker-compose.yml..."
docker-compose -f docker-compose.yml config -q

if [ $? -ne 0 ]; then
  echo "docker-compose.yml is not valid. Please fix the errors."
  exit 1
fi

echo "docker-compose.yml is valid. Proceeding with commit."

Pre-Push Hook

A pre-push hook can run tests and linting on your code before allowing it to be pushed to the remote repository.

Example: Run Tests and Linting

#!/bin/sh
# Pre-push hook to run tests and linting

# Run tests
echo "Running tests..."
pytest

if [ $? -ne 0 ]; then
  echo "Tests failed. Please fix the errors before pushing."
  exit 1
fi

# Run pylint
echo "Running pylint..."
pylint hydrate_minio_weaviate

if [ $? -ne 0 ]; then
  echo "pylint found issues. Please fix them before pushing."
  exit 1
fi

echo "All checks passed. Proceeding with push."

Post-Merge Hook

A post-merge hook can automatically update dependencies or restart services after merging changes.

Example: Update Dependencies

#!/bin/sh
# Post-merge hook to update dependencies

# Check if requirements.txt changed
if git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD | grep -q 'requirements.txt'; then
  echo "requirements.txt changed. Updating dependencies..."
  pip install -r requirements.txt
fi

# Check if docker-compose.yml changed
if git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD | grep -q 'docker-compose.yml'; then
  echo "docker-compose.yml changed. Restarting services..."
  docker-compose down
  docker-compose up -d
fi

echo "Post-merge tasks completed."

Post-Commit Hook

A post-commit hook can notify a monitoring service or log commit details for auditing purposes.

Example: Notify Slack on Commit

#!/bin/sh
# Post-commit hook to notify Slack on commit

# Slack webhook URL
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK"

# Commit message
COMMIT_MESSAGE=$(git log -1 --pretty=%B)

# Payload for Slack
payload="{
  \"text\": \"New commit in cdaprod/hydrate repository:\n$COMMIT_MESSAGE\"
}"

# Send notification to Slack
curl -X POST -H 'Content-type: application/json' --data "$payload" $SLACK_WEBHOOK_URL

Setting Up Git Hooks

To set up these hooks:

  1. Navigate to the .git/hooks directory in your repository:

    cd path/to/your/cdaprod/hydrate/.git/hooks
  2. Create and edit the appropriate hook files (e.g., pre-commit, pre-push, post-merge, post-commit).

  3. Ensure each script is executable:

    chmod +x pre-commit pre-push post-merge post-commit

Sharing Hooks with Your Team

Since Git hooks are not version-controlled, consider the following approach to share hooks with your team:

  1. Create a directory for your hooks in the repository, such as .githooks.

  2. Place your hook scripts in this directory.

  3. Instruct team members to set the core.hooksPath configuration:

    git config core.hooksPath .githooks

Summary

By setting up customized Git hooks, you can automate validation, testing, dependency updates, and notifications for your MinIO deployment repository. These hooks help maintain code quality and streamline your development workflow. Adjust the examples to fit your specific needs and tools used in your project.

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