# github-webhooks-exec

> Execute shell commands on GitHub webhook events.

Latest version **1.5.0** (published 2022-11-16) · 0 weekly downloads

## Install

```sh
npm install github-webhooks-exec
pnpm add github-webhooks-exec
yarn add github-webhooks-exec
bun add github-webhooks-exec
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.0 |
| Published | 2022-11-16 |
| First published | 2022-10-27 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 4.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | phparkle |

## Links

- npm: https://www.npmjs.com/package/github-webhooks-exec
- npm.io page: https://npm.io/package/github-webhooks-exec

## Dependencies (4)

- [pino](https://npm.io/package/pino.md) ^8.7.0
- [fastq](https://npm.io/package/fastq.md) ^1.13.0
- [dotenv](https://npm.io/package/dotenv.md) ^16.0.3
- [@octokit/webhooks](https://npm.io/package/@octokit/webhooks.md) ^10.3.1

## Recent versions

- 1.5.0 (latest) — 2022-11-16
- 1.4.0 — 2022-11-11
- 1.3.1 — 2022-10-28
- 1.3.0 — 2022-10-28
- 1.2.1 — 2022-10-27
- 1.2.0 — 2022-10-27
- 1.1.5 — 2022-10-27
- 1.1.4 — 2022-10-27
- 1.1.3 — 2022-10-27
- 1.1.2 — 2022-10-27
- 1.1.1 — 2022-10-27
- 1.1.0 — 2022-10-27
- 1.0.0 — 2022-10-27

## README

# github-webhooks-exec

Execute shell commands on GitHub webhook events.

- Works with pm2 to start automatically on boot
- Queues commands to ensure sequential execution

## Requirements

- Node.js
- pm2

## Installation

```bash
npm install github-webhooks-exec
```

## Initial setup

### webhooks.js

Create `webhooks.js` in the directory where you want to execute shell commands from:

```js
const { webhooks, queue, logger } = require('github-webhooks-exec');

webhooks.on('push', event => {
  logger.info('Received push event');
  const commands = [
    'set -e',
    // etc.
  ];
  queue.push(commands.join('; '));
});
```

### .env

Create `.env` in the same directory:

```ini
GITHUB_WEBHOOKS_PORT=3000
GITHUB_WEBHOOKS_SECRET=password
```

Generate a long random string for `GITHUB_WEBHOOKS_SECRET`.

### ecosystem.config.js

Create `ecosystem.config.js`:

```js
module.exports = {
  apps: [
    {
      name: 'my-project-webhooks',
      script: './webhooks.js',
    },
  ],
};
```

## Running the server

Ensure pm2 is installed and set up:

```bash
npm install -g pm2
pm2 startup
```

Start the server:

```bash
pm2 start ecosystem.config.js
```

Save the pm2 app list to be restored on reboot:

```bash
pm2 save
```

## Adding the webhook on GitHub

In Settings > Webhooks of any GitHub repository or organization, add a webhook with the following settings:

| Field | Value |
| --- | --- |
| Payload URL | https://your.domain:3000/api/github/webhooks |
| Content type | application/json |
| Secret | The value of `GITHUB_WEBHOOKS_SECRET` in the `.env` file |
| Which events would you like to trigger this webhook? | Just the `push` event |
| Active | checked |

## Viewing logs

Install `pino-pretty`:

```bash
npm i -g pino-pretty
```

Tail logs:

```bash
pm2 logs --raw | pino-pretty
```

## API Reference

`github-webhooks-exec` provides the following named exports:

### webhooks

A `Webhooks` instance initialized with `GITHUB_WEBHOOKS_SECRET` from `.env`.

See https://github.com/octokit/webhooks.js/

### queue

A `fastq` instance that runs shell commands sequentially.

See https://github.com/mcollina/fastq

### logger

A `pino` logger instance.

See https://github.com/pinojs/pino

---
_Source: https://npm.io/package/github-webhooks-exec · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
