https://github.com/waseigo/webhook-gitlab-nextjs-runner
Go program that runs a NextJS app and responds to Gitlab's push-event webhook requests with a rebuild and restart of the app.
https://github.com/waseigo/webhook-gitlab-nextjs-runner
ci nextjs webhook-server
Last synced: 8 months ago
JSON representation
Go program that runs a NextJS app and responds to Gitlab's push-event webhook requests with a rebuild and restart of the app.
- Host: GitHub
- URL: https://github.com/waseigo/webhook-gitlab-nextjs-runner
- Owner: waseigo
- License: apache-2.0
- Created: 2023-12-05T22:33:37.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-07T23:12:42.000Z (almost 2 years ago)
- Last Synced: 2025-01-01T09:26:32.902Z (9 months ago)
- Topics: ci, nextjs, webhook-server
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# webhook-gitlab-nextjs-runner
I'm using this program to automatically rebuild and restart a NextJS app whenever a commit is pushed to the app's git repo on Gitlab.
There is actually nothing specific to either Gitlab (besides the `X-Gitlab-Token` header in the `authenticate()` function) or NextJS, and the program can be modified to work with any header, and to kill any process (not only `npm start`) based on the port the process occupies.
## Build
`make build`
## Setup
First do a `git clone` of your repo on the machine where your NextJS app will run.
Edit `run_webhook.sh` and provide the following environment variables:
* `GIT_REPO_PATH`: the on-disk location of your cloned repo,
* `WEBHOOK_SECRET_TOKEN`: the secret token you generated on Gitlab for authorizing webhook requests,
* `WEBHOOK_PORT`: the port on which the webhook handler will run (default: 8000), and
* `NEXTJS_PORT`: the port which NextJS will use (default: 3000).Copy `run_webhook.sh` and the `webhook` binary somewhere on the same machine where the NextJS app will run.
Execute `./webhook`, perhaps in tmux or GNU Screen.
## Usage
Upon launch, the program performs a `git pull` and, if needed, will install dependencies, will build, and will finally start the NextJS app in a coroutine.
If your app has already been running, it keeps running, except if the `git pull` indicates that there are updates.
Each webhook request triggers a `git pull`. Upon that: if there are changes, the program terminates any running process occupying the specified port (default: 3000) and does the following:
1. `npm install`
2. `npm run build`, and
3. `npm start` in a goroutine.## Example output
```
$ ./run_webhook.sh
2023-12-08T00:42:54+02:00 🛋️ Performing initial setup…
2023-12-08T00:42:54+02:00 🔎 Checking whether 'npm start' is already running on port 3000
2023-12-08T00:42:54+02:00 ✅ Found PID of process running on port 3000: 14140
2023-12-08T00:42:54+02:00 🤷 The app was running already; will not update
2023-12-08T00:42:54+02:00 🚀 Starting the webhook server on port 8000
2023-12-08T00:51:07+02:00 🤝 Received a valid secret token from Gitlab
2023-12-08T00:51:07+02:00 ⚠️ 'git push' detected
2023-12-08T00:51:07+02:00 📡 Performing 'git pull'…
2023-12-08T00:51:09+02:00 ✅ 'git pull' completed
2023-12-08T00:51:09+02:00 🔃 Rebuilding required
2023-12-08T00:51:09+02:00 💣 Killing 'npm start'…
2023-12-08T00:51:09+02:00 ✅ Found PID of process running on port 3000: 14140
2023-12-08T00:51:09+02:00 💣 Killing process with PID 14140
2023-12-08T00:51:09+02:00 🛠️ Running 'npm install'…
2023-12-08T00:51:11+02:00 ✅ 'npm install' completed
2023-12-08T00:51:11+02:00 🏗️ Running 'npm run build'…
2023-12-08T00:51:40+02:00 ✅ 'npm run build' completed
2023-12-08T00:51:40+02:00 🥳 Update completed and 'npm start' issued
```