Hey there, fellow developers! 👋
Today I want to share a cool automation I just built that's made my team's life so much easier. You know that feeling when you're deep in coding, and suddenly realize you missed a PR that's been sitting there for hours? Yeah, been there too many times. So I decided to fix this by setting up automatic PR notifications in our team's Slack channel.
The Problem I Was Trying to Solve 🤔
In my organization, we were constantly missing PRs or catching them late. It was becoming a real pain point - someone would create a PR and then have to manually ping the team in Slack. Not ideal, right? I wanted something that would automatically notify us as soon as a PR was created.
🚀 Step-by-Step Implementation
1. Setting Up the Development Environment
First, I encountered an issue with Python package installation:
error: externally-managed-environment
Solution: Created a virtual environment to manage dependencies independently:
python3 -m venv venv
source venv/bin/activate
pip install flask requests python-dotenv
2. Environment Configuration (.env)
Two crucial pieces needed:
- SLACK_WEBHOOK_URL: For sending notifications to Slack
- GITHUB_WEBHOOK_SECRET: For securing webhook endpoints
Getting Slack Webhook URL:
- Went to api.slack.com/apps
- Created a new app
- Activated Incoming Webhooks which is present under the features
- Added webhook to my workspace channel
Creating GitHub Webhook Secret:
- Generated a secure random string
- Used this as the secret key for webhook verification
3. Moving from Development to Production
Initially, I started with ngrok for development, but for production, I've moved to a more robust setup using EC2 and Nginx. Here's how:
Setting Up EC2 Instance
- Created a dedicated EC2 instance
- Set up security groups:
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw enable
Installing and Configuring Nginx
# Install Nginx
sudo apt update
sudo apt install nginx
# Create Nginx configuration
sudo nano /etc/nginx/sites-available/github-slack
Nginx configuration:
server {
listen 80;
server_name your_ec2_public_ip;
location / {
proxy_pass http://localhost:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Enable the configuration:
sudo ln -s /etc/nginx/sites-available/github-slack /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx
4. GitHub Webhook Configuration
Critical Settings:
- Payload URL: Now using EC2 public IP (e.g., http://your-ec2-ip/webhook)
- Content Type: application/json
- Secret: Added the same secret from .env file
Production Setup:
- Using EC2 public IP or domain name
- Configured proper security groups
- Set up monitoring and logging
5. Making It Production-Ready
Created a systemd service for reliability:
[Unit]
Description=GitHub PR Notification Service
After=network.target
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/github-slack-automation
Environment="PATH=/home/ubuntu/github-slack-automation/venv/bin"
ExecStart=/home/ubuntu/github-slack-automation/venv/bin/python app.py
Restart=always
[Install]
WantedBy=multi-user.target
Start and enable the service:
sudo systemctl daemon-reload
sudo systemctl start github-slack
sudo systemctl enable github-slack
6. Security Considerations
- Implemented webhook signature verification
- Used environment variables for sensitive data
- Added proper error handling and logging
- Set up firewall rules
- Regularly update system packages
🎓 Key Learnings
- Environment Management: Virtual environments are crucial for Python projects
- Production Deployment: Moving from ngrok to EC2/Nginx taught me about real-world deployment
- Security First: Always verify webhook signatures and protect sensitive data
- Monitoring Matters: Proper logging and monitoring are essential in production
🚧 Future Improvements I Plan For
- Add more detailed PR information in Slack messages
- Implement PR status updates
- Add SSL/HTTPS support using Let's Encrypt
- Set up automated backups
- Implement rate limiting
🔗 Code Repository
For the complete implementation, check out my GitHub repository: https://github.com/Haripriya2408/slack-pr-automation
📝 Conclusion
This automation project significantly improved our team's PR workflow. While setting it up had its challenges, particularly with environment management and production deployment, the end result is a reliable system that keeps our team informed about new PRs instantly.
The journey from a development setup with ngrok to a production-ready system with EC2 and Nginx taught me a lot about real-world deployment challenges and solutions.
Remember to:
- Replace the EC2 IP with your actual production URL
- Keep your webhook secrets secure! 🔐
- Regularly monitor logs and update dependencies
- Back up your configuration files
Happy coding! If you have any questions or suggestions, feel free to reach out! 👋
Debugging Tips
If you run into issues:
- Check application logs:
sudo journalctl -u github-slack -f
- Check Nginx logs:
sudo tail -f /var/log/nginx/error.log
- Verify webhook delivery in GitHub repository settings
- Ensure all environment variables are properly set
- Check EC2 security group settings
Additional Resources
- Flask Documentation: https://flask.palletsprojects.com/
- GitHub Webhooks Guide: https://docs.github.com/webhooks
- Nginx Documentation: https://nginx.org/en/docs/
- AWS EC2 Documentation: https://docs.aws.amazon.com/ec2/
Top comments (0)