A simple monitor that detects new commits on a GitHub repository branch and sends an email notification through the Resend API.
The project is designed to run through GitHub Actions. It fetches the latest commit from the configured branch, compares it with the SHA stored in state.json, and sends an email when a new commit is found.
- The
.github/workflows/monitor.ymlworkflow runs the check every 30 minutes. - The
scripts/check-commit.jsscript calls the GitHub API to fetch the latest commit from the configured branch. - The returned SHA is compared with the value stored in
state.json. - On the first run, the current SHA is saved without sending an email.
- If a new commit is found, the project sends an email with the commit details.
- After sending the email,
state.jsonis updated and committed automatically by GitHub Actions.
.
├── .github/workflows/monitor.yml
├── scripts/check-commit.js
├── package.json
├── state.json
└── README.md
The workflow uses the GITHUB_MONITOR environment and expects the following secrets:
| Secret | Description |
|---|---|
OWNER |
Owner or organization of the monitored repository. |
REPO |
Name of the monitored repository. |
BRANCH |
Monitored branch. Example: main or master. |
RESEND_API_KEY |
Resend API key used to send emails. |
TO_EMAIL |
Email address that receives notifications. |
FROM_EMAIL |
Sender address verified in Resend. |
GH_TOKEN |
GitHub token used to call the API. |
The workflow also needs write access to repository contents because it updates state.json automatically:
permissions:
contents: writeThe workflow runs in three situations:
- Every 30 minutes through cron.
- Manually through
workflow_dispatch. - On pushes to the
masterbranch of this repository.
Main trigger configuration:
on:
push:
branches:
- master
schedule:
- cron: "*/30 * * * *"
workflow_dispatch:This project has no external dependencies in package.json. It uses native Node.js features, including fetch, so use Node.js 20 or newer.
Set the environment variables and run:
npm run checkExample:
GITHUB_OWNER=octocat \
GITHUB_REPO=hello-world \
GITHUB_BRANCH=main \
RESEND_API_KEY=re_xxxxxxxxx \
TO_EMAIL=target@example.com \
FROM_EMAIL=monitor@example.com \
GITHUB_TOKEN=ghp_xxxxxxxxx \
npm run checkThe state.json file stores the last checked SHA:
{
"lastSha": "97f5393757e9c3d543cecc78ac5161b02dbdd3f3"
}This file prevents duplicate notifications for the same commit.
If it is empty or does not have lastSha, the next run is treated as the first run and no email is sent. It only saves the current SHA.
When a new commit is detected, the email contains:
- Monitored repository.
- Monitored branch.
- Short commit SHA.
- Author.
- Date.
- Commit message.
- Link to view the commit on GitHub.
- The monitored branch is defined by the
BRANCHsecret. IfGITHUB_BRANCHis not provided, the script defaults tomain. - The workflow push trigger is configured for the
masterbranch of this repository, not necessarily the monitored branch. - The first monitoring run does not send an email to avoid an unwanted initial notification.