Skip to content

Commit 0c88ef9

Browse files
committed
Add support for the "push" event
In addition to executing a script when a new release is published, add the ability to execute another script whenever a push occurs on the default branch
1 parent af93108 commit 0c88ef9

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

.env.defaults

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
SECRET=
2-
SCRIPT=
1+
# Port to run the server on
32
PORT=number=32490
3+
4+
# GitHub WebHook secret (required)
5+
SECRET=
6+
7+
# Script to execute when a release is published
8+
SCRIPT_RELEASE=
9+
10+
# Script to execute when a push occurs in the default branch
11+
SCRIPT_PUSH=
12+
13+
# Name of the default branch, if it isn't `main` or `master`
14+
DEFAULT_BRANCH=
15+
16+
# Whether to print out all events
417
VERBOSE=boolean=false

README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# github-webhook
22

3-
A minimal GitHub webhook listener that executes a specified command when a release is published.
3+
A minimal GitHub webhook listener that executes a script when a push event occurs on the default branch, or when a release is published.
44

55
[![ci](https://github.com/jessety/github-webhook/workflows/ci/badge.svg)](https://github.com/jessety/github-webhook/actions)
66

@@ -9,25 +9,48 @@ A minimal GitHub webhook listener that executes a specified command when a relea
99
```bash
1010
git clone ...
1111
cd github-webhook
12+
1213
npm install --production
1314
pm2 start ecosystem.config.json
1415
```
1516

1617
## Configuration
1718

18-
Edit an `.env` file in the project directory with the following options:
19+
Populate an `.env` file in the project directory with the following options:
1920

2021
```ini
21-
# GitHub Webhook Secret
22+
# GitHub Webhook Secret (required)
2223
SECRET=ABC123
2324

2425
# Script to execute when a release is published
25-
SCRIPT=/usr/bin/local/update.sh
26+
SCRIPT_RELEASE=/usr/local/bin/release.sh
27+
28+
# Script to execute when there is a push to the default branch
29+
SCRIPT_PUSH=/usr/local/bin/push.sh
30+
31+
# Name of the default branch, if it isn't "main" or "master"
32+
DEFAULT_BRANCH=primary
2633

2734
# Port to run the server on
2835
PORT=32490
2936
```
3037

38+
## Scripts
39+
40+
Scripts are executed with the name of the repository as the first parameter, and the event type as the second.
41+
42+
Push events are only triggered when a push is made to the default branch. The script is executed with the name of that branch as its third parameter. Given the above configuration, the following will be executed a push is made to the `primary` branch:
43+
44+
```bash
45+
/usr/local/bin/push.sh jessety/github-webhook push primary
46+
```
47+
48+
When a release is triggered, the release tag is sent as a third parameter:
49+
50+
```bash
51+
/usr/local/bin/release.sh jessety/github-webhook release v1.1.4
52+
```
53+
3154
## License
3255

3356
MIT © Jesse Youngblood

src/index.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ const webhook = new WebHook(options);
1616

1717
webhook.on('error', error => console.error(error));
1818

19-
// If verbose mode is enabled, print out every event we receive
19+
// If verbose mode is enabled, log out every event
2020

2121
if (options.verbose === true) {
2222

23+
console.log(`Verbose mode enabled, logging all incoming events.`);
24+
2325
webhook.onAny((event, payload) => {
2426

2527
const { repository, action } = payload;
2628
console.log(`${new Date().toLocaleString()} - ${repository.full_name}: ${event} ${action ? `${action}` : ''}`);
2729
});
2830
}
2931

30-
// When a release is published, execute the update script
32+
// When a release is published, execute the 'release' script
3133

3234
webhook.on('release', payload => {
3335

@@ -39,10 +41,41 @@ webhook.on('release', payload => {
3941

4042
console.log(`\n\n${new Date().toLocaleString()} - ${repository.full_name} ${release.tag_name} ${action}`);
4143

42-
execute(options.script, repository.full_name);
44+
if (options.script_release === '') {
45+
return;
46+
}
47+
48+
execute(options.script_release, repository.full_name, 'release', release.tag_name);
49+
});
50+
51+
// When a push event occurs on the default branch, execute the 'push' script
52+
53+
webhook.on('push', payload => {
54+
55+
const branch = payload.ref.replace('refs/heads/', '');
56+
57+
const defaultBranchNames = ['main', 'master'];
58+
59+
if (options.default_branch !== '' && defaultBranchNames.includes(options.default_branch) === false) {
60+
defaultBranchNames.push(options.default_branch);
61+
}
62+
63+
if (defaultBranchNames.includes(branch) === false) {
64+
return;
65+
}
66+
67+
const { repository } = payload;
68+
69+
console.log(`\n\n${new Date().toLocaleString()} - ${repository.full_name} push ${branch}`);
70+
71+
if (options.script_push === '') {
72+
return;
73+
}
74+
75+
execute(options.script_push, repository.full_name, 'push', branch);
4376
});
4477

45-
// Listen for incoming events
78+
// Start listening for incoming events
4679

4780
webhook.listen().then(() => {
4881

0 commit comments

Comments
 (0)