webhook code for automaticl deployment
Some checks failed
GitHub Actions / build (18) (push) Has been cancelled
GitHub Actions / build (20) (push) Has been cancelled
GitHub Actions / build (22) (push) Has been cancelled
GitHub Actions / check (push) Has been cancelled

This commit is contained in:
2025-05-09 23:35:40 +02:00
parent 1a79ec5a66
commit 758c07baf1
2 changed files with 80 additions and 0 deletions

16
deploy.sh Normal file
View File

@@ -0,0 +1,16 @@
#!/bin/bash
# Navigate to the project directory
cd /path/to/your/project
# Pull the latest changes
git pull
# Install dependencies
npm install
# Build the project
npm run build
# Optional: Restart your web server if needed
# systemctl restart nginx

64
webhook.js Normal file
View File

@@ -0,0 +1,64 @@
const http = require('http');
const { exec } = require('child_process');
const crypto = require('crypto');
// Your Gitea webhook secret
const WEBHOOK_SECRET = 'your-secret-here';
const server = http.createServer((req, res) => {
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
// Verify webhook signature if Gitea is configured to send one
const signature = req.headers['x-gitea-signature'];
if (signature) {
const hmac = crypto.createHmac('sha256', WEBHOOK_SECRET);
const digest = hmac.update(body).digest('hex');
if (digest !== signature) {
res.writeHead(401);
res.end('Invalid signature');
return;
}
}
try {
const payload = JSON.parse(body);
// Check if this is a push event
if (payload.ref === 'refs/heads/main') { // or your default branch
exec('bash deploy.sh', (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error}`);
res.writeHead(500);
res.end('Deployment failed');
return;
}
console.log(`Deployment successful: ${stdout}`);
res.writeHead(200);
res.end('Deployment successful');
});
} else {
res.writeHead(200);
res.end('Not a push to main branch');
}
} catch (error) {
console.error('Error processing webhook:', error);
res.writeHead(400);
res.end('Invalid payload');
}
});
} else {
res.writeHead(405);
res.end('Method not allowed');
}
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Webhook server listening on port ${PORT}`);
});