-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.js
More file actions
33 lines (26 loc) · 1.05 KB
/
webhook.js
File metadata and controls
33 lines (26 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const express = require('express');
const { exec } = require('child_process');
const app = express();
const PORT = 3000; // You can change this if needed
app.use(express.json());
app.post('/webhook', (req, res) => {
const payload = req.body;
// Verify the event is a push to the main branch
if (payload.ref === 'refs/heads/main') {
console.log('Received push event. Pulling latest code...');
exec('git pull origin main && docker-compose down && docker-compose up -d --build', (err, stdout, stderr) => {
if (err) {
console.error(`Error pulling code: ${err.message}`);
return res.status(500).send('Error pulling code');
}
console.log(`Git Pull Output: ${stdout}`);
console.error(`Git Pull Errors: ${stderr}`);
res.status(200).send('Update triggered successfully');
});
} else {
res.status(400).send('Not a push to main');
}
});
app.listen(PORT, () => {
console.log(`Webhook listener running on port ${PORT}`);
});