From f4c0b0188962cb482ce8063036ed87c82124d31d Mon Sep 17 00:00:00 2001 From: Valdr687 <106614142+Valdr687@users.noreply.github.com> Date: Wed, 28 Oct 2026 18:13:46 +0100 Subject: [PATCH 1/7] wip --- .dockerignore | 3 +++ .env.example | 10 ++++++++++ .gitignore | 1 + Dockerfile | 31 +++++++++++++++++++++++++++++++ package.json | 2 +- 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 .env.example create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a21f178 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +node_modules +dist +.git diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..e864a4a --- /dev/null +++ b/.env.example @@ -0,0 +1,10 @@ +# Copy this file to .env and fill in the values + +# Url to send the filtered webhooks to (required) +WEBHOOK_URL= + +# Set to true to drop events not defined in filterConfiguration.json +BLOCK_UNDEFINED_EVENTS=false + +# Port exposed on the host +PORT=3000 diff --git a/.gitignore b/.gitignore index f06235c..9c97bbd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules dist +.env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c9edf2a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +# Build stage: compile the TypeScript sources +FROM node:26.12.1-alpine AS build + +WORKDIR /usr/src/app + +COPY package*.json ./ +RUN npm ci + +COPY tsconfig.json ./ +COPY src ./src +RUN npm run build + +# Runtime stage: only production dependencies and compiled output +FROM node:26.12.1-alpine + +WORKDIR /usr/src/app + +ENV NODE_ENV=production +# Listen on all interfaces so the server is reachable from outside the container +ENV HOST=0.0.0.0 +ENV PORT=3000 + +COPY package*.json ./ +RUN npm ci --omit=dev + +COPY --from=build /usr/src/app/dist ./dist + +EXPOSE 3000 + +# WEBHOOK_URL must be provided at runtime, e.g. `docker run -e WEBHOOK_URL=...` +CMD ["node", "dist/src/server.js"] diff --git a/package.json b/package.json index dd17a36..a8f0e9e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "prebuild": "rm -rf dist", - "build": "tsc -p tsconfig.json & npm run copyfiles", + "build": "tsc -p tsconfig.json && npm run copyfiles", "copyfiles": "copyfiles package* dist", "start": "node dist/src/server.js", "watch": "ts-node-dev src/server.ts", From 5b9be1c24519ce8d0cff5fffd12eaf4500f84d02 Mon Sep 17 00:00:00 2001 From: Valdr687 <106614142+Valdr687@users.noreply.github.com> Date: Wed, 28 Oct 2026 18:50:59 +0100 Subject: [PATCH 2/7] chore: Update JS and fix an error code --- src/routes.ts | 4 +--- tsconfig.json | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/routes.ts b/src/routes.ts index c837e8f..0f1eb7c 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -1,14 +1,12 @@ import { FastifyInstance } from 'fastify'; import { EventBody, handleEvents } from './handleEvents'; - - export default async function routes(fastify: FastifyInstance) { fastify.post<{Body: EventBody}>('*', (request, reply) => { const { headers, body } = request; const event = headers['x-github-event'] as string; if (!event) { - reply.send(400) + reply.code(400).send() return } const {status, message} = handleEvents(event, body, fastify.log); diff --git a/tsconfig.json b/tsconfig.json index 6449545..f5b3144 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,7 @@ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* Language and Environment */ - "target": "es5", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "es2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ From 02d7acc3e2daec562168125294b67f3fb1dd4739 Mon Sep 17 00:00:00 2001 From: Valdr687 <106614142+Valdr687@users.noreply.github.com> Date: Wed, 28 Oct 2026 20:47:38 +0100 Subject: [PATCH 3/7] feat: publish, and more graceful stop handling --- .dockerignore | 2 ++ .env.example | 2 -- .github/workflows/publish.yaml | 63 ++++++++++++++++++++++++++++++++++ Dockerfile | 11 ++++-- README.md | 10 ++++++ compose.yml | 15 ++++++++ src/server.ts | 24 +++++++++++++ 7 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/publish.yaml create mode 100644 compose.yml diff --git a/.dockerignore b/.dockerignore index a21f178..4d717c3 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,5 @@ node_modules dist .git +.env +.gitignore diff --git a/.env.example b/.env.example index e864a4a..d987777 100644 --- a/.env.example +++ b/.env.example @@ -6,5 +6,3 @@ WEBHOOK_URL= # Set to true to drop events not defined in filterConfiguration.json BLOCK_UNDEFINED_EVENTS=false -# Port exposed on the host -PORT=3000 diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml new file mode 100644 index 0000000..b958a5a --- /dev/null +++ b/.github/workflows/publish.yaml @@ -0,0 +1,63 @@ +name: Publish + +on: + release: + types: [published] + +permissions: + contents: read + +jobs: + check: + name: Build and type check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 26 + cache: npm + + - run: npm ci + - run: npm run build + + publish: + name: Publish Docker image + needs: check + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 + + - uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - id: meta + uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0 + with: + images: ghcr.io/${{ github.repository }} + # Stable releases also get the latest and major.minor tags, + # prereleases get only their exact version tag. + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + + - uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 + with: + context: . + platforms: linux/amd64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile index c9edf2a..b50532f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,9 @@ COPY tsconfig.json ./ COPY src ./src RUN npm run build +# Drop dev dependencies so node_modules can be reused as-is in the runtime stage +RUN npm prune --omit=dev + # Runtime stage: only production dependencies and compiled output FROM node:26.12.1-alpine @@ -20,11 +23,13 @@ ENV NODE_ENV=production ENV HOST=0.0.0.0 ENV PORT=3000 -COPY package*.json ./ -RUN npm ci --omit=dev - +COPY --from=build /usr/src/app/package*.json ./ +COPY --from=build /usr/src/app/node_modules ./node_modules COPY --from=build /usr/src/app/dist ./dist +# Run as the unprivileged user provided by the node image +USER node + EXPOSE 3000 # WEBHOOK_URL must be provided at runtime, e.g. `docker run -e WEBHOOK_URL=...` diff --git a/README.md b/README.md index f9ce996..d92a701 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,13 @@ And you are done ! Now you can edit the *fully typed* file [filterConfiguration.json](src/filterConfiguration.json), to filter as you would like your webhooks. +### With Docker Compose + +- Copy [.env.example](.env.example) to `.env` and set `WEBHOOK_URL` +- Run `docker compose up -d` [^1] + +The port on the host can be configured using [the compose file](compose.yml). The [filterConfiguration.json](src/filterConfiguration.json) is mounted into the container, to apply changes, restart the container with `docker compose restart`[^2]. + ## Configuration ### Environment variables @@ -42,3 +49,6 @@ If you want to test locally, you will need to redirect the webhook sent by Githu You can also have access to the recent deliveries of a webhook, as explained here: https://docs.github.com/en/developers/webhooks-and-events/webhooks/testing-webhooks. To generate new events json schema, add them to the file `events/index.ts` and run the command `npm run generate-schemas`. + +[^1]: Or with `podman compose up -d` +[^2]: Or with `podman compose restart` \ No newline at end of file diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..dd525f7 --- /dev/null +++ b/compose.yml @@ -0,0 +1,15 @@ +services: + github-webhook-filter: + image: ghcr.io/rezoleo/github-webhook-filter:1.0.0 + container_name: github-webhook-filter + ports: + - 3000:3000 + environment: + # URL to send the events to + - WEBHOOK_URL=${WEBHOOK_URL:?WEBHOOK_URL is required} + # Choose wether to block events other than those defined in filterConfiguration + - BLOCK_UNDEFINED_EVENTS=${BLOCK_UNDEFINED_EVENTS:-false} + volumes: + # Edit the filters without rebuilding the image, restart the container to apply them + - ./src/filterConfiguration.json:/usr/src/app/dist/src/filterConfiguration.json:ro,z + restart: unless-stopped diff --git a/src/server.ts b/src/server.ts index e2f51e5..9b6d2ff 100644 --- a/src/server.ts +++ b/src/server.ts @@ -13,3 +13,27 @@ server.listen({host: process.env.HOST || "127.0.0.1", port: Number(process.env.P } server.log.info(`Server listening at ${address}`); }); + +// Give in-flight requests time to finish, but exit before Docker's 10s stop timeout sends SIGKILL +const SHUTDOWN_TIMEOUT_MS = 5000; + +async function closeGracefully(signal: NodeJS.Signals) { + server.log.info(`Received signal to terminate: ${signal}`); + + setTimeout(() => { + server.log.error(`Server did not close within ${SHUTDOWN_TIMEOUT_MS}ms, forcing exit`); + process.exit(1); + }, SHUTDOWN_TIMEOUT_MS).unref(); + + try { + await server.close(); + // await other things we should cleanup nicely + } catch (err) { + server.log.error(err); + process.exit(1); + } + // Re-send the signal now that the handler is removed, so the process exits with the expected code + process.kill(process.pid, signal); +} +process.once('SIGINT', closeGracefully); +process.once('SIGTERM', closeGracefully); From 25083d4cfee5c03c09a8645814bfe921fa2520e5 Mon Sep 17 00:00:00 2001 From: Valdr687 <106614142+Valdr687@users.noreply.github.com> Date: Fri, 30 Oct 2026 20:14:54 +0100 Subject: [PATCH 4/7] simplify the sigkill --- src/server.ts | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/server.ts b/src/server.ts index 9b6d2ff..6d1633d 100644 --- a/src/server.ts +++ b/src/server.ts @@ -14,25 +14,10 @@ server.listen({host: process.env.HOST || "127.0.0.1", port: Number(process.env.P server.log.info(`Server listening at ${address}`); }); -// Give in-flight requests time to finish, but exit before Docker's 10s stop timeout sends SIGKILL -const SHUTDOWN_TIMEOUT_MS = 5000; - async function closeGracefully(signal: NodeJS.Signals) { server.log.info(`Received signal to terminate: ${signal}`); - setTimeout(() => { - server.log.error(`Server did not close within ${SHUTDOWN_TIMEOUT_MS}ms, forcing exit`); - process.exit(1); - }, SHUTDOWN_TIMEOUT_MS).unref(); - - try { - await server.close(); - // await other things we should cleanup nicely - } catch (err) { - server.log.error(err); - process.exit(1); - } - // Re-send the signal now that the handler is removed, so the process exits with the expected code + await server.close(); process.kill(process.pid, signal); } process.once('SIGINT', closeGracefully); From c9204ad465e655c370ae5a0642b2f081282dc54a Mon Sep 17 00:00:00 2001 From: Valdr687 <106614142+Valdr687@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:12:38 +0100 Subject: [PATCH 5/7] remove z flag --- compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compose.yml b/compose.yml index dd525f7..53554c9 100644 --- a/compose.yml +++ b/compose.yml @@ -11,5 +11,5 @@ services: - BLOCK_UNDEFINED_EVENTS=${BLOCK_UNDEFINED_EVENTS:-false} volumes: # Edit the filters without rebuilding the image, restart the container to apply them - - ./src/filterConfiguration.json:/usr/src/app/dist/src/filterConfiguration.json:ro,z + - ./src/filterConfiguration.json:/usr/src/app/dist/src/filterConfiguration.json:ro restart: unless-stopped From 5094d16a53068e485577db104b8b86f3c5d95e8b Mon Sep 17 00:00:00 2001 From: Valdr687 <106614142+Valdr687@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:23:49 +0100 Subject: [PATCH 6/7] init and better path --- Dockerfile | 11 ++++++----- compose.yml | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index b50532f..c996569 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # Build stage: compile the TypeScript sources FROM node:26.12.1-alpine AS build -WORKDIR /usr/src/app +WORKDIR /app COPY package*.json ./ RUN npm ci @@ -16,16 +16,17 @@ RUN npm prune --omit=dev # Runtime stage: only production dependencies and compiled output FROM node:26.12.1-alpine -WORKDIR /usr/src/app +WORKDIR /app ENV NODE_ENV=production # Listen on all interfaces so the server is reachable from outside the container ENV HOST=0.0.0.0 ENV PORT=3000 -COPY --from=build /usr/src/app/package*.json ./ -COPY --from=build /usr/src/app/node_modules ./node_modules -COPY --from=build /usr/src/app/dist ./dist +COPY --from=build --chown=node:node /app/package*.json ./ +COPY --from=build --chown=node:node /app/node_modules ./node_modules +COPY --from=build --chown=node:node /app/dist ./dist + # Run as the unprivileged user provided by the node image USER node diff --git a/compose.yml b/compose.yml index 53554c9..173bb98 100644 --- a/compose.yml +++ b/compose.yml @@ -2,6 +2,7 @@ services: github-webhook-filter: image: ghcr.io/rezoleo/github-webhook-filter:1.0.0 container_name: github-webhook-filter + init: true ports: - 3000:3000 environment: @@ -11,5 +12,5 @@ services: - BLOCK_UNDEFINED_EVENTS=${BLOCK_UNDEFINED_EVENTS:-false} volumes: # Edit the filters without rebuilding the image, restart the container to apply them - - ./src/filterConfiguration.json:/usr/src/app/dist/src/filterConfiguration.json:ro + - ./path/to/filterConfiguration.json:/app/dist/src/filterConfiguration.json:ro restart: unless-stopped From 0e385226349d24b3de7c4adc3c181fb5a29db140 Mon Sep 17 00:00:00 2001 From: Valdr687 <106614142+Valdr687@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:35:00 +0100 Subject: [PATCH 7/7] edit CI --- .github/workflows/publish.yaml | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index b958a5a..4bd3d51 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -1,6 +1,9 @@ -name: Publish +name: CI on: + push: + branches: [main] + pull_request: release: types: [published] @@ -24,8 +27,8 @@ jobs: - run: npm ci - run: npm run build - publish: - name: Publish Docker image + container: + name: Build image needs: check runs-on: ubuntu-latest permissions: @@ -39,6 +42,7 @@ jobs: - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 - uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0 + if: github.event_name == 'release' with: registry: ghcr.io username: ${{ github.actor }} @@ -58,6 +62,6 @@ jobs: with: context: . platforms: linux/amd64 - push: true + push: ${{ github.event_name == 'release' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }}